Skip to content

Commit 2b08fac

Browse files
committed
Merge branch 'main' of https://github.com/alstondsouza1/Running-start-digital-support into feature/faq-search
2 parents 6c2a4c2 + a930839 commit 2b08fac

File tree

1 file changed

+92
-20
lines changed

1 file changed

+92
-20
lines changed

frontend/src/pages/Admin.jsx

Lines changed: 92 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
import { useState } from "react";
2-
import {
3-
Box,
4-
Button,
5-
TextField,
6-
Typography,
7-
Paper,
8-
} from "@mui/material";
1+
import { useMemo, useState } from "react";
2+
import { Box, Button, TextField, Typography, Paper } from "@mui/material";
3+
4+
// partner data (unchanged)
5+
import { currentStudentsQuestions } from "../data/currentStudent";
6+
import { prospectiveStudentsQuestions } from "../data/prospectiveStudent";
7+
8+
// category config (unchanged)
9+
import { categorySets } from "../data/categories";
10+
11+
// adapter (your file)
12+
import { adaptQuestions } from "../data/flexQuestions";
13+
14+
function groupByType(questions) {
15+
return questions.reduce((acc, q) => {
16+
const type = q.type;
17+
if (!acc[type]) acc[type] = [];
18+
acc[type].push(q);
19+
return acc;
20+
}, {});
21+
}
922

1023
// partner data (unchanged)
1124
import { currentStudentsQuestions } from "../data/currentStudent";
@@ -29,22 +42,46 @@ export default function Admin() {
2942
const [username, setUsername] = useState("");
3043
const [password, setPassword] = useState("");
3144
const [error, setError] = useState("");
32-
const [isLoggedIn, setIsLoggedIn] = useState(() => {
33-
return localStorage.getItem("adminLoggedIn") === "true";
34-
});
3545

46+
const [isLoggedIn, setIsLoggedIn] = useState(() => {
47+
return localStorage.getItem("adminLoggedIn") === "true";
48+
});
3649

3750
function handleSubmit(e) {
3851
e.preventDefault();
3952

53+
// replace with real auth later
4054
if (username === "admin" && password === "1234") {
4155
setIsLoggedIn(true);
56+
localStorage.setItem("adminLoggedIn", "true");
4257
setError("");
58+
setPassword(""); // optional: clear after login
4359
} else {
4460
setError("Invalid username or password");
4561
}
4662
}
4763

64+
function handleLogout() {
65+
setIsLoggedIn(false);
66+
localStorage.removeItem("adminLoggedIn");
67+
setUsername("");
68+
setPassword("");
69+
setError("");
70+
}
71+
72+
const { groupedCurrent, groupedProspective } = useMemo(() => {
73+
const adminCurrentQuestions = adaptQuestions(currentStudentsQuestions, "current");
74+
const adminProspectiveQuestions = adaptQuestions(
75+
prospectiveStudentsQuestions,
76+
"prospective"
77+
);
78+
79+
return {
80+
groupedCurrent: groupByType(adminCurrentQuestions),
81+
groupedProspective: groupByType(adminProspectiveQuestions),
82+
};
83+
}, []);
84+
4885
// =========================
4986
// ADMIN DASHBOARD
5087
// =========================
@@ -64,9 +101,28 @@ export default function Admin() {
64101

65102
return (
66103
<Box sx={{ p: 3 }}>
67-
<Typography variant="h4" gutterBottom>
68-
Admin Dashboard
69-
</Typography>
104+
<Box
105+
sx={{
106+
display: "flex",
107+
alignItems: "center",
108+
justifyContent: "space-between",
109+
gap: 2,
110+
mb: 2,
111+
}}
112+
>
113+
<Typography variant="h4">Admin Dashboard</Typography>
114+
115+
<Button
116+
variant="contained"
117+
onClick={handleLogout}
118+
sx={{
119+
backgroundColor: "#006225",
120+
"&:hover": { backgroundColor: "#D14900" },
121+
}}
122+
>
123+
Logout
124+
</Button>
125+
</Box>
70126

71127
{/* ================= CURRENT STUDENTS ================= */}
72128
<Typography variant="h5" sx={{ mt: 3 }}>
@@ -80,10 +136,19 @@ export default function Admin() {
80136
</Typography>
81137

82138
{(groupedCurrent[cat.id] || []).map((q) => (
83-
<Paper key={q.id} sx={{ p: 2, mt: 1 }}>
139+
<Paper
140+
key={q.id ?? `${cat.id}-${q.question}`}
141+
sx={{ p: 2, mt: 1 }}
142+
>
84143
{q.question}
85144
</Paper>
86145
))}
146+
147+
{(groupedCurrent[cat.id] || []).length === 0 && (
148+
<Typography color="text.secondary" sx={{ mt: 1 }}>
149+
No questions mapped to this category yet.
150+
</Typography>
151+
)}
87152
</Box>
88153
))}
89154

@@ -99,10 +164,19 @@ export default function Admin() {
99164
</Typography>
100165

101166
{(groupedProspective[cat.id] || []).map((q) => (
102-
<Paper key={q.id} sx={{ p: 2, mt: 1 }}>
167+
<Paper
168+
key={q.id ?? `${cat.id}-${q.question}`}
169+
sx={{ p: 2, mt: 1 }}
170+
>
103171
{q.question}
104172
</Paper>
105173
))}
174+
175+
{(groupedProspective[cat.id] || []).length === 0 && (
176+
<Typography color="text.secondary" sx={{ mt: 1 }}>
177+
No questions mapped to this category yet.
178+
</Typography>
179+
)}
106180
</Box>
107181
))}
108182
</Box>
@@ -165,9 +239,7 @@ export default function Admin() {
165239
sx={{
166240
mt: 2,
167241
backgroundColor: "#006225",
168-
"&:hover": {
169-
backgroundColor: "#D14900",
170-
},
242+
"&:hover": { backgroundColor: "#D14900" },
171243
}}
172244
>
173245
Login
@@ -176,4 +248,4 @@ export default function Admin() {
176248
</Paper>
177249
</Box>
178250
);
179-
}
251+
}

0 commit comments

Comments
 (0)