62 lines
1.4 KiB
TypeScript
62 lines
1.4 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import {
|
|
DevicesResponse,
|
|
GeneralResponse,
|
|
getDevices,
|
|
getGeneral,
|
|
getQuestions,
|
|
QuestionsResponse,
|
|
} from "@api/statistic";
|
|
|
|
import type { Moment } from "moment";
|
|
|
|
interface useAnalyticsProps {
|
|
ready: boolean;
|
|
quizId: string | undefined;
|
|
to: Moment | null;
|
|
from: Moment | null;
|
|
}
|
|
|
|
export function useAnalytics({ ready, quizId, to, from }: useAnalyticsProps) {
|
|
const formatTo = to?.unix();
|
|
const formatFrom = from?.unix();
|
|
|
|
const [devices, setDevices] = useState<DevicesResponse | null>(null);
|
|
const [general, setGeneral] = useState<GeneralResponse | null>(null);
|
|
const [questions, setQuestions] = useState<QuestionsResponse | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (!quizId || !ready) return;
|
|
|
|
const requestStatistics = async () => {
|
|
if (!formatTo || !formatFrom) {
|
|
return;
|
|
}
|
|
|
|
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);
|
|
}
|
|
};
|
|
|
|
requestStatistics();
|
|
}, [ready, quizId, to, from]);
|
|
|
|
return { devices, general, questions };
|
|
}
|