-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathApp.jsx
More file actions
331 lines (313 loc) · 8.66 KB
/
Copy pathApp.jsx
File metadata and controls
331 lines (313 loc) · 8.66 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import { useEffect, useContext } from "react";
import CssBaseline from "@mui/material/CssBaseline";
import { Box, ThemeProvider, CircularProgress } from "@mui/material";
import {
BrowserRouter as Router,
Routes,
Route,
Navigate,
useLocation,
useNavigate
} from "react-router-dom";
import theme from "./theme";
import PropTypes from "prop-types";
import Header from "./components/Header";
import Footer from "./components/Footer";
import Login from "./components/Auth/Login";
import HomePage from "./components/HomePage";
import Dashboard from "./components/Dashboard";
import Register from "./components/Auth/Register";
import CurieEditor from "./components/CurieEditor";
import SearchResults from "./components/SearchResults";
import Organizations from "./components/organizations";
import SingleTermView from "./components/SingleTermView";
import PullRequestView from "./components/PullRequest";
import OntologyPage from "./components/CellCards/OntologyPage";
import OntologyGridPage from "./components/CellCards/OntologyGridPage";
import OntologyBrowsePage from "./components/CellCards/OntologyBrowsePage";
import { GlobalDataProvider } from "./contexts/DataContext";
import ResetPassword from "./components/Auth/ResetPassword";
import ForgotPassword from "./components/Auth/ForgotPassword";
import SingleOrganization from "./components/SingleOrganization";
import TermActivity from "./components/term_activity/TermActivity";
import Documentation from "./components/documentation/Documentation";
import OrganizationsCurieEditor from "./components/CurieEditor/OrganizationCurieEditor";
import { handleOrcidLogin } from "./api/endpoints";
import { GlobalDataContext } from "./contexts/DataContext";
import { API_CONFIG } from "./config";
import { requestUserSettings } from "./components/Auth/utils";
import { useCookies } from 'react-cookie';
const PageContainer = ({ children }) => {
return (
<Box sx={{ display: "flex", height: "calc(100vh - 7.5rem)" }}>
{children}
</Box>
);
};
const ProtectedRoute = ({ children }) => {
const { user } = useContext(GlobalDataContext);
const navigate = useNavigate();
const location = useLocation();
useEffect(() => {
if (!user) {
navigate('/login', { state: { from: location.pathname + location.search } });
}
}, [user, navigate, location]);
return user ? children : null;
};
function MainContent() {
const { setUserData, loading } = useContext(GlobalDataContext);
const navigate = useNavigate();
const location = useLocation();
// eslint-disable-next-line no-unused-vars
const [existingCookies, setCookie, removeCookie] = useCookies(['session']);
useEffect(() => {
(async () => {
const userSettings = JSON.parse(localStorage.getItem(API_CONFIG.SESSION_DATA.SETTINGS));
if (userSettings) {
try {
const userData = await requestUserSettings(userSettings?.groupname);
setUserData({
name: userData['groupname'],
id: userData['orcid'],
email: userData?.emails[0]?.email,
role: userData['own-role'],
groupname: userData['groupname'],
settings: userData
});
// Only redirect to home if user is currently on login/register pages
if (location.pathname === '/login' || location.pathname === '/register') {
navigate("/");
}
} catch (error) {
console.error("Error fetching user settings:", error);
localStorage.removeItem(API_CONFIG.SESSION_DATA.SETTINGS);
localStorage.removeItem(API_CONFIG.SESSION_DATA.COOKIE);
removeCookie('session', { path: '/' });
// setErrors((prev) => ({
// ...prev,
// auth: "Session expired. Please log in again.",
// }));
}
}
})();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [location.pathname]);
if (loading) {
return (
<div
style={{
display: "flex",
alignItems: "center",
justifyContent: "center",
width: "100%",
height: "100vh"
}}
>
<CircularProgress />
</div>
)
}
return (
<Box
sx={{
display: "flex",
flexDirection: "column",
minHeight: "100vh",
}}
>
<Layout>
<Routes>
<Route
path="/"
element={
<Box sx={{ flex: 1 }}>
<HomePage />
</Box>
}
/>
<Route
path="/:group/search"
element={
<PageContainer>
<SearchResults />
</PageContainer>
}
/>
<Route
path="/organizations"
element={
<ProtectedRoute>
<PageContainer>
<Organizations />
</PageContainer>
</ProtectedRoute>
}
/>
<Route
path="/curie-editor"
element={
<ProtectedRoute>
<PageContainer>
<CurieEditor />
</PageContainer>
</ProtectedRoute>
}
/>
<Route
path="/term-activity"
element={
<PageContainer>
<TermActivity />
</PageContainer>
}
/>
<Route
path="/documentation"
element={
<PageContainer>
<Documentation />
</PageContainer>
}
/>
<Route
path="/:group/dashboard"
element={
<PageContainer>
<Dashboard />
</PageContainer>
}
/>
<Route
path="/:org/ontology/:slug"
element={
<PageContainer>
<OntologyPage />
</PageContainer>
}
>
<Route index element={<OntologyGridPage />} />
<Route path="browse" element={<OntologyBrowsePage />} />
</Route>
{/* A term read inside an ontology: the same page as the plain term route below, with
the context ontology named by the path. A sibling of the ontology route rather than a
child of it — the term page brings its own header, and the layout route above would
render the ontology's on top of it. */}
<Route
path="/:group/ontology/:ontologySlug/:term/:tab?"
element={
<PageContainer>
<SingleTermView />
</PageContainer>
}
/>
<Route path="/login" element={<Login />} />
<Route path="/register" element={<Register />} />
<Route path="/forgot" element={<ForgotPassword />} />
<Route path="/reset" element={<ResetPassword />} />
<Route
path="/:title"
element={
<ProtectedRoute>
<PageContainer>
<SingleOrganization />
</PageContainer>
</ProtectedRoute>
}
/>
<Route
path="/:title/curie-editor"
element={
<ProtectedRoute>
<PageContainer>
<OrganizationsCurieEditor />
</PageContainer>
</ProtectedRoute>
}
/>
{/* /<group>/pulls/<id> is the backend's own (proxied) address for the
record, so the in-app view lives alongside it at /pull-requests/. */}
<Route
path="/:group/pull-requests/:pullId"
element={
<PageContainer>
<PullRequestView />
</PageContainer>
}
/>
<Route
path="/:group/:term/versions/:versionHash"
element={
<PageContainer>
<SingleTermView />
</PageContainer>
}
/>
<Route
path="/:group/:term/:tab?"
element={
<PageContainer>
<SingleTermView />
</PageContainer>
}
/>
<Route path="*" element={<Navigate to="/" replace />} />
</Routes>
</Layout>
</Box>
);
}
const Layout = ({ children }) => {
const authPaths = ["/login", "/register", "/forgot", "/reset"];
const location = useLocation();
const isAuthPath = authPaths.includes(location.pathname);
useEffect(() => {
const params = new URLSearchParams(location.search);
const code = params.get("code");
if (code) {
(async () => {
try {
const response = await handleOrcidLogin(code);
localStorage.setItem("token", response.token);
} catch (error) {
console.log("error: ", error);
}
})();
}
}, [location]);
return (
<Box
sx={{
display: "flex",
flexDirection: "column",
minHeight: "100vh",
}}
>
{!isAuthPath && <Header />}
{children}
{!isAuthPath && <Footer />}
</Box>
);
};
function App() {
return (
<ThemeProvider theme={theme}>
<CssBaseline />
<GlobalDataProvider>
<Router>
<MainContent />
</Router>
</GlobalDataProvider>
</ThemeProvider>
);
}
PageContainer.propTypes = {
children: PropTypes.node.isRequired,
};
ProtectedRoute.propTypes = {
children: PropTypes.node.isRequired,
};
Layout.propTypes = {
children: PropTypes.node.isRequired,
};
export default App;