-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathindex.js
190 lines (159 loc) · 5.42 KB
/
index.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
/**
=========================================================
* Material Dashboard 2 React - v2.1.0
=========================================================
* Product Page: https://www.creative-tim.com/product/material-dashboard-react
* Copyright 2022 Creative Tim (https://www.creative-tim.com)
Coded by www.creative-tim.com
=========================================================
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
/**
This file is used for controlling the global states of the components,
you can customize the states for the different components here.
*/
import { createContext, useContext, useReducer, useMemo, useState, useEffect } from "react";
// prop-types is a library for typechecking of props
import PropTypes from "prop-types";
import { useLocation, useNavigate } from "react-router-dom";
// Material Dashboard 2 React main context
const MaterialUI = createContext();
// authentication context
export const AuthContext = createContext({
isAuthenticated: false,
login: () => {},
register: () => {},
logout: () => {},
});
const AuthContextProvider = ({ children }) => {
const [isAuthenticated, setIsAuthenticated] = useState(false);
const navigate = useNavigate();
const location = useLocation();
const token = localStorage.getItem("token");
useEffect(() => {
if (!token) return;
setIsAuthenticated(true);
navigate(location.pathname);
}, []);
useEffect(() => {
if (!token) {
navigate("/auth/login");
return;
}
setIsAuthenticated(isAuthenticated);
navigate(location.pathname);
}, [isAuthenticated]);
const login = (token) => {
localStorage.setItem("token", token);
setIsAuthenticated(true);
navigate("/dashboard");
};
const logout = () => {
localStorage.removeItem("token");
setIsAuthenticated(false);
navigate("/auth/login");
};
return (
<AuthContext.Provider value={{ isAuthenticated, login, logout }}>
{children}
</AuthContext.Provider>
);
};
// Setting custom name for the context which is visible on react dev tools
MaterialUI.displayName = "MaterialUIContext";
// Material Dashboard 2 React reducer
function reducer(state, action) {
switch (action.type) {
case "MINI_SIDENAV": {
return { ...state, miniSidenav: action.value };
}
case "TRANSPARENT_SIDENAV": {
return { ...state, transparentSidenav: action.value };
}
case "WHITE_SIDENAV": {
return { ...state, whiteSidenav: action.value };
}
case "SIDENAV_COLOR": {
return { ...state, sidenavColor: action.value };
}
case "TRANSPARENT_NAVBAR": {
return { ...state, transparentNavbar: action.value };
}
case "FIXED_NAVBAR": {
return { ...state, fixedNavbar: action.value };
}
case "OPEN_CONFIGURATOR": {
return { ...state, openConfigurator: action.value };
}
case "DIRECTION": {
return { ...state, direction: action.value };
}
case "LAYOUT": {
return { ...state, layout: action.value };
}
case "DARKMODE": {
return { ...state, darkMode: action.value };
}
default: {
throw new Error(`Unhandled action type: ${action.type}`);
}
}
}
// Material Dashboard 2 React context provider
function MaterialUIControllerProvider({ children }) {
const initialState = {
miniSidenav: false,
transparentSidenav: false,
whiteSidenav: false,
sidenavColor: "info",
transparentNavbar: true,
fixedNavbar: true,
openConfigurator: false,
direction: "ltr",
layout: "dashboard",
darkMode: false,
};
const [controller, dispatch] = useReducer(reducer, initialState);
const value = useMemo(() => [controller, dispatch], [controller, dispatch]);
return <MaterialUI.Provider value={value}>{children}</MaterialUI.Provider>;
}
// Material Dashboard 2 React custom hook for using context
function useMaterialUIController() {
const context = useContext(MaterialUI);
if (!context) {
throw new Error(
"useMaterialUIController should be used inside the MaterialUIControllerProvider."
);
}
return context;
}
// Typechecking props for the MaterialUIControllerProvider
MaterialUIControllerProvider.propTypes = {
children: PropTypes.node.isRequired,
};
// Context module functions
const setMiniSidenav = (dispatch, value) => dispatch({ type: "MINI_SIDENAV", value });
const setTransparentSidenav = (dispatch, value) => dispatch({ type: "TRANSPARENT_SIDENAV", value });
const setWhiteSidenav = (dispatch, value) => dispatch({ type: "WHITE_SIDENAV", value });
const setSidenavColor = (dispatch, value) => dispatch({ type: "SIDENAV_COLOR", value });
const setTransparentNavbar = (dispatch, value) => dispatch({ type: "TRANSPARENT_NAVBAR", value });
const setFixedNavbar = (dispatch, value) => dispatch({ type: "FIXED_NAVBAR", value });
const setOpenConfigurator = (dispatch, value) => dispatch({ type: "OPEN_CONFIGURATOR", value });
const setDirection = (dispatch, value) => dispatch({ type: "DIRECTION", value });
const setLayout = (dispatch, value) => dispatch({ type: "LAYOUT", value });
const setDarkMode = (dispatch, value) => dispatch({ type: "DARKMODE", value });
export {
AuthContextProvider,
MaterialUIControllerProvider,
useMaterialUIController,
setMiniSidenav,
setTransparentSidenav,
setWhiteSidenav,
setSidenavColor,
setTransparentNavbar,
setFixedNavbar,
setOpenConfigurator,
setDirection,
setLayout,
setDarkMode,
};