[d8b6c91] | 1 | import logo from './logo.svg';
|
---|
| 2 | import './App.css';
|
---|
[b248810] | 3 | import {useDispatch, useSelector} from "react-redux";
|
---|
[d8b6c91] | 4 | import {BrowserRouter} from "react-router-dom";
|
---|
| 5 | import {Header} from "./views/static/Header";
|
---|
[befb988] | 6 | import RoutesConfig from "./auth/RoutesConfig";
|
---|
| 7 | import React, {useEffect, useState} from "react";
|
---|
[d8b6c91] | 8 | import {AuthActions} from "./redux/actions/authActions";
|
---|
| 9 | import {AUTH_TOKEN} from "./axios/axiosInstance";
|
---|
[b248810] | 10 | import {jwtDecode} from "jwt-decode";
|
---|
| 11 | import {NoAccess} from "./views/static/NoAccess";
|
---|
[befb988] | 12 | import {Loading} from "./views/static/Loading";
|
---|
| 13 | import {ToastContainer} from "react-toastify";
|
---|
[d8b6c91] | 14 |
|
---|
| 15 | function App() {
|
---|
| 16 | const dispatch = useDispatch();
|
---|
| 17 |
|
---|
| 18 | useEffect(() => {
|
---|
| 19 | dispatch(AuthActions.updateToken(localStorage.getItem(AUTH_TOKEN)))
|
---|
| 20 | }, [dispatch])
|
---|
| 21 |
|
---|
[b248810] | 22 | const [user, setUser] = useState(null);
|
---|
| 23 | const [loading, setLoading] = useState(true);
|
---|
[befb988] | 24 | const [minimumLoadingTime, setMinimumLoadingTime] = useState(true);
|
---|
| 25 |
|
---|
[b248810] | 26 | const auth = useSelector(state => state.auth);
|
---|
| 27 |
|
---|
[befb988] | 28 | useEffect(() => {
|
---|
| 29 | // Simulate a minimum loading time of 1 second
|
---|
| 30 | const timer = setTimeout(() => {
|
---|
| 31 | setMinimumLoadingTime(false);
|
---|
| 32 | }, 1000);
|
---|
| 33 |
|
---|
| 34 | // Clear timeout if component unmounts
|
---|
| 35 | return () => clearTimeout(timer);
|
---|
| 36 | }, []);
|
---|
| 37 |
|
---|
[b248810] | 38 | useEffect(() => {
|
---|
| 39 | const token = localStorage.getItem(AUTH_TOKEN);
|
---|
| 40 | if (token !== null) {
|
---|
| 41 | try {
|
---|
| 42 | const decodedToken = jwtDecode(token);
|
---|
| 43 | setUser({
|
---|
| 44 | name: decodedToken.name,
|
---|
| 45 | role: decodedToken.role,
|
---|
[befb988] | 46 | hasAccess: decodedToken.access,
|
---|
| 47 | id: decodedToken.id
|
---|
[b248810] | 48 | });
|
---|
| 49 | setLoading(false);
|
---|
| 50 | } catch (error) {
|
---|
| 51 | console.error('Failed to decode token', error);
|
---|
| 52 | setLoading(false);
|
---|
| 53 | }
|
---|
| 54 | } else {
|
---|
| 55 | setLoading(false);
|
---|
| 56 | }
|
---|
| 57 | }, [auth]);
|
---|
| 58 |
|
---|
[befb988] | 59 | if (loading || minimumLoadingTime) {
|
---|
| 60 | return <Loading />; // Replace LoadingSpinner with your loading indicator component
|
---|
[b248810] | 61 | }
|
---|
| 62 |
|
---|
[d8b6c91] | 63 | return (
|
---|
| 64 | <div className="App">
|
---|
| 65 | <BrowserRouter>
|
---|
[b248810] | 66 | {user === null ? (
|
---|
| 67 | <>
|
---|
| 68 | <Header />
|
---|
| 69 | <RoutesConfig />
|
---|
| 70 | </>
|
---|
| 71 | ) : user.hasAccess ? (
|
---|
| 72 | <>
|
---|
| 73 | <Header />
|
---|
| 74 | <RoutesConfig />
|
---|
[befb988] | 75 | {/*<Loading/>*/}
|
---|
[b248810] | 76 | </>
|
---|
| 77 | ) : (
|
---|
| 78 | <NoAccess user={user}/>
|
---|
| 79 | )}
|
---|
[d8b6c91] | 80 | </BrowserRouter>
|
---|
[befb988] | 81 | <ToastContainer/>
|
---|
[d8b6c91] | 82 | </div>
|
---|
| 83 | );
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | export default App;
|
---|