-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathApp.js
308 lines (281 loc) · 10.4 KB
/
App.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import { useState, useEffect, useMemo, useContext } from "react";
// react-router components
import { Routes, Route, Navigate, useLocation, useNavigate } from "react-router-dom";
// @mui material components
import { ThemeProvider } from "@mui/material/styles";
import CssBaseline from "@mui/material/CssBaseline";
import Icon from "@mui/material/Icon";
// Material Dashboard 2 React components
import MDBox from "components/MDBox";
// Material Dashboard 2 React example components
import Sidenav from "examples/Sidenav";
import Configurator from "examples/Configurator";
// Material Dashboard 2 React themes
import theme from "assets/theme";
import themeRTL from "assets/theme/theme-rtl";
// Material Dashboard 2 React Dark Mode themes
import themeDark from "assets/theme-dark";
import themeDarkRTL from "assets/theme-dark/theme-rtl";
// RTL plugins
import rtlPlugin from "stylis-plugin-rtl";
import { CacheProvider } from "@emotion/react";
import createCache from "@emotion/cache";
// Material Dashboard 2 React routes
import routes from "routes";
// Material Dashboard 2 React contexts
import { useMaterialUIController, setMiniSidenav, setOpenConfigurator } from "context";
// Images
import brandWhite from "assets/images/logo-ct.png";
import brandDark from "assets/images/logo-ct-dark.png";
import { setupAxiosInterceptors } from "./services/interceptor";
import ProtectedRoute from "examples/ProtectedRoute";
import ForgotPassword from "auth/forgot-password";
import ResetPassword from "auth/reset-password";
import Login from "auth/login";
import Register from "auth/register";
import { AuthContext } from "context";
import UserProfile from "layouts/user-profile";
import UserManagement from "layouts/user-management";
import { MetaTags } from "react-meta-tags";
export default function App() {
const authContext = useContext(AuthContext);
const [controller, dispatch] = useMaterialUIController();
const {
miniSidenav,
direction,
layout,
openConfigurator,
sidenavColor,
transparentSidenav,
whiteSidenav,
darkMode,
} = controller;
const [onMouseEnter, setOnMouseEnter] = useState(false);
const [rtlCache, setRtlCache] = useState(null);
const { pathname } = useLocation();
// Cache for the rtl
useMemo(() => {
const cacheRtl = createCache({
key: "rtl",
stylisPlugins: [rtlPlugin],
});
setRtlCache(cacheRtl);
}, []);
// Open sidenav when mouse enter on mini sidenav
const handleOnMouseEnter = () => {
if (miniSidenav && !onMouseEnter) {
setMiniSidenav(dispatch, false);
setOnMouseEnter(true);
}
};
// Close sidenav when mouse leave mini sidenav
const handleOnMouseLeave = () => {
if (onMouseEnter) {
setMiniSidenav(dispatch, true);
setOnMouseEnter(false);
}
};
// Change the openConfigurator state
const handleConfiguratorOpen = () => setOpenConfigurator(dispatch, !openConfigurator);
// if the token expired or other errors it logs out and goes to the login page
const navigate = useNavigate();
setupAxiosInterceptors(() => {
authContext.logout();
navigate("/auth/login");
});
const [isDemo, setIsDemo] = useState(false);
useEffect(() => {
setIsDemo(process.env.REACT_APP_IS_DEMO === "true");
}, []);
// Setting the dir attribute for the body element
useEffect(() => {
document.body.setAttribute("dir", direction);
}, [direction]);
// Setting page scroll to 0 when changing the route
useEffect(() => {
document.documentElement.scrollTop = 0;
document.scrollingElement.scrollTop = 0;
}, [pathname]);
const getRoutes = (allRoutes) =>
allRoutes.map((route) => {
if (route.collapse) {
return getRoutes(route.collapse);
}
if (route.route && route.type !== "auth") {
return (
<Route
exact
path={route.route}
element={
<ProtectedRoute isAuthenticated={authContext.isAuthenticated}>
{route.component}
</ProtectedRoute>
}
key={route.key}
/>
);
}
return null;
});
const configsButton = (
<MDBox
display="flex"
justifyContent="center"
alignItems="center"
width="3.25rem"
height="3.25rem"
bgColor="white"
shadow="sm"
borderRadius="50%"
position="fixed"
right="2rem"
bottom="2rem"
zIndex={99}
color="dark"
sx={{ cursor: "pointer" }}
onClick={handleConfiguratorOpen}
>
<Icon fontSize="small" color="inherit">
settings
</Icon>
</MDBox>
);
return (
<>
{isDemo && (
<MetaTags>
<meta
name="keywords"
content="creative tim, updivision, material, laravel json:api, html dashboard, laravel, react, api admin, react laravel, html css dashboard laravel, material dashboard laravel, laravel api, react material dashboard, material admin, react dashboard, react admin, web dashboard, bootstrap 5 dashboard laravel, bootstrap 5, css3 dashboard, bootstrap 5 admin laravel, material dashboard bootstrap 5 laravel, frontend, api dashboard, responsive bootstrap 5 dashboard, api, material dashboard, material laravel bootstrap 5 dashboard, json:api"
/>
<meta
name="description"
content="A free full stack app powered by MUI component library, React and Laravel, featuring dozens of handcrafted UI elements"
/>
<meta
itemProp="name"
content="React Material Dashboard Laravel by Creative Tim & UPDIVISION"
/>
<meta
itemProp="description"
content="A free full stack app powered by MUI component library, React and Laravel, featuring dozens of handcrafted UI elements"
/>
<meta
itemProp="image"
content="https://s3.amazonaws.com/creativetim_bucket/products/686/original/react-material-dashboard-laravel.jpg?1664783836"
/>
<meta name="twitter:card" content="product" />
<meta name="twitter:site" content="@creativetim" />
<meta
name="twitter:title"
content="React Material Dashboard Laravel by Creative Tim & UPDIVISION"
/>
<meta
name="twitter:description"
content="A free full stack app powered by MUI component library, React and Laravel, featuring dozens of handcrafted UI elements"
/>
<meta name="twitter:creator" content="@creativetim" />
<meta
name="twitter:image"
content="https://s3.amazonaws.com/creativetim_bucket/products/686/original/react-material-dashboard-laravel.jpg?1664783836"
/>
<meta property="fb:app_id" content="655968634437471" />
<meta
property="og:title"
content="React Material Dashboard Laravel by Creative Tim & UPDIVISION"
/>
<meta property="og:type" content="article" />
<meta
property="og:url"
content="https://www.creative-tim.com/live/material-dashboard-react-laravel/"
/>
<meta
property="og:image"
content="https://s3.amazonaws.com/creativetim_bucket/products/686/original/react-material-dashboard-laravel.jpg?1664783836"
/>
<meta
property="og:description"
content="A free full stack app powered by MUI component library, React and Laravel, featuring dozens of handcrafted UI elements"
/>
<meta property="og:site_name" content="Creative Tim" />
</MetaTags>
)}
{direction === "rtl" ? (
<CacheProvider value={rtlCache}>
<ThemeProvider theme={darkMode ? themeDarkRTL : themeRTL}>
<CssBaseline />
{layout === "dashboard" && (
<>
<Sidenav
color={sidenavColor}
brand={(transparentSidenav && !darkMode) || whiteSidenav ? brandDark : brandWhite}
brandName="Material Dashboard 2"
routes={routes}
onMouseEnter={handleOnMouseEnter}
onMouseLeave={handleOnMouseLeave}
/>
<Configurator />
{configsButton}
</>
)}
{layout === "vr" && <Configurator />}
<Routes>
<Route path="login" element={<Navigate to="/auth/login" />} />
<Route path="register" element={<Navigate to="/auth/register" />} />
<Route path="forgot-password" element={<Navigate to="/auth/forgot-password" />} />
{getRoutes(routes)}
<Route path="*" element={<Navigate to="/dashboard" />} />
</Routes>
</ThemeProvider>
</CacheProvider>
) : (
<ThemeProvider theme={darkMode ? themeDark : theme}>
<CssBaseline />
{layout === "dashboard" && (
<>
<Sidenav
color={sidenavColor}
brand={(transparentSidenav && !darkMode) || whiteSidenav ? brandDark : brandWhite}
brandName="Material Dashboard 2"
routes={routes}
onMouseEnter={handleOnMouseEnter}
onMouseLeave={handleOnMouseLeave}
/>
<Configurator />
{configsButton}
</>
)}
{layout === "vr" && <Configurator />}
<Routes>
<Route path="/auth/login" element={<Login />} />
<Route path="/auth/register" element={<Register />} />
<Route path="/auth/forgot-password" element={<ForgotPassword />} />
<Route path="/auth/reset-password" element={<ResetPassword />} />
<Route
exact
path="user-profile"
element={
<ProtectedRoute isAuthenticated={authContext.isAuthenticated}>
<UserProfile />
</ProtectedRoute>
}
key="user-profile"
/>
<Route
exact
path="user-management"
element={
<ProtectedRoute isAuthenticated={authContext.isAuthenticated}>
<UserManagement />
</ProtectedRoute>
}
key="user-management"
/>
{getRoutes(routes)}
<Route path="*" element={<Navigate to="/dashboard" />} />
</Routes>
</ThemeProvider>
)}
</>
);
}