-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.tsx
More file actions
168 lines (156 loc) · 5.54 KB
/
Copy pathapp.tsx
File metadata and controls
168 lines (156 loc) · 5.54 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
import { Routes, Route, Navigate, useLocation, useSearchParams } from 'react-router-dom';
import { useState, useEffect } from 'react';
import { useAuth } from '../hooks/useAuth';
import NavBar from '../components/nav/Nav';
import Home from '../pages/Home';
import CourseView from "../pages/CourseView";
import Read from '../pages/Read';
import Create from '../pages/Create';
import NotFound from '../components/notfound/NotFound';
import { ExerciseView } from '../pages/create/exercise';
import ModuleForm from '../pages/ModuleForm';
import ResourceForm from '../pages/ResourceForm';
import CourseForm from '../pages/CourseForm';
import DemoForms from '../pages/DemoForm';
import UserManagement from '../pages/UserManagement';
import { ErrorPage } from '../components/error/Error';
import LoadingPage from '../components/loading/LoadingPage';
import TeacherView from '../pages/course/TeacherView';
import DashboardGate from '../components/guards/DashboardGate';
import axios from 'axios';
import { buildGoogleLoginHref } from '../utils/auth';
const TOP_LEVEL = new Set(['', 'create', 'usermanagement', 'read', 'settings', 'login', 'error']);
export function App() {
const { user, loading, handleLogout } = useAuth();
const location = useLocation();
const [searchParams] = useSearchParams();
const isLandingPage = location.pathname === '/';
const isUserManagement = location.pathname.startsWith('/usermanagement');
const loginHref = !user
? buildGoogleLoginHref(`${location.pathname}${location.search}${location.hash}`)
: undefined;
const [courseTitle, setCourseTitle] = useState<string>(() =>
localStorage.getItem('courseTitle') || '',
);
const [courseId, setCourseId] = useState<string>(() =>
localStorage.getItem('courseId') || '',
);
useEffect(() => {
if (courseTitle) {
localStorage.setItem('courseTitle', courseTitle);
}
if (courseId) {
localStorage.setItem('courseId', courseId);
}
}, [courseTitle, courseId]);
// Get course from /api/courses/landing
useEffect(() => {
if (user?.isAdmin && !courseId) {
axios.get('/api/courses/landing')
.then(({ data }) => {
const first = data?.instructor?.[0];
if (first?.id && first?.title) {
setCourseId(first.id);
setCourseTitle(first.title);
}
})
.catch(() => {});
}
}, [user?.isAdmin, courseId]);
// Derive courseId from the current URL for students (and /read)
useEffect(() => {
const segs = location.pathname.split('/').filter(Boolean);
// "/:courseId"
if (segs.length === 1 && !TOP_LEVEL.has(segs[0])) {
if (segs[0] !== courseId) setCourseId(segs[0]);
return;
}
// "/:courseId/dashboard"
if (segs.length === 2 && segs[1] === 'dashboard' && !TOP_LEVEL.has(segs[0])) {
if (segs[0] !== courseId) setCourseId(segs[0]);
return;
}
// read page: ?course_id=...
const qp = searchParams.get('course_id');
if (qp && qp !== courseId) setCourseId(qp);
}, [location.pathname, location.search, courseId]);
if (loading) {
return <LoadingPage />;
}
return (
<div className="w-screen h-screen flex flex-col flex-nowrap bg-background-500">
<NavBar
name={user ? user.username : ''}
admin={user ? user.isAdmin : false}
handleLogout={handleLogout}
courseTitle={isLandingPage ? '' : courseTitle}
loginHref={loginHref}
/>
<div className="relative flex-1 overflow-auto">
<Routes>
<Route
path="/"
element={<Home user={user} />}
/>
<Route
path="/:course_id"
element={<CourseView onCourseChange={setCourseTitle} />}
/>
<Route path="/read/:uid" element={<Read />} />
<Route
path="/usermanagement"
element={
user && user.isAdmin ? (
<UserManagement />
) : (
<Navigate
to="/error"
state={{
message:
'Oops! You do not have permission to access this page.\nPress the button below to return to the main page.',
statusCode: 401,
}}
/>
)
}
/>
<Route
path="/create"
element={
user && user.isAdmin ? (
<Create />
) : (
<Navigate
to="/error"
state={{
message:
'Oops! You do not have permission to access this page.\nPress the button below to return to the main page.',
statusCode: 401,
}}
/>
)
}
>
<Route index element={<Navigate to="/create/demo"/>}/>
<Route path="demo" element={<DemoForms />} />
<Route path="exercise/*" element={<ExerciseView/>} />
<Route path="module" element={<ModuleForm />} />
<Route path="resource" element={<ResourceForm />} />
<Route path="course" element={<CourseForm />} />
</Route>
<Route
path="/:courseId/dashboard"
element={
<DashboardGate>
<TeacherView onCourseChange={setCourseTitle} />
</DashboardGate>
}
/>
<Route path="/error" element={<ErrorPage />} />
<Route path="*" element={<NotFound />} />
</Routes>
</div>
</div>
);
}
export default App;