2023-12-16 14:55:56 +00:00
|
|
|
import { Box, Typography } from "@mui/material";
|
|
|
|
|
2023-12-17 13:22:21 +00:00
|
|
|
import { Select as SelectComponent } from "../tools//Select";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2023-12-17 13:22:21 +00:00
|
|
|
import { useQuizViewStore, updateAnswer, deleteAnswer } from "@root/quizView/store";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
|
|
|
import type { QuizQuestionSelect } from "../../../model/questionTypes/select";
|
2023-12-17 21:28:57 +00:00
|
|
|
import { enqueueSnackbar } from "notistack";
|
|
|
|
import { sendAnswer } from "@api/quizRelase";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2023-12-17 21:28:57 +00:00
|
|
|
import { useQuestionsStore } from "@root/quizData/store"
|
2023-12-16 14:55:56 +00:00
|
|
|
type SelectProps = {
|
|
|
|
currentQuestion: QuizQuestionSelect;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Select = ({ currentQuestion }: SelectProps) => {
|
2023-12-17 21:28:57 +00:00
|
|
|
const { settings } = useQuestionsStore()
|
2023-12-16 14:55:56 +00:00
|
|
|
const { answers } = useQuizViewStore();
|
|
|
|
const { answer } =
|
|
|
|
answers.find(
|
2023-12-17 13:22:21 +00:00
|
|
|
({ questionId }) => questionId === currentQuestion.id
|
2023-12-16 14:55:56 +00:00
|
|
|
) ?? {};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Box>
|
|
|
|
<Typography variant="h5">{currentQuestion.title}</Typography>
|
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
display: "flex",
|
|
|
|
flexDirection: "column",
|
|
|
|
width: "100%",
|
|
|
|
marginTop: "20px",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<SelectComponent
|
|
|
|
placeholder={currentQuestion.content.default}
|
|
|
|
activeItemIndex={answer ? Number(answer) : -1}
|
|
|
|
items={currentQuestion.content.variants.map(({ answer }) => answer)}
|
2023-12-17 21:28:57 +00:00
|
|
|
onChange={async(_, value) => {
|
2023-12-16 14:55:56 +00:00
|
|
|
if (value < 0) {
|
2023-12-17 13:22:21 +00:00
|
|
|
deleteAnswer(currentQuestion.id);
|
2023-12-16 14:55:56 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-17 21:28:57 +00:00
|
|
|
try {
|
|
|
|
|
|
|
|
await sendAnswer({
|
|
|
|
questionId: currentQuestion.id,
|
|
|
|
body: String(value),
|
|
|
|
//@ts-ignore
|
|
|
|
qid: settings.qid
|
|
|
|
})
|
|
|
|
|
|
|
|
updateAnswer(currentQuestion.id, String(value));
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
enqueueSnackbar("ответ не был засчитан")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-16 14:55:56 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
};
|