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 {
|
2024-03-30 18:54:28 +00:00
|
|
|
quizId: string;
|
|
|
|
to: number;
|
|
|
|
from: number;
|
2024-03-28 09:18:24 +00:00
|
|
|
}
|
|
|
|
|
2024-03-30 18:54:28 +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
|
|
|
|
2024-03-30 18:54:28 +00:00
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
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-03-30 18:54:28 +00:00
|
|
|
})();
|
|
|
|
}, [to, from]);
|
2024-03-28 09:18:24 +00:00
|
|
|
|
2024-03-30 18:54:28 +00:00
|
|
|
return { devices, general, questions };
|
|
|
|
}
|