-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathApp.jsx
151 lines (129 loc) · 4.96 KB
/
App.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React, {useEffect, useState} from 'react';
import {BrowserRouter as Router, Redirect, Route, Switch} from "react-router-dom";
import NavBar from './components/NavBar'
import TopBar from "./components/TopBar";
import MainContent from "./components/MainContent";
import Auth from "./components/pages/Auth";
import checkLogin from "./components/CheckLogin";
import jwt_decode from "jwt-decode";
import Cookies from "js-cookie";
import './App.css'
const baseUrl = "https://api.icaf.site/api/v1";
function App() {
// User login states
const [role, setRole] = useState(checkLogin());
const [userId, setUserId] = useState();
// Base API URL for the logged-in user
const [baseUserUrl, setBaseUserUrl] = useState("");
// User account data states
const [user, setUser] = useState({
role: "",
fname: " ",
lname: " ",
email: "",
contact: "",
avatar: ""
});
// Application-wide UI states
const [collapsed, setCollapsed] = useState(false);
const [fullscreen, setFullscreen] = useState(false);
// User data related app-wide UI states
const [avatarSrc, setAvatarSrc] = useState("");
const [avatarTxt, setAvatarTxt] = useState(" ");
// Set userId and baseUserUrl if and when the user is logged in (when role is set)
useEffect(() => {
if (role) {
setUserId(jwt_decode(Cookies.get("adminToken")).id);
setBaseUserUrl(`${baseUrl}/${role}s`);
}
}, [role]);
// Set user data
useEffect(() => {
// Fetch and set user data using the set API URL
if (baseUserUrl && userId) {
fetch(`${baseUserUrl}/${userId}`)
.then(raw => raw.json())
.then(data => setUser({role, ...data}))
.catch(err => console.log(err));
}
}, [baseUserUrl, userId]);
// Set user avatar src and fallback text
useEffect(() => {
setAvatarSrc(user.avatar && `${baseUserUrl}/image/${user.avatar}`);
setAvatarTxt(`${user.fname[0]}${user.lname[0]}`);
}, [user]);
// setState method wrappers to be passed to child components
const onSetCollapsed = () => setCollapsed(!collapsed);
// Toggle fullscreen
const onSetFullscreen = () => {
if (!fullscreen) {
document.documentElement.requestFullscreen()
.catch(err => console.error(err));
} else {
document.exitFullscreen()
.catch(err => console.error(err));
}
}
// On fullscreen change, set the state accordingly
document.documentElement.onfullscreenchange = () => {
document.fullscreenElement ?
setFullscreen(true) : setFullscreen(false);
}
// Login callback function
const loginCallback = () => {
setRole(checkLogin());
}
// Logout function
const logout = () => {
// Remove the JWT token
Cookies.remove("adminToken");
//Reset user related states
setRole(false);
setUserId("");
setUser({role: "", fname: " ", lname: " ", email: "", contact: "", avatar: ""});
}
return (
<div className="App">
<Router>
<Switch>
{/* Login/Register interface */}
<Route path="/auth">
{/* Redirect to dashboard if logged in */}
{role && <Redirect to="/" />}
<Auth baseUrl={baseUrl} loginCallback={loginCallback} />
</Route>
{/* Admin Panel interface | accessible only when logged in */}
<Route path="/">
{/* Redirect to Login/Register interface if not logged in */}
{!role && <Redirect to="/auth" />}
<NavBar role={role} collapsed={collapsed} />
<TopBar
collapsed={collapsed}
setCollapsed={onSetCollapsed}
fullscreen={fullscreen}
setFullscreen={onSetFullscreen}
logout={logout}
avatarSrc={avatarSrc}
avatarTxt={avatarTxt}
firstName={user.fname}
role={role}
userId={userId}
/>
{/* Main Content Area */}
<MainContent
collapsed={collapsed}
baseUrl={baseUrl}
baseUserUrl={baseUserUrl}
role={role}
user={user}
setUser={setUser}
avatarSrc={avatarSrc}
avatarTxt={avatarTxt}
/>
</Route>
</Switch>
</Router>
</div>
)
}
export default App;