2023-12-16 14:55:56 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
import {
|
|
|
|
Box,
|
|
|
|
Typography,
|
|
|
|
RadioGroup,
|
|
|
|
FormGroup,
|
|
|
|
FormControlLabel,
|
|
|
|
Radio,
|
|
|
|
Checkbox,
|
2023-12-17 13:22:21 +00:00
|
|
|
TextField,
|
2023-12-16 14:55:56 +00:00
|
|
|
useTheme,
|
|
|
|
} from "@mui/material";
|
|
|
|
|
|
|
|
import {
|
|
|
|
useQuizViewStore,
|
|
|
|
updateAnswer,
|
|
|
|
deleteAnswer,
|
|
|
|
updateOwnVariant,
|
|
|
|
deleteOwnVariant,
|
2023-12-17 13:22:21 +00:00
|
|
|
} from "@root/quizView/store";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
|
|
|
import RadioCheck from "@ui_kit/RadioCheck";
|
|
|
|
import RadioIcon from "@ui_kit/RadioIcon";
|
|
|
|
import { CheckboxIcon } from "@icons/Checkbox";
|
|
|
|
|
|
|
|
import type { QuizQuestionVariant } from "../../../model/questionTypes/variant";
|
|
|
|
import type { QuestionVariant } from "../../../model/questionTypes/shared";
|
2023-12-17 21:28:57 +00:00
|
|
|
import { enqueueSnackbar } from "notistack";
|
|
|
|
import { sendAnswer } from "@api/quizRelase";
|
|
|
|
import { useQuestionsStore } from "@root/quizData/store"
|
2023-12-16 14:55:56 +00:00
|
|
|
|
|
|
|
type VariantProps = {
|
|
|
|
stepNumber: number;
|
|
|
|
currentQuestion: QuizQuestionVariant;
|
|
|
|
};
|
|
|
|
|
|
|
|
type VariantItemProps = {
|
|
|
|
currentQuestion: QuizQuestionVariant;
|
|
|
|
variant: QuestionVariant;
|
|
|
|
answer: string | string[] | undefined;
|
|
|
|
index: number;
|
|
|
|
own?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Variant = ({ currentQuestion }: VariantProps) => {
|
2023-12-17 21:28:57 +00:00
|
|
|
const { settings } = useQuestionsStore()
|
2023-12-16 14:55:56 +00:00
|
|
|
const { answers, ownVariants } = 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
|
|
|
) ?? {};
|
|
|
|
const ownVariant = ownVariants.find(
|
2023-12-17 13:22:21 +00:00
|
|
|
(variant) => variant.id === currentQuestion.id
|
2023-12-16 14:55:56 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const Group = currentQuestion.content.multi ? FormGroup : RadioGroup;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!ownVariant) {
|
2023-12-17 13:22:21 +00:00
|
|
|
updateOwnVariant(currentQuestion.id, "");
|
2023-12-16 14:55:56 +00:00
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Box>
|
|
|
|
<Typography variant="h5">{currentQuestion.title}</Typography>
|
|
|
|
<Box sx={{ display: "flex" }}>
|
|
|
|
<Group
|
2023-12-17 13:22:21 +00:00
|
|
|
name={currentQuestion.id.toString()}
|
2023-12-16 14:55:56 +00:00
|
|
|
value={currentQuestion.content.variants.findIndex(
|
|
|
|
({ id }) => answer === id
|
|
|
|
)}
|
|
|
|
sx={{
|
|
|
|
display: "flex",
|
|
|
|
flexWrap: "wrap",
|
|
|
|
flexDirection: "row",
|
|
|
|
justifyContent: "space-between",
|
|
|
|
flexBasis: "100%",
|
|
|
|
marginTop: "20px",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
display: "flex",
|
|
|
|
flexDirection: "row",
|
|
|
|
flexWrap: "wrap",
|
|
|
|
width: "100%",
|
|
|
|
gap: "20px",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{currentQuestion.content.variants.map((variant, index) => (
|
|
|
|
<VariantItem
|
|
|
|
key={variant.id}
|
|
|
|
currentQuestion={currentQuestion}
|
|
|
|
variant={variant}
|
|
|
|
answer={answer}
|
|
|
|
index={index}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
{currentQuestion.content.own && ownVariant && (
|
|
|
|
<VariantItem
|
|
|
|
own
|
|
|
|
currentQuestion={currentQuestion}
|
|
|
|
variant={ownVariant.variant}
|
|
|
|
answer={answer}
|
|
|
|
index={currentQuestion.content.variants.length + 2}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
</Group>
|
|
|
|
{currentQuestion.content.back && (
|
|
|
|
<Box sx={{ maxWidth: "400px", width: "100%", height: "300px" }}>
|
|
|
|
<img
|
|
|
|
src={currentQuestion.content.back}
|
|
|
|
style={{ width: "100%", height: "100%", objectFit: "cover" }}
|
|
|
|
alt=""
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const VariantItem = ({
|
|
|
|
currentQuestion,
|
|
|
|
variant,
|
|
|
|
answer,
|
|
|
|
index,
|
|
|
|
own = false,
|
|
|
|
}: VariantItemProps) => {
|
|
|
|
const theme = useTheme();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormControlLabel
|
|
|
|
key={variant.id}
|
|
|
|
sx={{
|
|
|
|
margin: "0",
|
|
|
|
borderRadius: "12px",
|
|
|
|
padding: "15px",
|
|
|
|
border: `1px solid ${theme.palette.grey2.main}`,
|
|
|
|
display: "flex",
|
|
|
|
maxWidth: "685px",
|
|
|
|
justifyContent: "space-between",
|
|
|
|
width: "100%",
|
|
|
|
"&.MuiFormControl-root": {
|
|
|
|
width: "100%",
|
2023-12-17 13:22:21 +00:00
|
|
|
|
|
|
|
}
|
2023-12-16 14:55:56 +00:00
|
|
|
}}
|
|
|
|
value={index}
|
|
|
|
labelPlacement="start"
|
|
|
|
control={
|
|
|
|
currentQuestion.content.multi ? (
|
|
|
|
<Checkbox
|
|
|
|
checked={!!answer?.includes(variant.id)}
|
|
|
|
checkedIcon={<CheckboxIcon checked />}
|
|
|
|
icon={<CheckboxIcon />}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Radio checkedIcon={<RadioCheck />} icon={<RadioIcon />} />
|
|
|
|
)
|
|
|
|
}
|
2023-12-17 13:22:21 +00:00
|
|
|
//@ts-ignore
|
|
|
|
label={own ? <TextField label="Другое..." /> : variant.answer}
|
2023-12-17 21:28:57 +00:00
|
|
|
onClick={async(event) => {
|
2023-12-16 14:55:56 +00:00
|
|
|
event.preventDefault();
|
|
|
|
const variantId = currentQuestion.content.variants[index].id;
|
|
|
|
|
|
|
|
if (currentQuestion.content.multi) {
|
|
|
|
const currentAnswer = typeof answer !== "string" ? answer || [] : [];
|
|
|
|
|
2023-12-17 21:28:57 +00:00
|
|
|
try {
|
|
|
|
|
|
|
|
await sendAnswer({
|
|
|
|
questionId: currentQuestion.id,
|
|
|
|
body: currentAnswer.includes(variantId)
|
2023-12-16 14:55:56 +00:00
|
|
|
? currentAnswer?.filter((item) => item !== variantId)
|
2023-12-17 21:28:57 +00:00
|
|
|
: [...currentAnswer, variantId],
|
|
|
|
//@ts-ignore
|
|
|
|
qid: settings.qid
|
|
|
|
})
|
|
|
|
|
|
|
|
updateAnswer(
|
|
|
|
currentQuestion.id,
|
|
|
|
currentAnswer.includes(variantId)
|
|
|
|
? currentAnswer?.filter((item) => item !== variantId)
|
|
|
|
: [...currentAnswer, variantId]
|
|
|
|
);
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
|
|
|
enqueueSnackbar("ответ не был засчитан")
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:55:56 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-17 21:28:57 +00:00
|
|
|
try {
|
|
|
|
|
|
|
|
await sendAnswer({
|
|
|
|
questionId: currentQuestion.id,
|
|
|
|
body: variantId,
|
|
|
|
//@ts-ignore
|
|
|
|
qid: settings.qid
|
|
|
|
})
|
|
|
|
|
|
|
|
updateAnswer(currentQuestion.id, variantId);
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
|
|
|
enqueueSnackbar("ответ не был засчитан")
|
|
|
|
}
|
2023-12-16 14:55:56 +00:00
|
|
|
|
|
|
|
if (answer === variantId) {
|
2023-12-17 13:22:21 +00:00
|
|
|
deleteAnswer(currentQuestion.id);
|
2023-12-16 14:55:56 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|