frontAnswerer/src/pages/ViewPublicationPage/questions/Date.tsx

127 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-12-16 14:55:56 +00:00
import dayjs from "dayjs";
import { DatePicker } from "@mui/x-date-pickers";
2023-12-29 00:58:19 +00:00
import { Box, Typography, useTheme } from "@mui/material";
import { modes } from "../../../utils/themes/Publication/themePublication";
2023-12-16 14:55:56 +00:00
2024-01-19 11:46:17 +00:00
import { useQuizViewStore, updateAnswer } from "@stores/quizView/store";
2023-12-16 14:55:56 +00:00
import type { QuizQuestionDate } from "../../../model/questionTypes/date";
import CalendarIcon from "@icons/CalendarIcon";
import { enqueueSnackbar } from "notistack";
import { sendAnswer } from "@api/quizRelase";
2024-01-19 11:46:17 +00:00
import { useQuestionsStore } from "@stores/quizData/store";
2023-12-16 14:55:56 +00:00
type DateProps = {
currentQuestion: QuizQuestionDate;
};
export const Date = ({ currentQuestion }: DateProps) => {
2023-12-29 00:58:19 +00:00
const theme = useTheme();
const mode = modes;
2024-01-09 10:17:56 +00:00
const { settings } = useQuestionsStore();
2023-12-16 14:55:56 +00:00
const { answers } = useQuizViewStore();
const answer = answers.find(
({ questionId }) => questionId === currentQuestion.id
2023-12-16 14:55:56 +00:00
)?.answer as string;
const [day, month, year] = answer?.split(".") || [];
return (
<Box>
2024-01-09 10:17:56 +00:00
<Typography variant="h5" color={theme.palette.text.primary}>
{currentQuestion.title}
</Typography>
2023-12-16 14:55:56 +00:00
<Box
sx={{
display: "flex",
flexDirection: "column",
width: "100%",
marginTop: "20px",
}}
>
<DatePicker
slots={{
2023-12-29 00:58:19 +00:00
//@ts-ignore
2024-01-09 10:17:56 +00:00
openPickerIcon: () => (
<CalendarIcon
sx={{
"& path": { stroke: theme.palette.primary.main },
"& rect": { stroke: theme.palette.primary.main },
}}
/>
),
2023-12-16 14:55:56 +00:00
}}
value={dayjs(
2024-01-09 10:17:56 +00:00
month && day && year
2023-12-16 14:55:56 +00:00
? new window.Date(`${month}.${day}.${year}`)
: new window.Date()
)}
onChange={async (date) => {
2023-12-16 14:55:56 +00:00
if (!date) {
return;
}
2024-01-09 10:17:56 +00:00
try {
await sendAnswer({
questionId: currentQuestion.id,
2024-01-09 10:17:56 +00:00
body: new window.Date(date.toDate()).toLocaleDateString(
"ru-RU",
{
year: "numeric",
month: "2-digit",
day: "2-digit",
}
),
//@ts-ignore
2024-01-09 10:17:56 +00:00
qid: settings.qid,
});
updateAnswer(
currentQuestion.id,
String(
new window.Date(date.toDate()).toLocaleDateString("ru-RU", {
year: "numeric",
month: "2-digit",
day: "2-digit",
})
)
);
} catch (e) {
2024-01-09 10:17:56 +00:00
enqueueSnackbar("ответ не был засчитан");
}
2023-12-16 14:55:56 +00:00
}}
slotProps={{
openPickerButton: {
sx: {
p: 0,
},
"data-cy": "open-datepicker",
},
2024-01-09 10:17:56 +00:00
layout: {
sx: { backgroundColor: theme.palette.background.default },
},
2023-12-16 14:55:56 +00:00
}}
sx={{
"& .MuiInputBase-root": {
2024-01-09 10:17:56 +00:00
backgroundColor: mode[settings.cfg.theme]
? "white"
: theme.palette.background.default,
2023-12-16 14:55:56 +00:00
borderRadius: "10px",
maxWidth: "250px",
pr: "22px",
"& input": {
py: "11px",
pl: "20px",
lineHeight: "19px",
},
"& fieldset": {
borderColor: "#9A9AAF",
},
},
}}
/>
</Box>
</Box>
);
2024-01-09 10:17:56 +00:00
};