2023-12-29 00:58:19 +00:00
|
|
|
import { Box, Typography, useTheme } from "@mui/material";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2023-12-17 13:22:21 +00:00
|
|
|
import { Select as SelectComponent } from "../tools//Select";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2024-04-03 12:42:12 +00:00
|
|
|
import { useQuizViewStore } from "@stores/quizView";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2023-12-17 21:28:57 +00:00
|
|
|
import { sendAnswer } from "@api/quizRelase";
|
2024-02-08 13:42:31 +00:00
|
|
|
import { enqueueSnackbar } from "notistack";
|
|
|
|
import type { QuizQuestionSelect } from "../../../model/questionTypes/select";
|
2024-02-14 11:03:35 +00:00
|
|
|
import { useQuizData } from "@contexts/QuizDataContext";
|
2024-02-19 14:09:27 +00:00
|
|
|
import { useState } from "react";
|
2024-03-05 14:29:52 +00:00
|
|
|
import { quizThemes } from "@utils/themes/Publication/themePublication";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
|
|
|
type SelectProps = {
|
2024-04-03 12:42:12 +00:00
|
|
|
currentQuestion: QuizQuestionSelect;
|
2023-12-16 14:55:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const Select = ({ currentQuestion }: SelectProps) => {
|
2024-04-03 12:42:12 +00:00
|
|
|
const theme = useTheme();
|
|
|
|
const { quizId, settings, preview } = useQuizData();
|
|
|
|
const [isSending, setIsSending] = useState<boolean>(false);
|
|
|
|
const answers = useQuizViewStore(state => state.answers);
|
|
|
|
const deleteAnswer = useQuizViewStore(state => state.deleteAnswer);
|
|
|
|
const updateAnswer = useQuizViewStore(state => state.updateAnswer);
|
|
|
|
const { answer } =
|
|
|
|
answers.find(({ questionId }) => questionId === currentQuestion.id) ?? {};
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2024-04-03 12:42:12 +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",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<SelectComponent
|
|
|
|
disabled={isSending}
|
|
|
|
placeholder={currentQuestion.content.default}
|
|
|
|
activeItemIndex={answer ? Number(answer) : -1}
|
|
|
|
items={currentQuestion.content.variants.map(({ answer }) => answer)}
|
|
|
|
colorMain={theme.palette.primary.main}
|
|
|
|
sx={{
|
|
|
|
"& .MuiSelect-select.MuiSelect-outlined": { zIndex: 1 },
|
|
|
|
"& .MuiOutlinedInput-notchedOutline": {
|
|
|
|
background: settings.cfg.design
|
|
|
|
? quizThemes[settings.cfg.theme].isLight
|
|
|
|
? "#F2F3F7"
|
|
|
|
: "rgba(255,255,255, 0.3)"
|
|
|
|
: "transparent",
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
onChange={async (_, value) => {
|
|
|
|
setIsSending(true);
|
|
|
|
if (value < 0) {
|
|
|
|
deleteAnswer(currentQuestion.id);
|
|
|
|
try {
|
|
|
|
await sendAnswer({
|
|
|
|
questionId: currentQuestion.id,
|
|
|
|
body: "",
|
|
|
|
qid: quizId,
|
|
|
|
preview
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
enqueueSnackbar("ответ не был засчитан");
|
|
|
|
}
|
|
|
|
return setIsSending(false);
|
|
|
|
}
|
2024-01-30 16:49:33 +00:00
|
|
|
|
2024-04-03 12:42:12 +00:00
|
|
|
try {
|
|
|
|
await sendAnswer({
|
|
|
|
questionId: currentQuestion.id,
|
|
|
|
body: String(
|
|
|
|
currentQuestion.content.variants[Number(value)].answer
|
|
|
|
),
|
|
|
|
qid: quizId,
|
|
|
|
preview
|
|
|
|
});
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2024-04-03 12:42:12 +00:00
|
|
|
updateAnswer(currentQuestion.id, String(value), 0);
|
|
|
|
} catch (e) {
|
|
|
|
enqueueSnackbar("ответ не был засчитан");
|
|
|
|
}
|
2023-12-17 21:28:57 +00:00
|
|
|
|
2024-04-03 12:42:12 +00:00
|
|
|
setIsSending(false);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
);
|
2023-12-16 14:55:56 +00:00
|
|
|
};
|