Created a system to go directly to an assignment from a URL
This commit is contained in:
@@ -6,15 +6,51 @@ import {shouldRedirectHome} from "@/utils/navigation.disabled";
|
||||
import ExamPage from "./(exam)/ExamPage";
|
||||
import Head from "next/head";
|
||||
import {User} from "@/interfaces/user";
|
||||
import { redirect, serialize } from "@/utils";
|
||||
import { filterBy, findBy, redirect, serialize } from "@/utils";
|
||||
import { requestUser } from "@/utils/api";
|
||||
import { getAssignment, getAssignments, getAssignmentsByAssignee } from "@/utils/assignments.be";
|
||||
import { Assignment } from "@/interfaces/results";
|
||||
import useExamStore from "@/stores/examStore";
|
||||
import { useEffect } from "react";
|
||||
import { Exam } from "@/interfaces/exam";
|
||||
import { getExamsByIds } from "@/utils/exams.be";
|
||||
import { sortByModule } from "@/utils/moduleUtils";
|
||||
import { uniqBy } from "lodash";
|
||||
import { useRouter } from "next/router";
|
||||
import { getSessionByAssignment, getSessionsByUser } from "@/utils/sessions.be";
|
||||
import { Session } from "@/hooks/useSessions";
|
||||
import moment from "moment";
|
||||
|
||||
export const getServerSideProps = withIronSessionSsr(async ({req, res}) => {
|
||||
export const getServerSideProps = withIronSessionSsr(async ({req, res, query}) => {
|
||||
const user = await requestUser(req, res)
|
||||
if (!user) return redirect("/login")
|
||||
const destination = Buffer.from(req.url || "/").toString("base64")
|
||||
if (!user) return redirect(`/login?destination=${destination}`)
|
||||
|
||||
if (shouldRedirectHome(user)) return redirect("/")
|
||||
|
||||
const {assignment: assignmentID} = query as {assignment?: string}
|
||||
|
||||
if (assignmentID) {
|
||||
const assignment = await getAssignment(assignmentID)
|
||||
|
||||
if (!assignment) return redirect("/exercises")
|
||||
if (!["admin", "developer"].includes(user.type) && !assignment.assignees.includes(user.id)) return redirect("/exercises")
|
||||
|
||||
const exams = await getExamsByIds(uniqBy(assignment.exams, "id"))
|
||||
const session = await getSessionByAssignment(assignmentID)
|
||||
|
||||
if (
|
||||
filterBy(assignment.results, 'user', user.id) ||
|
||||
moment(assignment.startDate).isBefore(moment()) ||
|
||||
moment(assignment.endDate).isAfter(moment())
|
||||
)
|
||||
return redirect("/exercises")
|
||||
|
||||
return {
|
||||
props: serialize({user, assignment, exams, session})
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
props: serialize({user}),
|
||||
};
|
||||
@@ -22,13 +58,59 @@ export const getServerSideProps = withIronSessionSsr(async ({req, res}) => {
|
||||
|
||||
interface Props {
|
||||
user: User;
|
||||
assignment?: Assignment
|
||||
exams?: Exam[]
|
||||
session?: Session
|
||||
}
|
||||
|
||||
export default function Page({user}: Props) {
|
||||
export default function Page({user, assignment, exams = [], session}: Props) {
|
||||
const router = useRouter()
|
||||
|
||||
const state = useExamStore((state) => state)
|
||||
|
||||
useEffect(() => {
|
||||
if (assignment && exams.length > 0 && !state.assignment && !session) {
|
||||
state.setUserSolutions([]);
|
||||
state.setShowSolutions(false);
|
||||
state.setAssignment(assignment);
|
||||
state.setExams(exams.sort(sortByModule));
|
||||
state.setSelectedModules(
|
||||
exams
|
||||
.map((x) => x!)
|
||||
.sort(sortByModule)
|
||||
.map((x) => x!.module),
|
||||
);
|
||||
|
||||
router.replace(router.asPath)
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [assignment, exams, session])
|
||||
|
||||
useEffect(() => {
|
||||
if (assignment && exams.length > 0 && !state.assignment && !!session) {
|
||||
state.setShuffles(session.userSolutions.map((x) => ({exerciseID: x.exercise, shuffles: x.shuffleMaps ? x.shuffleMaps : []})));
|
||||
state.setSelectedModules(session.selectedModules);
|
||||
state.setExam(session.exam);
|
||||
state.setExams(session.exams);
|
||||
state.setSessionId(session.sessionId);
|
||||
state.setAssignment(session.assignment);
|
||||
state.setExerciseIndex(session.exerciseIndex);
|
||||
state.setPartIndex(session.partIndex);
|
||||
state.setModuleIndex(session.moduleIndex);
|
||||
state.setTimeSpent(session.timeSpent);
|
||||
state.setUserSolutions(session.userSolutions);
|
||||
state.setShowSolutions(false);
|
||||
state.setQuestionIndex(session.questionIndex);
|
||||
|
||||
router.replace(router.asPath)
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [assignment, exams, session])
|
||||
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
<title>Exercises | EnCoach</title>
|
||||
<title>Exams | EnCoach</title>
|
||||
<meta
|
||||
name="description"
|
||||
content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."
|
||||
@@ -36,7 +118,7 @@ export default function Page({user}: Props) {
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<link rel="icon" href="/favicon.ico" />
|
||||
</Head>
|
||||
<ExamPage page="exercises" user={user} />
|
||||
<ExamPage page="exams" user={user} hideSidebar={!!assignment} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user