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

119 lines
3.4 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
import { useQuizViewStore, updateAnswer } from "@root/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";
import { useQuestionsStore } from "@root/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;
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>
2023-12-29 00:58:19 +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
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(
answer
? new window.Date(`${month}.${day}.${year}`)
: new window.Date()
)}
onChange={async (date) => {
2023-12-16 14:55:56 +00:00
if (!date) {
return;
}
2023-12-29 00:58:19 +00:00
try {
await sendAnswer({
questionId: currentQuestion.id,
body: new window.Date(date.toDate()).toLocaleDateString("ru-RU", {
2023-12-16 14:55:56 +00:00
year: "numeric",
month: "2-digit",
day: "2-digit",
}),
//@ts-ignore
qid: settings.qid
})
2023-12-29 00:58:19 +00:00
updateAnswer(
currentQuestion.id,
String(
new window.Date(date.toDate()).toLocaleDateString("ru-RU", {
year: "numeric",
month: "2-digit",
day: "2-digit",
})
)
);
} catch (e) {
enqueueSnackbar("ответ не был засчитан")
}
2023-12-29 00:58:19 +00:00
2023-12-16 14:55:56 +00:00
}}
slotProps={{
openPickerButton: {
sx: {
p: 0,
},
"data-cy": "open-datepicker",
},
2023-12-29 00:58:19 +00:00
layout: {
sx: {backgroundColor: theme.palette.background.default,}
}
2023-12-16 14:55:56 +00:00
}}
sx={{
2023-12-29 00:58:19 +00:00
2023-12-16 14:55:56 +00:00
"& .MuiInputBase-root": {
2023-12-29 00:58:19 +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",
},
},
2023-12-29 00:58:19 +00:00
2023-12-16 14:55:56 +00:00
}}
/>
</Box>
</Box>
);
2023-12-29 00:58:19 +00:00
};