Created the stats page where a user can select another user to view their stats;

Improved the whole stats and the home page
This commit is contained in:
Tiago Ribeiro
2023-04-20 22:43:30 +01:00
parent e60130069d
commit 02d76e4c3c
13 changed files with 361 additions and 124 deletions

View File

@@ -1,37 +1,37 @@
import {Module} from "@/interfaces";
import {Stat} from "@/interfaces/user";
import {capitalize} from "lodash";
import {convertCamelCaseToReadable} from "@/utils/string";
export const totalExams = (stats: Stat[]): number => {
const moduleStats = formatModuleTotalStats(stats);
return moduleStats.reduce((previous, current) => previous + current.value, 0);
};
export const averageScore = (stats: Stat[]): number => {
const {correct, total} = stats.reduce(
(acc, current) => ({correct: acc.correct + current.score.correct, total: acc.total + current.score.total}),
{correct: 0, total: 0},
);
return parseFloat(((correct / total) * 100).toFixed(2));
};
export const formatModuleTotalStats = (stats: Stat[]): {label: string; value: number}[] => {
const result: {[key in Module]: {exams: string[]; total: number}} = {
reading: {
exams: [],
total: 0,
},
listening: {
exams: [],
total: 0,
},
writing: {
exams: [],
total: 0,
},
speaking: {
exams: [],
total: 0,
},
};
const moduleSessions: {[key: string]: string[]} = {};
stats.forEach((stat) => {
if (result[stat.module].exams)
result[stat.module] = {
exams: [...result[stat.module].exams.filter((x) => x !== stat.exam), stat.exam],
total: result[stat.module].total + 1,
};
if (stat.module in moduleSessions) {
if (!moduleSessions[stat.module].includes(stat.session)) {
moduleSessions[stat.module] = [...moduleSessions[stat.module], stat.session];
}
} else {
moduleSessions[stat.module] = [stat.session];
}
});
return Object.keys(result).map((key) => ({label: capitalize(key), value: result[key as Module].total}));
return ["reading", "listening", "writing", "speaking"].map((module) => ({
label: capitalize(module),
value: moduleSessions[module]?.length || 0,
}));
};
export const formatModuleAverageScoreStats = (stats: Stat[]): {label: string; value: number}[] => {
@@ -48,12 +48,12 @@ export const formatModuleAverageScoreStats = (stats: Stat[]): {label: string; va
}
});
return Object.keys(moduleScores).map((x) => {
const {correct, total} = moduleScores[x as keyof typeof moduleScores];
return ["reading", "listening", "writing", "speaking"].map((x) => {
const score = moduleScores[x as keyof typeof moduleScores];
return {
label: capitalize(x),
value: correct / total,
value: score ? parseFloat(((score.correct / score.total) * 100).toFixed(2)) : 0,
};
});
};
@@ -86,7 +86,7 @@ export const formatExerciseAverageScoreStats = (stats: Stat[]): {label: string;
return {
label: convertCamelCaseToReadable(x),
value: correct / total,
value: parseFloat(((correct / total) * 100).toFixed(2)),
};
});
};