Skip to content

Commit

Permalink
add main api
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatyana-js committed Dec 28, 2024
1 parent 6a5e242 commit 324b313
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
23 changes: 9 additions & 14 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,27 @@ import PageNotFound from './pages/PageNotFound.jsx';
import MainPage from './pages/MainPage.jsx';
import LoginPage from './pages/LoginPage.jsx';
import router from './utils/routes.js';
import AuthProvider, { PrivateRoute } from './context/AuthProvider.jsx';
import AuthProvider from './context/AuthProvider.jsx';

const App = () => {
const { t } = useTranslation();

return (
<BrowserRouter>
<div className="d-flex h-100 flex-column">
<Navbar bg="white" expand="lg" className="shadow-sm">
<Container>
<Navbar.Brand href="/">{t('navBar.title')}</Navbar.Brand>
<button type="button" className="btn btn-primary">{t('navBar.button')}</button>
</Container>
</Navbar>
<AuthProvider>
<Navbar bg="white" expand="lg" className="shadow-sm">
<Container>
<Navbar.Brand href="/">{t('navBar.title')}</Navbar.Brand>
<button type="button" className="btn btn-primary">{t('navBar.button')}</button>
</Container>
</Navbar>
<Routes>
<Route path={router.mainPath} element={(
<PrivateRoute>
<MainPage />
</PrivateRoute>)} />
<Route path={router.main} element={(<MainPage />)} />
<Route path={router.login} element={<LoginPage />} />
<Route path={router.main()} element={<MainPage />} />
<Route path={router.login()} element={<LoginPage />} />
<Route path="*" element={<PageNotFound />} />
</Routes>
</AuthProvider>

</div>
</BrowserRouter>
);
Expand Down
12 changes: 0 additions & 12 deletions frontend/src/context/AuthProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import useAuth from '../hooks/index.jsx';
import AuthContext from '../context/index.jsx';
import router from '../utils/routes.js';

// eslint-disable-next-line react/prop-types
const AuthProvider = ({ children }) => {
Expand All @@ -21,13 +18,4 @@ const AuthProvider = ({ children }) => {
);
};

export const PrivateRoute = ({ children }) => {
const auth = useAuth();
const navigate = useNavigate();

return (
auth.loggedIn ? children : navigate(router.mainPath)
);
};

export default AuthProvider;
5 changes: 5 additions & 0 deletions frontend/src/locales/ru.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Channels from "../components/Сhannels";

export default {
translation: {
loginForm: {
Expand All @@ -11,6 +13,9 @@ export default {
navBar: {
title: 'Hexlet Chat',
button: 'Выйти',
},
channels: {
title: 'Каналы',
}
// singUpForm: {
// username: ''
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/LoginPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ const LoginPage = () => {
onSubmit: async (values) => {
setAuthFailed(false);
try {
const res = await axios.post(router.loginPath, values);
const res = await axios.post(router.loginPath(), values);
console.log(res);
localStorage.setItem('token', res.data.token);
auth.logIn();
navigate(router.main);
navigate(router.main());
} catch (err) {
formik.setSubmitting(false);
if (axios.isAxiosError(err) && err.response.status === 401) {
Expand Down
16 changes: 15 additions & 1 deletion frontend/src/pages/MainPage.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
import { Container, Row, Col, Nav } from 'react-bootstrap';
import { useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import AddChannelsButton from '../components/AddCannelsButton.jsx';
import Channel from '../components/Сhannels.jsx';
import MessageField from '../components/MessageField.jsx';
import useAuth from '../hooks/index.jsx';
import router from '../utils/routes.js';

const MainPage = () => {
const { t } = useTranslation();
const { loggedIn } = useAuth();
const navigate = useNavigate();

useEffect(() => {
if (!loggedIn) {
navigate(router.login());
}
}, []);

return (
<Container className="h-100 my-4 overflow-hidden rounded shadow">
<Row className="h-100 bg-white flex-md-row">
<Col sx={4} className="col-md-2 border-end px-0 bg-light flex-column h-100 d-flex">
<div className="d-flex mt-1 justify-content-between mb-2 ps-4 pe-2 p-4">
<b>Каналы</b>
<b>{t('channels.title')}</b>
<AddChannelsButton />
</div>
<Nav as="ul" id="channels-box" className="flex-column nav-pills nav-fill px-2 mb-3 overflow-auto h-100 d-block">
Expand Down
9 changes: 4 additions & 5 deletions frontend/src/utils/routes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// const apiPath = '/api/v1';
const apiPath = '/api/v1';

export default {
login: '/login',
loginPath: '/api/v1/login',
mainPath: '/api/v1/channels',
main: '/',
main: () => '/',
login: () => '/login',
loginPath: () => [apiPath, 'login'].join('/'),
};

0 comments on commit 324b313

Please sign in to comment.