adminFront/src/utils/hooks/useQuizStatistic.ts

35 lines
770 B
TypeScript
Raw Normal View History

2024-04-03 21:53:48 +00:00
import { useEffect, useState } from "react";
2024-04-30 11:30:59 +00:00
import { QuizStatisticResponse, getStatistic } from "@root/api/quizStatistics";
2024-04-03 21:53:48 +00:00
import type { Moment } from "moment";
interface useQuizStatisticProps {
2024-05-21 07:41:31 +00:00
to: Moment | null;
from: Moment | null;
2024-04-03 21:53:48 +00:00
}
export function useQuizStatistic({ to, from }: useQuizStatisticProps) {
2024-05-21 07:41:31 +00:00
const formatTo = to?.unix();
const formatFrom = from?.unix();
2024-04-03 21:53:48 +00:00
2024-05-23 12:01:51 +00:00
const [data, setData] = useState<QuizStatisticResponse>({
2024-05-21 07:41:31 +00:00
Registrations: 0,
Quizes: 0,
Results: 0,
});
2024-04-03 21:53:48 +00:00
2024-05-21 07:41:31 +00:00
useEffect(() => {
const requestStatistics = async () => {
2024-05-23 12:01:51 +00:00
const [gottenData] = await getStatistic(Number(formatTo), Number(formatFrom));
if (gottenData) {
setData(gottenData);
}
2024-05-21 07:41:31 +00:00
};
2024-04-03 21:53:48 +00:00
2024-05-21 07:41:31 +00:00
requestStatistics();
}, [formatTo, formatFrom]);
2024-04-03 21:53:48 +00:00
2024-05-21 07:41:31 +00:00
return { ...data };
2024-04-03 21:53:48 +00:00
}