frontPanel/src/utils/hooks/useAnalytics.ts
2024-04-03 13:50:17 +03:00

46 lines
1.2 KiB
TypeScript

import { getGeneral, getDevices, getQuestions } from "@api/statistic";
import { useEffect, useState } from "react";
import moment from "moment";
interface Props {
quizId: string;
to: number;
from: number;
}
export function useAnalytics({ quizId, to, from }: Props) {
const formatTo = to === null ? 0 : moment(to).unix();
const formatFrom = from === null ? 0 : moment(from).unix();
console.log(to, from);
if (quizId === undefined) return {};
const [devices, setDevices] = useState({});
const [general, setGeneral] = useState({});
const [questions, setQuestions] = useState({});
useEffect(() => {
(async () => {
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);
}
})();
}, [to, from]);
return { devices, general, questions };
}