получение данных

This commit is contained in:
Nastya 2024-03-28 12:18:24 +03:00
parent b0b6352cc0
commit dd58065255
4 changed files with 55 additions and 5 deletions

@ -25,13 +25,15 @@ export type QuestionsResponse = {
export const getDevices = async ( export const getDevices = async (
quizId: string, quizId: string,
to: number,
from: number,
): Promise<[DevicesResponse | null, string?]> => { ): Promise<[DevicesResponse | null, string?]> => {
try { try {
const devicesResponse = await makeRequest<unknown, DevicesResponse>({ const devicesResponse = await makeRequest<unknown, DevicesResponse>({
method: "POST", method: "POST",
url: `${apiUrl}/${quizId}/devices`, url: `${apiUrl}/${quizId}/devices`,
useToken: false,
withCredentials: true, withCredentials: true,
body: {to, from}
}); });
return [devicesResponse]; return [devicesResponse];
@ -44,13 +46,15 @@ export const getDevices = async (
export const getGeneral = async ( export const getGeneral = async (
quizId: string, quizId: string,
to: number,
from: number,
): Promise<[GeneralResponse | null, string?]> => { ): Promise<[GeneralResponse | null, string?]> => {
try { try {
const generalResponse = await makeRequest<unknown, GeneralResponse>({ const generalResponse = await makeRequest<unknown, GeneralResponse>({
method: "POST", method: "POST",
url: `${apiUrl}/${quizId}/general`, url: `${apiUrl}/${quizId}/general`,
useToken: false,
withCredentials: true, withCredentials: true,
body: {to, from}
}); });
return [generalResponse]; return [generalResponse];
@ -63,13 +67,15 @@ export const getGeneral = async (
export const getQuestions = async ( export const getQuestions = async (
quizId: string, quizId: string,
to: number,
from: number,
): Promise<[QuestionsResponse | null, string?]> => { ): Promise<[QuestionsResponse | null, string?]> => {
try { try {
const questionsResponse = await makeRequest<unknown, QuestionsResponse>({ const questionsResponse = await makeRequest<unknown, QuestionsResponse>({
method: "POST", method: "POST",
url: `${apiUrl}/${quizId}/questions`, url: `${apiUrl}/${quizId}/questions`,
useToken: false,
withCredentials: true, withCredentials: true,
body: {to, from}
}); });
return [questionsResponse]; return [questionsResponse];

@ -1,4 +1,4 @@
import { useState } from "react"; import { useLayoutEffect, useState } from "react";
import { import {
Box, Box,
Button, Button,
@ -11,6 +11,8 @@ import {
import { DatePicker } from "@mui/x-date-pickers"; import { DatePicker } from "@mui/x-date-pickers";
import { LineChart } from "@mui/x-charts"; import { LineChart } from "@mui/x-charts";
import moment from "moment"; import moment from "moment";
import { useQuizStore } from "@root/quizes/store";
import { useAnalytics } from "@utils/hooks/useAnalytics";
import HeaderFull from "@ui_kit/Header/HeaderFull"; import HeaderFull from "@ui_kit/Header/HeaderFull";
import SectionWrapper from "@ui_kit/SectionWrapper"; import SectionWrapper from "@ui_kit/SectionWrapper";
@ -20,11 +22,22 @@ import { AnswersStatistics } from "./Answers";
import { Devices } from "./Devices"; import { Devices } from "./Devices";
import CalendarIcon from "@icons/CalendarIcon"; import CalendarIcon from "@icons/CalendarIcon";
import { redirect } from "react-router-dom";
export default function Analytics() { export default function Analytics() {
const { editQuizId } = useQuizStore();
const [isOpen, setOpen] = useState(false); const [isOpen, setOpen] = useState(false);
const [isOpenEnd, setOpenEnd] = useState(false); const [isOpenEnd, setOpenEnd] = useState(false);
const [to, setTo] = useState(0);
const [from, setFrom] = useState(0);
const { devices, general, questions } = useAnalytics({quizId: editQuizId?.toString(), to, from})
useLayoutEffect(() => {
console.log("editQuizId ", editQuizId)
if (editQuizId === undefined) redirect("/list")
}, [editQuizId])
const theme = useTheme(); const theme = useTheme();
const isTablet = useMediaQuery(theme.breakpoints.down(1000)); const isTablet = useMediaQuery(theme.breakpoints.down(1000));
const isMobile = useMediaQuery(theme.breakpoints.down(600)); const isMobile = useMediaQuery(theme.breakpoints.down(600));

@ -48,7 +48,7 @@ export default function QuizCard({
} }
function handleStatisticClick() { function handleStatisticClick() {
setEditQuizId(quiz.backendId); setEditQuizId(quiz.backendId);
navigate(`/analytics/${quiz.backendId}`); navigate(`/analytics`);
} }
const questionCount = useRef(quiz.questions_count.toString() || ""); const questionCount = useRef(quiz.questions_count.toString() || "");

@ -0,0 +1,31 @@
import { getGeneral, getDevices, getQuestions } from "@api/statistic";
import { useEffect, useState } from "react";
interface Props {
quizId: string;
to: number;
from: number;
}
export function useAnalytics({
quizId,
to,
from,
}: Props) {
const [devices, setDevices] = useState();
const [general, setGeneral] = useState();
const [questions, setQuestions] = useState();
useEffect(() => {
(async () => {
const gottenGeneral = await getGeneral(quizId, to, from)
const gottenDevices = await getDevices(quizId, to, from)
const gottenQuestions = await getQuestions(quizId, to, from)
setDevices(gottenGeneral)
setGeneral(gottenDevices)
setQuestions(gottenQuestions)
})()
}, [to, from]);
return { devices, general, questions };
}