Skip to content

Commit

Permalink
Refactor pages adding into App with app-config
Browse files Browse the repository at this point in the history
  • Loading branch information
SamIvan-ark committed Jan 14, 2024
1 parent 4847304 commit 631871d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 30 deletions.
51 changes: 25 additions & 26 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,41 @@ import {
import 'react-toastify/dist/ReactToastify.css';

import { hooks } from './providers';
import {
MainPage,
LoginPage,
NotFoundPage,
SignupPage,
} from './pages';
import config from './app-config';
import pages from './pages';

const ConditionalRoute = ({ children, redirectWhenAuthIsExist, redirectTo }) => {
const PrivateRoute = ({ children, redirectTo }) => {
const auth = hooks.useAuth();

return redirectWhenAuthIsExist === auth.loggedIn ? <Navigate to={redirectTo} /> : children;
return auth.loggedIn ? children : <Navigate to={redirectTo} />;
};

const App = () => (
<div className="h-100" id="chat">
<div className="d-flex flex-column h-100">
<Router>
<Routes>
<Route path="*" element={<NotFoundPage />} />
<Route
path="/"
element={(
<ConditionalRoute redirectWhenAuthIsExist={false} redirectTo="/login">
<MainPage />
</ConditionalRoute>
)}
/>
<Route
path="/login"
element={(
<ConditionalRoute redirectWhenAuthIsExist redirectTo="/">
<LoginPage />
</ConditionalRoute>
)}
/>
<Route path="/signup" element={<SignupPage />} />
{Object
.values(config.pages)
.map(({
id,
route,
isPrivate,
component,
}) => {
const Component = pages[component]();
return (
<Route
key={id}
path={route}
element={isPrivate ? (
<PrivateRoute redirectTo="/login">
<Component />
</PrivateRoute>
) : <Component />}
/>
);
})}
</Routes>
</Router>
<ToastContainer />
Expand Down
30 changes: 30 additions & 0 deletions frontend/src/app-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const pages = {
notFoundPage: {
id: 0,
route: '*',
component: 'notFoundPage',
isPrivate: false,
},
mainPage: {
id: 1,
route: '/',
component: 'mainPage',
isPrivate: true,
},
loginPage: {
id: 2,
route: '/login',
component: 'loginPage',
isPrivate: false,
},
signupPage: {
id: 3,
route: '/signup',
component: 'signupPage',
isPrivate: false,
},
};

export default {
pages,
};
15 changes: 11 additions & 4 deletions frontend/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export { default as LoginPage } from './LoginPage';
export { default as MainPage } from './MainPage';
export { default as NotFoundPage } from './NotFoundPage';
export { default as SignupPage } from './SignupPage';
import LoginPage from './LoginPage';
import MainPage from './MainPage';
import NotFoundPage from './NotFoundPage';
import SignupPage from './SignupPage';

export default {
loginPage: () => LoginPage,
mainPage: () => MainPage,
notFoundPage: () => NotFoundPage,
signupPage: () => SignupPage,
};

0 comments on commit 631871d

Please sign in to comment.