frontPanel/src/pages/QuizAnswersPage/QuizAnswersPage.tsx

338 lines
11 KiB
TypeScript
Raw Normal View History

2024-01-20 10:27:21 +00:00
import {
Box,
Button,
IconButton,
Typography,
useTheme,
useMediaQuery,
Skeleton,
2024-01-20 10:27:21 +00:00
} from "@mui/material";
import HeaderFull from "@ui_kit/Header/HeaderFull";
import SectionWrapper from "@ui_kit/SectionWrapper";
import { FC, useEffect, useState } from "react";
2024-01-20 10:27:21 +00:00
import { FileExportIcon } from "./icons/FileExporIcon";
import { UpdateIcon } from "./icons/UpdateIcon";
import { Select } from "../../pages/Questions/Select";
import { CheckboxSelect } from "../../ui_kit/CheckboxSelect";
import { CardAnswer } from "./CardAnswer";
import { FilterIcon } from "./icons/FilterIcon";
2024-01-21 22:09:26 +00:00
import { FilterModal } from "@ui_kit/Modal/FilterModal/FilterModal";
import { ExportContactsModal } from "@ui_kit/Modal/ExportContactsModal";
import { AnswerResultListEx, getResultsList, obsolescenceResult } from "@api/result";
import { useObsolescenceIdResult, useResultStore } from "@root/results/store";
import { setResults } from "@root/results/actions";
import { quizApi } from "@api/quiz";
import { setQuizes } from "@root/quizes/actions";
import { useCurrentQuiz, useQuizes } from "@root/quizes/hooks";
import { useQuizStore } from "@root/quizes/store";
2024-01-20 10:27:21 +00:00
const options = [
{ label: "Муром (1)", value: "option1" },
{ label: "Москва (1)", value: "option2" },
];
let lossDebouncer: null | ReturnType<typeof setTimeout> = null
let lossId: string[] = [] as string[]
const onLossNew = (id: string) => {
//Если в массиве ещё нет такого id - добавляем
if (!lossId.includes(id)) lossId.push(id)
//Если таймер есть - сбрасываем
if (typeof lossDebouncer === "number") clearTimeout(lossDebouncer);
//Назначем новый таймер
lossDebouncer = setTimeout(async() => {
//стреляем на лишение новизны
await obsolescenceResult(lossId)
//сбрасываем массив
lossId = []
}, 3000)
}
2024-01-20 10:27:21 +00:00
export const QuizAnswersPage: FC = () => {
const theme = useTheme();
const isTablet = useMediaQuery(theme.breakpoints.down(1000));
2024-01-21 22:09:26 +00:00
const isMobile = useMediaQuery(theme.breakpoints.down(600));
2024-01-21 22:09:26 +00:00
const [filterModalOpen, setFilterModalOpen] = useState<boolean>(false);
const [exportContactsModalOpen, setExportContactsModalOpen] = useState<boolean>(false);
const [filterNew, setFilterNew] = useState<string>("За всё время")
const quizList = useQuizStore();
const quiz = useCurrentQuiz();
const { editQuizId } = useQuizStore();
const { results } = useResultStore();
const { total_count } = useResultStore();
// const {idResultArray, addIdResult, clearIdResultArray} = useObsolescenceIdResult()
useEffect(() => {
const getData = async () => {
if (editQuizId !== null) {
const quizes = await quizApi.getList();
setQuizes(quizes);
const result = await getResultsList(editQuizId);
console.log("Это данные с сервера", result);
setResults(result);
const resAnswer = await AnswerResultListEx(editQuizId)
console.log("щтветы респондентов на экспорт в юз эффекте", resAnswer)
}
};
getData();
}, []);
const DateDefinition = (result: string) => {
//определяем когда был получен результат - вчера, сегодня или число и месяц
let restime = new Date(result);
let timeCompleting = Date.parse(String(result));
const timeNow = Date.now();
let dayResult;
if (timeNow - timeCompleting < 86400000) {
dayResult = "Сегодня";
}
if (172800000 > timeNow - timeCompleting > 86400000) {
dayResult = "Вчера";
} else {
dayResult = restime.toLocaleDateString();
}
return dayResult;
};
const TimeDefinition = (result: string) => {
//достаём время
let timeResult = new Date(result).toLocaleTimeString().slice(0, -3);
return timeResult;
};
if (quiz === undefined)
return (
<Skeleton sx={{ width: "100vw", height: "100vh", transform: "none" }} />
);
2024-01-20 10:27:21 +00:00
return (
<Box>
<HeaderFull isRequest={true}/>
2024-01-21 22:09:26 +00:00
<SectionWrapper
sx={{ padding: isMobile ? "0 16px" : "20px" }}
maxWidth="lg"
>
2024-01-20 10:27:21 +00:00
<Typography
sx={{ fontSize: "36px", fontWeight: "500", mb: "50px", mt: "60px" }}
>
{quiz.name}
2024-01-20 10:27:21 +00:00
</Typography>
<Box
sx={{
width: "100%",
display: isTablet ? "flex" : "-moz-initial",
alignItems: "center",
justifyContent: "space-between",
}}
>
<Box sx={{ display: "flex", alignItems: "center", gap: "8px" }}>
<Typography sx={{ fontSize: "18px", color: "#4D4D4D" }}>
Ответы на квиз
</Typography>
<Typography sx={{ fontSize: "18px", color: "#9A9AAF" }}>
({total_count})
2024-01-20 10:27:21 +00:00
</Typography>
</Box>
<Box
sx={{
mt: "20px",
mb: "31px",
gap: "30px",
display: "flex",
alignItems: "center",
justifyContent: "space-between",
width: isTablet ? "auto" : "100%",
}}
>
<Box sx={{ display: "flex", alignItems: "center", gap: "10px" }}>
<IconButton
onClick={async () => {
//setExportContactsModalOpen(true)
const resAnswer = await AnswerResultListEx(quizList.editQuizId)
console.log("ответы респондентов на экспорт по клику", resAnswer)
const download = function () {
// Creating a Blob for having a csv file format
// and passing the data with type
const blob = new Blob([resAnswer], { type: 'text/csv' });
// Creating an object for downloading url
const url = window.URL.createObjectURL(blob)
// Creating an anchor(a) tag of HTML
const a = document.createElement('a')
// Passing the blob downloading url
a.setAttribute('href', url)
// Setting the anchor tag attribute for downloading
// and passing the download file name
a.setAttribute('download', 'download.csv');
// Performing a download with click
a.click()
}
download()
}}
2024-01-20 10:27:21 +00:00
sx={{
width: "44px",
height: "44px",
borderRadius: "8px",
border: "1px solid #7E2AEA",
}}
>
<FileExportIcon />
</IconButton>
<IconButton
sx={{
width: "44px",
height: "44px",
borderRadius: "8px",
border: "1px solid #7E2AEA",
}}
onClick={() => {
getResultsList(quizList.editQuizId);
}}
2024-01-20 10:27:21 +00:00
>
<UpdateIcon />
</IconButton>
{isTablet && (
<IconButton
2024-01-21 22:09:26 +00:00
onClick={() => setFilterModalOpen(true)}
2024-01-20 10:27:21 +00:00
sx={{
2024-01-21 22:09:26 +00:00
background: "#fff",
2024-01-20 10:27:21 +00:00
width: "44px",
height: "44px",
borderRadius: "8px",
border: "1px solid #7E2AEA",
}}
>
<FilterIcon />
</IconButton>
)}
</Box>
{!isTablet && (
<Box
sx={{
display: "flex",
alignItems: "center",
gap: "20px",
width: "100%",
justifyContent: "flex-end",
}}
>
<Select
sx={{ maxWidth: "223px", width: "100%" }}
items={[
"За все время",
"Сегодня",
"Вчера",
"Последние 7 дней",
"Последние 30 дней",
"Этот месяц",
]}
onChange={(e) => setFilterNew(e.value)}
2024-01-20 10:27:21 +00:00
/>
<Select
sx={{ maxWidth: "223px", width: "100%" }}
items={["Все заявки", "Новые", "Ошибка интеграции"]}
/>
<CheckboxSelect
sx={{ maxWidth: "223px", width: "100%" }}
placeholder="Выберите город"
options={options}
/>
<Button
sx={{
maxWidth: "200px",
width: "100%",
height: "48px",
borderRadius: "8px",
border: "1px solid #7E2AEA",
color: "#7E2AEA",
}}
>
Сбросить фильтры
</Button>
</Box>
)}
</Box>
</Box>
{!isTablet && (
<Box
sx={{
display: "flex",
width: "100%",
alignItems: "center",
mb: "15px",
}}
>
<Typography sx={{ fontSize: "18px", color: "#9A9AAF", mr: "40px" }}>
заявки
</Typography>
<Typography sx={{ fontSize: "18px", color: "#9A9AAF" }}>
Дата
</Typography>
<Typography
sx={{ fontSize: "18px", color: "#9A9AAF", ml: "288px" }}
>
Контакты
</Typography>
</Box>
)}
<Box
sx={{
display: "flex",
alignItems: "flex-start",
flexDirection: isTablet ? "-moz-initial" : "column",
flexWrap: isTablet ? "wrap" : "nowrap",
gap: "20px",
}}
>
{results.map((result) => {
const dataResult = new Date(result.created_at);
let dayResult = DateDefinition(result.created_at);
let timeResult = TimeDefinition(result.created_at);
return (
<CardAnswer
name={result.content.name}
email={result.content.email}
isNew={result.new}
idResult={result.id}
dayResult={dayResult}
timeResult={timeResult}
onLossNew={onLossNew}
/>
);
})}
2024-01-20 10:27:21 +00:00
</Box>
</SectionWrapper>
2024-01-21 22:09:26 +00:00
<FilterModal
open={filterModalOpen}
handleClose={() => setFilterModalOpen(false)}
/>
<ExportContactsModal
open={exportContactsModalOpen}
handleClose={() => setExportContactsModalOpen(false)}
/>
2024-01-20 10:27:21 +00:00
</Box>
);
};