From b59589b855aaf83c8e80a0f3eeeb7fc44735af61 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Tue, 7 May 2024 09:07:17 +0100 Subject: [PATCH] ENCOA-26: Student profile count stats was invalid --- src/dashboards/Student.tsx | 2 +- src/hooks/useStats.tsx | 14 +++++++++----- src/pages/stats.tsx | 11 +++++------ src/utils/moduleUtils.ts | 2 +- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/dashboards/Student.tsx b/src/dashboards/Student.tsx index 3e5f5999..6b1c7061 100644 --- a/src/dashboards/Student.tsx +++ b/src/dashboards/Student.tsx @@ -35,7 +35,7 @@ interface Props { export default function StudentDashboard({user}: Props) { const [corporateUserToShow, setCorporateUserToShow] = useState(); - const {stats} = useStats(user.id); + const {stats} = useStats(user.id, !user?.id); const {users} = useUsers(); const {assignments, isLoading: isAssignmentsLoading, reload: reloadAssignments} = useAssignments({assignees: user?.id}); const {invites, isLoading: isInvitesLoading, reload: reloadInvites} = useInvites({to: user.id}); diff --git a/src/hooks/useStats.tsx b/src/hooks/useStats.tsx index e076f737..bcea4234 100644 --- a/src/hooks/useStats.tsx +++ b/src/hooks/useStats.tsx @@ -2,18 +2,22 @@ import {Stat, User} from "@/interfaces/user"; import axios from "axios"; import {useEffect, useState} from "react"; -export default function useStats(id?: string) { +export default function useStats(id?: string, shouldNotQuery?: boolean) { const [stats, setStats] = useState([]); const [isLoading, setIsLoading] = useState(false); const [isError, setIsError] = useState(false); - useEffect(() => { + const getData = () => { + if (shouldNotQuery) return; + setIsLoading(true); axios .get(!id ? "/api/stats" : `/api/stats/user/${id}`) - .then((response) => setStats(response.data)) + .then((response) => setStats(response.data.filter((x) => (id ? x.user === id : true)))) .finally(() => setIsLoading(false)); - }, [id]); + }; - return {stats, isLoading, isError}; + useEffect(getData, [id, shouldNotQuery]); + + return {stats, reload: getData, isLoading, isError}; } diff --git a/src/pages/stats.tsx b/src/pages/stats.tsx index e19324c9..eb1490d3 100644 --- a/src/pages/stats.tsx +++ b/src/pages/stats.tsx @@ -72,8 +72,7 @@ export default function Stats() { const {user} = useUser({redirectTo: "/login"}); const {users} = useUsers(); const {groups} = useGroups(user?.id); - const {stats} = useStats(statsUserId); - const {stats: userStats} = useStats(user?.id); + const {stats} = useStats(statsUserId, !statsUserId); useEffect(() => { if (user) setStatsUserId(user.id); @@ -160,23 +159,23 @@ export default function Stats() { {user && ( x.id === statsUserId) || user} items={[ { icon: , - value: countFullExams(userStats), + value: countFullExams(stats), label: "Exams", tooltip: "Number of all conducted completed exams", }, { icon: , - value: countExamModules(userStats), + value: countExamModules(stats), label: "Modules", tooltip: "Number of all exam modules performed including Level Test", }, { icon: , - value: `${userStats.length > 0 ? averageScore(userStats) : 0}%`, + value: `${stats.length > 0 ? averageScore(stats) : 0}%`, label: "Average Score", tooltip: "Average success rate for questions responded", }, diff --git a/src/utils/moduleUtils.ts b/src/utils/moduleUtils.ts index 9fd8ccf8..5ee11d48 100644 --- a/src/utils/moduleUtils.ts +++ b/src/utils/moduleUtils.ts @@ -37,7 +37,7 @@ export const countFullExams = (stats: Stat[]) => { const sessionExams = groupBySession(stats); return Object.keys(sessionExams).filter((x) => { const sessionStats = sessionExams[x as keyof typeof sessionExams]; - const sessionModules = uniq(sessionStats.map((x) => x.module)); + const sessionModules = sessionStats.map((x) => x.module); return ( sessionModules.includes("reading") &&