- Adapted more of the design to be more responsive;

- Revamped some of the UserSolutions;
- Transformed the homepage chart to use actual dynamic values;
- Created an endpoint for the stats;
This commit is contained in:
Tiago Ribeiro
2023-04-20 00:20:20 +01:00
parent d61592b73e
commit 87f7b717fc
22 changed files with 5468 additions and 829 deletions

View File

@@ -14,7 +14,7 @@ import Finish from "@/exams/Finish";
import axios from "axios";
import {withIronSessionSsr} from "iron-session/next";
import {sessionOptions} from "@/lib/session";
import {User} from "@/interfaces/user";
import {Stat, User} from "@/interfaces/user";
import Speaking from "@/exams/Speaking";
export const getServerSideProps = withIronSessionSsr(({req, res}) => {
@@ -37,11 +37,12 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => {
}, sessionOptions);
export default function Page({user}: {user: User}) {
const [userSolutions, setUserSolutions] = useState<UserSolution[]>([]);
const [selectedModules, setSelectedModules] = useState<Module[]>([]);
const [hasBeenUploaded, setHasBeenUploaded] = useState(false);
const [showSolutions, setShowSolutions] = useState(false);
const [moduleIndex, setModuleIndex] = useState(0);
const [exam, setExam] = useState<Exam>();
const [userSolutions, setUserSolutions] = useState<UserSolution[]>([]);
const [showSolutions, setShowSolutions] = useState(false);
const [timer, setTimer] = useState(-1);
useEffect(() => {
@@ -54,6 +55,25 @@ export default function Page({user}: {user: User}) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedModules, moduleIndex]);
useEffect(() => {
(async () => {
if (selectedModules.length > 0 && moduleIndex >= selectedModules.length && !hasBeenUploaded) {
const stats: Stat[] = userSolutions.map((solution) => ({
...solution,
exam: solution.exam!,
module: solution.module!,
user: user.id,
}));
axios
.post<{ok: boolean}>("/api/stats", stats)
.then((response) => setHasBeenUploaded(response.data.ok))
.catch(() => setHasBeenUploaded(false));
}
})();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedModules, moduleIndex, hasBeenUploaded]);
useEffect(() => {
if (exam) {
setTimer(exam.minTimer * 60);
@@ -87,15 +107,15 @@ export default function Page({user}: {user: User}) {
};
const updateExamWithUserSolutions = (exam: Exam): Exam => {
const exercises = exam.exercises.map((x) => Object.assign(x, {userSolutions: userSolutions.find((y) => x.id === y.id)?.solutions}));
const exercises = exam.exercises.map((x) => Object.assign(x, {userSolutions: userSolutions.find((y) => x.id === y.exercise)?.solutions}));
return Object.assign(exam, exercises);
};
const onFinish = (solutions: UserSolution[]) => {
const solutionIds = solutions.map((x) => x.id);
const solutionIds = solutions.map((x) => x.exercise);
setUserSolutions((prev) => [...prev.filter((x) => !solutionIds.includes(x.id)), ...solutions]);
setUserSolutions((prev) => [...prev.filter((x) => !solutionIds.includes(x.exercise)), ...solutions]);
setModuleIndex((prev) => prev + 1);
};