frontPanel/src/utils/hooks/useAnalytics.ts

33 lines
1.0 KiB
TypeScript
Raw Normal View History

2024-03-28 09:18:24 +00:00
import { getGeneral, getDevices, getQuestions } from "@api/statistic";
import { useEffect, useState } from "react";
2024-03-29 06:10:33 +00:00
import moment from "moment";
2024-03-28 09:18:24 +00:00
interface Props {
quizId: string;
to: number;
from: number;
2024-03-28 09:18:24 +00:00
}
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({});
2024-03-28 09:18:24 +00:00
useEffect(() => {
(async () => {
const gottenGeneral = await getGeneral(quizId, formatTo, formatFrom);
const gottenDevices = await getDevices(quizId, formatTo, formatFrom);
const gottenQuestions = await getQuestions(quizId, formatTo, formatFrom);
setDevices(gottenGeneral[0]);
setGeneral(gottenDevices[0]);
setQuestions(gottenQuestions[0]);
})();
}, [to, from]);
2024-03-28 09:18:24 +00:00
return { devices, general, questions };
}