- Created a Diagnostics component;
- Corrected the history code;
This commit is contained in:
26
src/pages/api/users/update.ts
Normal file
26
src/pages/api/users/update.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type {NextApiRequest, NextApiResponse} from "next";
|
||||
import {app} from "@/firebase";
|
||||
import {getFirestore, collection, getDocs, getDoc, doc, setDoc} from "firebase/firestore";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {User} from "@/interfaces/user";
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
const docUser = await getDoc(doc(db, "users", req.session.user.id));
|
||||
const user = docUser.data() as User;
|
||||
|
||||
const userRef = doc(db, "users", user.id);
|
||||
setDoc(userRef, req.body, {merge: true});
|
||||
|
||||
res.status(200).json({ok: true});
|
||||
}
|
||||
@@ -24,6 +24,8 @@ import {toast} from "react-toastify";
|
||||
import {useRouter} from "next/router";
|
||||
import Icon from "@mdi/react";
|
||||
import {mdiArrowRight, mdiChevronRight} from "@mdi/js";
|
||||
import {uniqBy} from "lodash";
|
||||
import {getExamById} from "@/utils/exams";
|
||||
|
||||
export const getServerSideProps = withIronSessionSsr(({req, res}) => {
|
||||
const user = req.session.user;
|
||||
@@ -64,27 +66,6 @@ export default function History({user}: {user: User}) {
|
||||
}
|
||||
}, [stats, isStatsLoading]);
|
||||
|
||||
const getExam = async (module: Module): Promise<Exam | undefined> => {
|
||||
const examRequest = await axios<Exam[]>(`/api/exam/${module}`);
|
||||
if (examRequest.status !== 200) {
|
||||
toast.error("Something went wrong!");
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const newExam = examRequest.data;
|
||||
|
||||
switch (module) {
|
||||
case "reading":
|
||||
return newExam.shift() as ReadingExam;
|
||||
case "listening":
|
||||
return newExam.shift() as ListeningExam;
|
||||
case "writing":
|
||||
return newExam.shift() as WritingExam;
|
||||
case "speaking":
|
||||
return newExam.shift() as SpeakingExam;
|
||||
}
|
||||
};
|
||||
|
||||
const formatTimestamp = (timestamp: string) => {
|
||||
const date = moment(parseInt(timestamp));
|
||||
const formatter = "YYYY/MM/DD - HH:mm";
|
||||
@@ -100,9 +81,7 @@ export default function History({user}: {user: User}) {
|
||||
const total = dateStats.reduce((accumulator, current) => accumulator + current.score.total, 0);
|
||||
|
||||
const selectExam = () => {
|
||||
const examPromises = formatModuleTotalStats(dateStats)
|
||||
.filter((x) => x.value > 0)
|
||||
.map((module) => getExam(module.label.toLowerCase() as Module));
|
||||
const examPromises = uniqBy(dateStats, "exam").map((stat) => getExamById(stat.module, stat.exam));
|
||||
|
||||
Promise.all(examPromises).then((exams) => {
|
||||
if (exams.every((x) => !!x)) {
|
||||
|
||||
@@ -11,6 +11,8 @@ import useStats from "@/hooks/useStats";
|
||||
import {averageScore, formatModuleTotalStats, totalExams} from "@/utils/stats";
|
||||
import {Divider} from "primereact/divider";
|
||||
import useUser from "@/hooks/useUser";
|
||||
import Diagnostic from "@/components/Diagnostic";
|
||||
import {ToastContainer} from "react-toastify";
|
||||
|
||||
export const getServerSideProps = withIronSessionSsr(({req, res}) => {
|
||||
const user = req.session.user;
|
||||
@@ -41,6 +43,25 @@ export default function Home() {
|
||||
useEffect(() => setShowEndExam(window.innerWidth <= 960), []);
|
||||
useEffect(() => setWindowWidth(window.innerWidth), []);
|
||||
|
||||
if (user && user.isFirstLogin) {
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
<title>IELTS GPT | Muscat Training Institute</title>
|
||||
<meta
|
||||
name="description"
|
||||
content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."
|
||||
/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<link rel="icon" href="/favicon.ico" />
|
||||
</Head>
|
||||
<main className="w-full h-full min-h-[100vh] flex flex-col items-center justify-center bg-neutral-100 text-black">
|
||||
<Diagnostic user={user} />
|
||||
</main>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
@@ -52,6 +73,7 @@ export default function Home() {
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<link rel="icon" href="/favicon.ico" />
|
||||
</Head>
|
||||
<ToastContainer />
|
||||
{user && (
|
||||
<main className="w-full h-full min-h-[100vh] flex flex-col items-center bg-neutral-100 text-black">
|
||||
<Navbar userType={user.type} profilePicture={user.profilePicture} showExamEnd={showEndExam} />
|
||||
|
||||
Reference in New Issue
Block a user