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

68 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-12-16 14:55:56 +00:00
import { Box, Typography } from "@mui/material";
import { Select as SelectComponent } from "../tools//Select";
2023-12-16 14:55:56 +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";
import { enqueueSnackbar } from "notistack";
import { sendAnswer } from "@api/quizRelase";
2023-12-16 14:55:56 +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) => {
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
) ?? {};
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)}
onChange={async(_, value) => {
2023-12-16 14:55:56 +00:00
if (value < 0) {
deleteAnswer(currentQuestion.id);
2023-12-16 14:55:56 +00:00
return;
}
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>
);
};