Merged in bug-user-level-progress (pull request #37)

Bug user level progress

Approved-by: Alvaro Doria
This commit is contained in:
João Ramos
2024-02-19 14:35:08 +00:00
committed by Alvaro Doria
2 changed files with 40 additions and 27 deletions

View File

@@ -227,35 +227,40 @@ export default function StudentDashboard({user}: Props) {
<section className="flex flex-col gap-3"> <section className="flex flex-col gap-3">
<span className="text-lg font-bold">Score History</span> <span className="text-lg font-bold">Score History</span>
<div className="-md:grid-rows-4 grid gap-6 md:grid-cols-2"> <div className="-md:grid-rows-4 grid gap-6 md:grid-cols-2">
{MODULE_ARRAY.map((module) => ( {MODULE_ARRAY
<div className="border-mti-gray-anti-flash flex flex-col gap-2 rounded-xl border p-4" key={module}> .map((module) => {
<div className="flex items-center gap-2 md:gap-3"> const desiredLevel = user.desiredLevels[module] || 9;
<div className="bg-mti-gray-smoke flex h-8 w-8 items-center justify-center rounded-lg md:h-12 md:w-12 md:rounded-xl"> const level = user.levels[module] || 0;
{module === "reading" && <BsBook className="text-ielts-reading h-4 w-4 md:h-5 md:w-5" />} return (
{module === "listening" && <BsHeadphones className="text-ielts-listening h-4 w-4 md:h-5 md:w-5" />} <div className="border-mti-gray-anti-flash flex flex-col gap-2 rounded-xl border p-4" key={module}>
{module === "writing" && <BsPen className="text-ielts-writing h-4 w-4 md:h-5 md:w-5" />} <div className="flex items-center gap-2 md:gap-3">
{module === "speaking" && <BsMegaphone className="text-ielts-speaking h-4 w-4 md:h-5 md:w-5" />} <div className="bg-mti-gray-smoke flex h-8 w-8 items-center justify-center rounded-lg md:h-12 md:w-12 md:rounded-xl">
{module === "level" && <BsClipboard className="text-ielts-level h-4 w-4 md:h-5 md:w-5" />} {module === "reading" && <BsBook className="text-ielts-reading h-4 w-4 md:h-5 md:w-5" />}
{module === "listening" && <BsHeadphones className="text-ielts-listening h-4 w-4 md:h-5 md:w-5" />}
{module === "writing" && <BsPen className="text-ielts-writing h-4 w-4 md:h-5 md:w-5" />}
{module === "speaking" && <BsMegaphone className="text-ielts-speaking h-4 w-4 md:h-5 md:w-5" />}
{module === "level" && <BsClipboard className="text-ielts-level h-4 w-4 md:h-5 md:w-5" />}
</div>
<div className="flex w-full justify-between">
<span className="text-sm font-bold md:font-extrabold">{capitalize(module)}</span>
<span className="text-mti-gray-dim text-sm font-normal">
Level {level} / Level 9 (Desired Level: {desiredLevel})
</span>
</div>
</div> </div>
<div className="flex w-full justify-between"> <div className="md:pl-14">
<span className="text-sm font-bold md:font-extrabold">{capitalize(module)}</span> <ProgressBar
<span className="text-mti-gray-dim text-sm font-normal"> color={module}
Level {user.levels[module] || 0} / Level 9 (Desired Level: {user.desiredLevels[module] || 9}) label=""
</span> mark={Math.round((desiredLevel * 100) / 9)}
markLabel={`Desired Level: ${desiredLevel}`}
percentage={Math.round((level * 100) / 9)}
className="h-2 w-full"
/>
</div> </div>
</div> </div>
<div className="md:pl-14"> );
<ProgressBar })}
color={module}
label=""
mark={Math.round((user.desiredLevels[module] * 100) / 9)}
markLabel={`Desired Level: ${user.desiredLevels[module]}`}
percentage={Math.round((user.levels[module] * 100) / 9)}
className="h-2 w-full"
/>
</div>
</div>
))}
</div> </div>
</section> </section>
</> </>

View File

@@ -5,6 +5,7 @@ import {Stat, User} from "@/interfaces/user";
import {sessionOptions} from "@/lib/session"; import {sessionOptions} from "@/lib/session";
import {calculateBandScore} from "@/utils/score"; import {calculateBandScore} from "@/utils/score";
import {groupByModule, groupBySession} from "@/utils/stats"; import {groupByModule, groupBySession} from "@/utils/stats";
import { MODULE_ARRAY } from "@/utils/moduleUtils";
import {getAuth} from "firebase/auth"; import {getAuth} from "firebase/auth";
import {collection, doc, getDoc, getDocs, getFirestore, query, updateDoc, where} from "firebase/firestore"; import {collection, doc, getDoc, getDocs, getFirestore, query, updateDoc, where} from "firebase/firestore";
import {withIronSessionApiRoute} from "iron-session/next"; import {withIronSessionApiRoute} from "iron-session/next";
@@ -55,7 +56,7 @@ async function update(req: NextApiRequest, res: NextApiResponse) {
}, },
}; };
MODULES.forEach((module: Module) => { MODULE_ARRAY.forEach((module: Module) => {
const moduleStats = sessionStats.filter((x) => x.module === module); const moduleStats = sessionStats.filter((x) => x.module === module);
if (moduleStats.length === 0) return; if (moduleStats.length === 0) return;
@@ -87,11 +88,18 @@ async function update(req: NextApiRequest, res: NextApiResponse) {
.filter((x) => x.total > 0) .filter((x) => x.total > 0)
.reduce((acc, cur) => ({total: acc.total + cur.total, correct: acc.correct + cur.correct}), {total: 0, correct: 0}); .reduce((acc, cur) => ({total: acc.total + cur.total, correct: acc.correct + cur.correct}), {total: 0, correct: 0});
const levelLevel = sessionLevels
.map((x) => x.level)
.filter((x) => x.total > 0)
.reduce((acc, cur) => ({total: acc.total + cur.total, correct: acc.correct + cur.correct}), {total: 0, correct: 0});
const levels = { const levels = {
reading: calculateBandScore(readingLevel.correct, readingLevel.total, "reading", req.session.user.focus), reading: calculateBandScore(readingLevel.correct, readingLevel.total, "reading", req.session.user.focus),
listening: calculateBandScore(listeningLevel.correct, listeningLevel.total, "listening", req.session.user.focus), listening: calculateBandScore(listeningLevel.correct, listeningLevel.total, "listening", req.session.user.focus),
writing: calculateBandScore(writingLevel.correct, writingLevel.total, "writing", req.session.user.focus), writing: calculateBandScore(writingLevel.correct, writingLevel.total, "writing", req.session.user.focus),
speaking: calculateBandScore(speakingLevel.correct, speakingLevel.total, "speaking", req.session.user.focus), speaking: calculateBandScore(speakingLevel.correct, speakingLevel.total, "speaking", req.session.user.focus),
level: calculateBandScore(levelLevel.correct, levelLevel.total, "level", req.session.user.focus),
}; };
const userDoc = doc(db, "users", req.session.user.id); const userDoc = doc(db, "users", req.session.user.id);