frontAnswerer/lib/components/ViewPublicationPage/questions/Select.tsx

79 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-12-29 00:58:19 +00:00
import { Box, Typography, useTheme } from "@mui/material";
2023-12-16 14:55:56 +00:00
import { Select as SelectComponent } from "../tools//Select";
2023-12-16 14:55:56 +00:00
import { deleteAnswer, updateAnswer, useQuizViewStore } from "@stores/quizView";
2023-12-16 14:55:56 +00:00
import { sendAnswer } from "@api/quizRelase";
import { enqueueSnackbar } from "notistack";
import type { QuizQuestionSelect } from "../../../model/questionTypes/select";
import { useQuizData } from "@contexts/QuizDataContext";
2023-12-16 14:55:56 +00:00
type SelectProps = {
2024-02-02 14:35:02 +00:00
currentQuestion: QuizQuestionSelect;
2023-12-16 14:55:56 +00:00
};
export const Select = ({ currentQuestion }: SelectProps) => {
2024-02-02 14:35:02 +00:00
const theme = useTheme();
const { quizId } = useQuizData();
2024-02-02 14:35:02 +00:00
const { answers } = useQuizViewStore();
const { answer } =
answers.find(
({ questionId }) => questionId === currentQuestion.id
) ?? {};
2023-12-16 14:55:56 +00:00
2024-02-02 14:35:02 +00:00
return (
<Box>
<Typography variant="h5" color={theme.palette.text.primary}>{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)}
colorMain={theme.palette.primary.main}
onChange={async (_, value) => {
if (value < 0) {
deleteAnswer(currentQuestion.id);
try {
2024-02-02 14:35:02 +00:00
await sendAnswer({
questionId: currentQuestion.id,
body: "",
qid: quizId,
2024-02-02 14:35:02 +00:00
});
2023-12-16 14:55:56 +00:00
2024-02-02 14:35:02 +00:00
} catch (e) {
enqueueSnackbar("ответ не был засчитан");
}
return;
}
2024-02-02 14:35:02 +00:00
try {
2024-02-02 14:35:02 +00:00
await sendAnswer({
questionId: currentQuestion.id,
body: String(currentQuestion.content.variants[Number(value)].answer),
qid: quizId,
2024-02-02 14:35:02 +00:00
});
updateAnswer(currentQuestion.id, String(value), 0);
2024-02-02 14:35:02 +00:00
} catch (e) {
enqueueSnackbar("ответ не был засчитан");
}
}}
/>
</Box>
</Box>
);
2023-12-16 14:55:56 +00:00
};