frontPanel/src/utils/hooks/useAnalytics.ts

62 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-03-28 09:18:24 +00:00
import { useEffect, useState } from "react";
2024-04-01 18:09:26 +00:00
import {
DevicesResponse,
GeneralResponse,
getDevices,
getGeneral,
getQuestions,
QuestionsResponse,
} from "@api/statistic";
2024-03-28 09:18:24 +00:00
2024-04-03 14:22:51 +00:00
import type { Moment } from "moment";
2024-04-01 18:09:26 +00:00
interface useAnalyticsProps {
ready: boolean;
2024-04-01 18:09:26 +00:00
quizId: string | undefined;
2024-04-03 14:22:51 +00:00
to: Moment | null;
from: Moment | null;
2024-03-28 09:18:24 +00:00
}
export function useAnalytics({ ready, quizId, to, from }: useAnalyticsProps) {
2024-04-03 14:22:51 +00:00
const formatTo = to?.unix();
const formatFrom = from?.unix();
2024-04-01 18:09:26 +00:00
const [devices, setDevices] = useState<DevicesResponse | null>(null);
const [general, setGeneral] = useState<GeneralResponse | null>(null);
const [questions, setQuestions] = useState<QuestionsResponse | null>(null);
2024-03-28 09:18:24 +00:00
useEffect(() => {
if (!quizId || !ready) return;
2024-04-03 14:22:51 +00:00
const requestStatistics = async () => {
if (!formatTo || !formatFrom) {
return;
}
2024-04-03 10:50:17 +00:00
const [gottenGeneral] = await getGeneral(quizId, formatTo, formatFrom);
const [gottenDevices] = await getDevices(quizId, formatTo, formatFrom);
const [gottenQuestions] = await getQuestions(
quizId,
formatTo,
formatFrom,
);
if (gottenGeneral) {
setGeneral(gottenGeneral);
}
if (gottenDevices) {
setDevices(gottenDevices);
}
if (gottenQuestions) {
setQuestions(gottenQuestions);
}
2024-04-03 14:22:51 +00:00
};
requestStatistics();
}, [ready, quizId, to, from]);
2024-03-28 09:18:24 +00:00
return { devices, general, questions };
}