From c37bb2691b01b3ea505db6c0add0ca2e21d198d3 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Mon, 18 Dec 2023 17:57:27 +0000 Subject: [PATCH] Added the ability to view the stats in a specific time interval --- src/dashboards/Admin.tsx | 2 +- src/pages/stats.tsx | 71 +++++++++++++++++++++++----------------- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/src/dashboards/Admin.tsx b/src/dashboards/Admin.tsx index 37514f3d..b91291d7 100644 --- a/src/dashboards/Admin.tsx +++ b/src/dashboards/Admin.tsx @@ -206,7 +206,7 @@ export default function AdminDashboard({user}: Props) { const DefaultDashboard = () => ( <> -
+
{ export default function Stats() { const [statsUserId, setStatsUserId] = useState(); - const [startDate, setStartDate] = useState(null); + const [startDate, setStartDate] = useState(moment("01/01/2023").toDate()); const [endDate, setEndDate] = useState(new Date()); + const [displayStats, setDisplayStats] = useState([]); const {user} = useUser({redirectTo: "/login"}); const {users} = useUsers(); @@ -72,16 +75,19 @@ export default function Stats() { if (user) setStatsUserId(user.id); }, [user]); - // useEffect(() => { - // if (stats && stats.length > 0) { - // const sortedStats = stats.sort((a, b) => a.date - b.date); - // const firstStat = sortedStats.shift()!; + useEffect(() => { + const startDateFilter = (s: Stat) => moment.unix(s.date / 1000).isAfter(moment(startDate)); + const endDateFilter = (s: Stat) => { + console.log(moment.unix(s.date / 1000), moment(endDate).isAfter(moment.unix(s.date))); + return moment(endDate).isAfter(moment.unix(s.date / 1000)); + }; - // setStartDate(moment.unix(firstStat.date).toDate()); - // console.log(stats.filter((x) => moment.unix(x.date).isAfter(startDate))); - // console.log(stats.filter((x) => moment.unix(x.date).isBefore(endDate))); - // } - // }, [stats]); + const filters = []; + if (startDate) filters.push(startDateFilter); + if (endDate) filters.push(endDateFilter); + + setDisplayStats(filters.reduce((d, f) => d.filter(f), stats)); + }, [endDate, startDate, stats]); const calculateTotalScorePerSession = () => { const groupedBySession = groupBySession(stats); @@ -167,7 +173,7 @@ export default function Stats() { />
-
+
<> {(user.type === "developer" || user.type === "admin") && (