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

80 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
2024-01-19 11:46:17 +00:00
import { useQuizViewStore, updateAnswer, deleteAnswer } from "@stores/quizView/store";
2023-12-16 14:55:56 +00:00
import type { QuizQuestionSelect } from "../../../model/questionTypes/select";
import { enqueueSnackbar } from "notistack";
import { sendAnswer } from "@api/quizRelase";
2024-02-02 14:35:02 +00:00
import { useQuizData } from "@utils/hooks/useQuizData";
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();
2023-12-29 00:58:19 +00:00
2024-02-02 14:35:02 +00:00
const { settings } = useQuizData();
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: settings.qid
});
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: settings.qid
});
2024-02-02 14:35:02 +00:00
updateAnswer(currentQuestion.id, String(value));
2024-02-02 14:35:02 +00:00
} catch (e) {
enqueueSnackbar("ответ не был засчитан");
}
}}
/>
</Box>
</Box>
);
2023-12-16 14:55:56 +00:00
};