Skip to content

Commit

Permalink
Update lazy load of pages, improve UX
Browse files Browse the repository at this point in the history
1. Update app.config.js — pages components are inported here
2. Add suspense component into every route of App
3. Add CenteredSpinner component for fallback, also for chat while it fetching
4. Remove pages/index.js
  • Loading branch information
SamIvan-ark committed Jan 17, 2024
1 parent 5897405 commit 426801e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 43 deletions.
47 changes: 28 additions & 19 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,55 @@
import React from 'react';
import React, { Suspense } from 'react';
import { ToastContainer } from 'react-toastify';
import {
BrowserRouter as Router, Routes, Route, Navigate,
} from 'react-router-dom';
import 'react-toastify/dist/ReactToastify.css';

import CenteredSpinner from './components/CenteredSpinner';
import config from './app.config';
import { hooks } from './providers';
import config from './app-config';
import pages from './pages';

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

return auth.loggedIn ? children : <Navigate to={redirectTo} />;
};
const WrappedRouteComponent = ({ children, isPrivate, redirectTo }) => (
<Suspense fallback={<CenteredSpinner />}>
{isPrivate ? (
<PrivateRoute redirectTo={redirectTo}>
{children}
</PrivateRoute>
) : children}
</Suspense>
);

const { pages } = config;

const App = () => (
<div className="h-100" id="chat">
<div className="d-flex flex-column h-100">
<Router>
<Routes>
{Object
.values(config.pages)
.values(pages)
.map(({
id,
route,
isPrivate,
component,
}) => {
const Component = pages[component]();
return (
<Route
key={id}
path={route}
element={isPrivate ? (
<PrivateRoute redirectTo="/login">
<Component />
</PrivateRoute>
) : <Component />}
/>
);
})}
Component,
redirectTo,
}) => (
<Route
key={id}
path={route}
element={(
<WrappedRouteComponent isPrivate={isPrivate} redirectTo={redirectTo}>
<Component />
</WrappedRouteComponent>
)}
/>
))}
</Routes>
</Router>
<ToastContainer />
Expand Down
11 changes: 7 additions & 4 deletions frontend/src/app-config.js → frontend/src/app.config.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import { lazy } from 'react';

const pages = {
notFoundPage: {
id: 0,
route: '*',
component: 'notFoundPage',
Component: lazy(() => import('./pages/NotFoundPage')),
isPrivate: false,
},
mainPage: {
id: 1,
route: '/',
component: 'mainPage',
redirectTo: '/login',
Component: lazy(() => import('./pages/MainPage')),
isPrivate: true,
},
loginPage: {
id: 2,
route: '/login',
component: 'loginPage',
Component: lazy(() => import('./pages/LoginPage')),
isPrivate: false,
},
signupPage: {
id: 3,
route: '/signup',
component: 'signupPage',
Component: lazy(() => import('./pages/SignupPage')),
isPrivate: false,
},
};
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/components/CenteredSpinner.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Spinner } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';

const CenteredSpinner = () => {
const { t } = useTranslation();
return (
<div className="d-flex justify-content-center align-items-center h-100">
<Spinner animation="border" variant="primary">
<span className="visually-hidden">{t('processes.loading')}</span>
</Spinner>
</div>
);
};

export default CenteredSpinner;
14 changes: 5 additions & 9 deletions frontend/src/pages/MainPage.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, { useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { Spinner } from 'react-bootstrap';
import { useDispatch } from 'react-redux';
import { useTranslation } from 'react-i18next';

import { Navbar, Chat } from '../components';
import { hooks } from '../providers';
import getAuthHeader from '../utils/getAuthHeader';
import CenteredSpinner from '../components/CenteredSpinner';
import { Chat, Navbar } from '../components';
import { fetchChatData } from '../api/serverApi';
import { hooks } from '../providers';
import { serverRoutes } from '../utils/routes';
import getAuthHeader from '../utils/getAuthHeader';
import { addChannels, setDefaultChannel, setActive } from '../slices/channelsSlice';
import { addMessages } from '../slices/messagesSlice';
import toasts from '../utils/toasts';
Expand Down Expand Up @@ -51,11 +51,7 @@ const MainPage = () => {
connectToChat();
}, [auth, dispatch, navigate, socket, t]);
return (fetchStatus !== 'idle') ? (
<div className="d-flex justify-content-center align-items-center h-100">
<Spinner animation="border" variant="primary">
<span className="visually-hidden">{t('processes.loading')}</span>
</Spinner>
</div>
<CenteredSpinner />
) : (
<>
<Navbar />
Expand Down
11 changes: 0 additions & 11 deletions frontend/src/pages/index.js

This file was deleted.

0 comments on commit 426801e

Please sign in to comment.