frontAnswerer/lib/components/ViewPublicationPage/questions/Date/index.tsx

111 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-04-23 14:45:49 +00:00
import { useState } from "react";
import moment from "moment";
2023-12-16 14:55:56 +00:00
import { DatePicker } from "@mui/x-date-pickers";
2023-12-29 00:58:19 +00:00
import { Box, Typography, useTheme } from "@mui/material";
import { enqueueSnackbar } from "notistack";
2024-04-23 14:45:49 +00:00
import { sendAnswer } from "@api/quizRelase";
2024-04-23 14:45:49 +00:00
import { useQuizViewStore } from "@/stores/quizView";
import { useQuizData } from "@contexts/QuizDataContext";
2024-02-02 14:35:02 +00:00
import { quizThemes } from "@utils/themes/Publication/themePublication";
2024-04-23 14:45:49 +00:00
import CalendarIcon from "@icons/CalendarIcon";
import type { Moment } from "moment";
import type { QuizQuestionDate } from "@model/questionTypes/date";
2023-12-16 14:55:56 +00:00
type DateProps = {
2024-04-22 14:13:43 +00:00
currentQuestion: QuizQuestionDate;
2023-12-16 14:55:56 +00:00
};
export const Date = ({ currentQuestion }: DateProps) => {
2024-04-23 14:45:49 +00:00
const [isSending, setIsSending] = useState<boolean>(false);
2024-04-22 14:13:43 +00:00
const { settings, quizId, preview } = useQuizData();
const answers = useQuizViewStore((state) => state.answers);
2024-04-23 14:45:49 +00:00
const { updateAnswer } = useQuizViewStore((state) => state);
const theme = useTheme();
2024-04-22 14:13:43 +00:00
const answer = answers.find(
({ questionId }) => questionId === currentQuestion.id
)?.answer as string;
const currentAnswer = moment(answer) || moment();
2024-04-23 14:45:49 +00:00
const onDateChange = async (date: Moment | null) => {
if (isSending || !date) return;
setIsSending(true);
try {
await sendAnswer({
questionId: currentQuestion.id,
body: moment(date).format("YYYY.MM.DD"),
qid: quizId,
preview,
});
updateAnswer(currentQuestion.id, date, 0);
} catch (error) {
enqueueSnackbar("ответ не был засчитан");
}
setIsSending(false);
};
2024-04-22 14:13:43 +00:00
return (
<Box>
<Typography
variant="h5"
color={theme.palette.text.primary}
sx={{ wordBreak: "break-word" }}
>
{currentQuestion.title}
</Typography>
<Box
sx={{
display: "flex",
flexDirection: "column",
width: "100%",
marginTop: "20px",
}}
>
<DatePicker
format="DD/MM/YYYY"
slots={{
openPickerIcon: () => (
<CalendarIcon
2024-01-09 10:17:56 +00:00
sx={{
2024-04-22 14:13:43 +00:00
"& path": { stroke: theme.palette.primary.main },
"& rect": { stroke: theme.palette.primary.main },
2024-01-09 10:17:56 +00:00
}}
2024-04-22 14:13:43 +00:00
/>
),
}}
value={currentAnswer}
2024-04-23 14:45:49 +00:00
onChange={onDateChange}
2024-04-22 14:13:43 +00:00
slotProps={{
2024-04-23 14:45:49 +00:00
openPickerButton: { sx: { p: 0 }, "data-cy": "open-datepicker" },
2024-04-22 14:13:43 +00:00
layout: {
sx: { backgroundColor: theme.palette.background.default },
},
}}
sx={{
"& .MuiInputBase-root": {
backgroundColor: settings.cfg.design
? quizThemes[settings.cfg.theme].isLight
? "#F2F3F7"
: "rgba(154,154,175, 0.2)"
: quizThemes[settings.cfg.theme].isLight
? "white"
: theme.palette.background.default,
borderRadius: "10px",
maxWidth: "250px",
pr: "30px",
2024-04-23 14:45:49 +00:00
"& input": { py: "11px", pl: "20px", lineHeight: "19px" },
"& fieldset": { borderColor: "#9A9AAF" },
2024-04-22 14:13:43 +00:00
},
}}
/>
</Box>
</Box>
);
2024-01-09 10:17:56 +00:00
};