2023-12-16 14:55:56 +00:00
|
|
|
import {
|
2024-01-30 16:49:33 +00:00
|
|
|
Box,
|
|
|
|
Checkbox,
|
|
|
|
FormControlLabel,
|
|
|
|
FormGroup,
|
2024-02-14 11:03:35 +00:00
|
|
|
TextField as MuiTextField,
|
2024-01-30 16:49:33 +00:00
|
|
|
Radio,
|
|
|
|
RadioGroup,
|
2024-02-14 11:03:35 +00:00
|
|
|
TextFieldProps,
|
2024-01-30 16:49:33 +00:00
|
|
|
Typography,
|
2024-02-14 11:03:35 +00:00
|
|
|
useTheme
|
2023-12-16 14:55:56 +00:00
|
|
|
} from "@mui/material";
|
2024-01-30 16:49:33 +00:00
|
|
|
import { FC, useEffect } from "react";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
|
|
|
import {
|
2024-01-30 16:49:33 +00:00
|
|
|
deleteAnswer,
|
|
|
|
updateAnswer,
|
|
|
|
updateOwnVariant,
|
|
|
|
useQuizViewStore
|
2024-02-12 10:58:51 +00:00
|
|
|
} from "@stores/quizView";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2024-01-30 16:49:33 +00:00
|
|
|
import { CheckboxIcon } from "@icons/Checkbox";
|
2023-12-16 14:55:56 +00:00
|
|
|
import RadioCheck from "@ui_kit/RadioCheck";
|
|
|
|
import RadioIcon from "@ui_kit/RadioIcon";
|
|
|
|
|
2023-12-17 21:28:57 +00:00
|
|
|
import { sendAnswer } from "@api/quizRelase";
|
2024-01-30 16:49:33 +00:00
|
|
|
|
2024-02-14 11:03:35 +00:00
|
|
|
import { useQuizData } from "@contexts/QuizDataContext";
|
|
|
|
import { useRootContainerSize } from "@contexts/RootContainerWidthContext";
|
2024-01-30 16:49:33 +00:00
|
|
|
import { quizThemes } from "@utils/themes/Publication/themePublication";
|
|
|
|
import { enqueueSnackbar } from "notistack";
|
|
|
|
import type { QuestionVariant } from "../../../model/questionTypes/shared";
|
|
|
|
import type { QuizQuestionVariant } from "../../../model/questionTypes/variant";
|
|
|
|
|
|
|
|
const TextField = MuiTextField as unknown as FC<TextFieldProps>;
|
2023-12-16 14:55:56 +00:00
|
|
|
|
|
|
|
type VariantProps = {
|
2024-01-30 16:49:33 +00:00
|
|
|
currentQuestion: QuizQuestionVariant;
|
2023-12-16 14:55:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type VariantItemProps = {
|
2024-01-30 16:49:33 +00:00
|
|
|
currentQuestion: QuizQuestionVariant;
|
|
|
|
variant: QuestionVariant;
|
|
|
|
answer: string | string[] | undefined;
|
|
|
|
index: number;
|
|
|
|
own?: boolean;
|
2023-12-16 14:55:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const Variant = ({ currentQuestion }: VariantProps) => {
|
2024-01-30 16:49:33 +00:00
|
|
|
const theme = useTheme();
|
2024-02-14 11:03:35 +00:00
|
|
|
const isMobile = useRootContainerSize() < 650;
|
2024-01-30 16:49:33 +00:00
|
|
|
const { answers, ownVariants } = useQuizViewStore();
|
|
|
|
const { answer } =
|
|
|
|
answers.find(
|
|
|
|
({ questionId }) => questionId === currentQuestion.id
|
|
|
|
) ?? {};
|
|
|
|
const ownVariant = ownVariants.find(
|
|
|
|
(variant) => variant.id === currentQuestion.id
|
|
|
|
);
|
|
|
|
|
|
|
|
const Group = currentQuestion.content.multi ? FormGroup : RadioGroup;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!ownVariant) {
|
|
|
|
updateOwnVariant(currentQuestion.id, "");
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Box>
|
2024-02-15 01:20:35 +00:00
|
|
|
<Typography variant="h5" color={theme.palette.text.primary} sx={{wordBreak: "break-word"}}>{currentQuestion.title}</Typography>
|
2024-02-11 15:35:45 +00:00
|
|
|
<Box sx={{ display: "flex", gap: "20px",
|
2024-02-13 22:50:30 +00:00
|
|
|
flexDirection: isMobile ? "column-reverse" : undefined, alignItems: isMobile ? "center" : undefined}}>
|
2024-01-30 16:49:33 +00:00
|
|
|
<Group
|
|
|
|
name={currentQuestion.id.toString()}
|
|
|
|
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}
|
2024-01-31 14:58:16 +00:00
|
|
|
// @ts-ignore
|
2024-01-30 16:49:33 +00:00
|
|
|
answer={answer}
|
|
|
|
index={index}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
{currentQuestion.content.own && ownVariant && (
|
|
|
|
<VariantItem
|
|
|
|
own
|
|
|
|
currentQuestion={currentQuestion}
|
|
|
|
variant={ownVariant.variant}
|
2024-01-31 14:58:16 +00:00
|
|
|
// @ts-ignore
|
2024-01-30 16:49:33 +00:00
|
|
|
answer={answer}
|
|
|
|
index={currentQuestion.content.variants.length + 2}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
</Group>
|
|
|
|
{currentQuestion.content.back && currentQuestion.content.back !== " " && (
|
|
|
|
<Box sx={{ maxWidth: "400px", width: "100%", height: "300px" }}>
|
|
|
|
<img
|
|
|
|
key={currentQuestion.id}
|
|
|
|
src={currentQuestion.content.back}
|
|
|
|
style={{ width: "100%", height: "100%", objectFit: "cover" }}
|
|
|
|
alt=""
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
);
|
2023-12-16 14:55:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const VariantItem = ({
|
2024-01-30 16:49:33 +00:00
|
|
|
currentQuestion,
|
|
|
|
variant,
|
|
|
|
answer,
|
|
|
|
index,
|
|
|
|
own = false,
|
2023-12-16 14:55:56 +00:00
|
|
|
}: VariantItemProps) => {
|
2024-01-30 16:49:33 +00:00
|
|
|
const theme = useTheme();
|
2024-02-14 11:03:35 +00:00
|
|
|
const { settings, quizId } = useQuizData();
|
2023-12-29 17:17:50 +00:00
|
|
|
|
2024-01-30 16:49:33 +00:00
|
|
|
return (
|
|
|
|
<FormControlLabel
|
|
|
|
key={variant.id}
|
|
|
|
sx={{
|
|
|
|
margin: "0",
|
|
|
|
borderRadius: "12px",
|
|
|
|
color: theme.palette.text.primary,
|
|
|
|
padding: "15px",
|
|
|
|
border: `1px solid`,
|
|
|
|
borderColor: answer === variant.id
|
|
|
|
? theme.palette.primary.main
|
|
|
|
: "#9A9AAF",
|
|
|
|
backgroundColor: quizThemes[settings.cfg.theme].isLight
|
|
|
|
? "white"
|
|
|
|
: theme.palette.background.default,
|
|
|
|
display: "flex",
|
|
|
|
maxWidth: "685px",
|
|
|
|
maxHeight: "85px",
|
|
|
|
justifyContent: "space-between",
|
|
|
|
width: "100%",
|
|
|
|
"&.MuiFormControl-root": {
|
|
|
|
width: "100%",
|
|
|
|
},
|
|
|
|
"& .MuiFormControlLabel-label": {
|
2024-02-13 22:45:27 +00:00
|
|
|
wordBreak: "break-word",
|
|
|
|
height: variant.answer.length <= 60 ? undefined : "60px",
|
|
|
|
overflow: "auto",
|
2024-01-30 16:49:33 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
value={index}
|
|
|
|
labelPlacement="start"
|
|
|
|
control={
|
|
|
|
currentQuestion.content.multi ?
|
|
|
|
<Checkbox
|
|
|
|
checked={!!answer?.includes(variant.id)}
|
|
|
|
checkedIcon={<CheckboxIcon checked color={theme.palette.primary.main} />}
|
|
|
|
icon={<CheckboxIcon />}
|
|
|
|
/>
|
|
|
|
:
|
|
|
|
<Radio checkedIcon={<RadioCheck color={theme.palette.primary.main} />} icon={<RadioIcon />} />
|
|
|
|
}
|
|
|
|
label={own ? <TextField label="Другое..." /> : variant.answer}
|
|
|
|
onClick={async (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
const variantId = currentQuestion.content.variants[index].id;
|
2024-02-14 11:03:35 +00:00
|
|
|
console.log(answer);
|
2024-01-30 16:49:33 +00:00
|
|
|
|
|
|
|
if (currentQuestion.content.multi) {
|
|
|
|
const currentAnswer = typeof answer !== "string" ? answer || [] : [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
await sendAnswer({
|
|
|
|
questionId: currentQuestion.id,
|
|
|
|
body: currentAnswer.includes(variantId)
|
|
|
|
? currentAnswer?.filter((item) => item !== variantId)
|
|
|
|
: [...currentAnswer, variantId],
|
2024-02-14 11:03:35 +00:00
|
|
|
qid: quizId,
|
2024-02-02 14:35:02 +00:00
|
|
|
});
|
2024-01-30 16:49:33 +00:00
|
|
|
|
|
|
|
updateAnswer(
|
|
|
|
currentQuestion.id,
|
|
|
|
currentAnswer.includes(variantId)
|
|
|
|
? currentAnswer?.filter((item) => item !== variantId)
|
2024-02-11 18:04:30 +00:00
|
|
|
: [...currentAnswer, variantId],
|
|
|
|
currentQuestion.content.variants[index].points || 0
|
2024-01-30 16:49:33 +00:00
|
|
|
);
|
|
|
|
} catch (e) {
|
2024-02-14 11:03:35 +00:00
|
|
|
console.log(e);
|
2024-02-02 14:35:02 +00:00
|
|
|
enqueueSnackbar("ответ не был засчитан");
|
2024-01-30 16:49:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await sendAnswer({
|
|
|
|
questionId: currentQuestion.id,
|
|
|
|
body: currentQuestion.content.variants[index].answer,
|
2024-02-14 11:03:35 +00:00
|
|
|
qid: quizId,
|
2024-02-02 14:35:02 +00:00
|
|
|
});
|
2024-01-30 16:49:33 +00:00
|
|
|
|
2024-02-11 18:04:30 +00:00
|
|
|
updateAnswer(currentQuestion.id, variantId,
|
|
|
|
answer === variantId ? 0
|
|
|
|
:
|
|
|
|
currentQuestion.content.variants[index].points || 0
|
|
|
|
);
|
2024-01-30 16:49:33 +00:00
|
|
|
|
|
|
|
} catch (e) {
|
2024-02-14 11:03:35 +00:00
|
|
|
console.log(e);
|
2024-02-02 14:35:02 +00:00
|
|
|
enqueueSnackbar("ответ не был засчитан");
|
2024-01-30 16:49:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (answer === variantId) {
|
|
|
|
try {
|
|
|
|
|
|
|
|
await sendAnswer({
|
|
|
|
questionId: currentQuestion.id,
|
|
|
|
body: "",
|
2024-02-14 11:03:35 +00:00
|
|
|
qid: quizId,
|
2024-02-02 14:35:02 +00:00
|
|
|
});
|
2024-01-30 16:49:33 +00:00
|
|
|
|
|
|
|
} catch (e) {
|
2024-02-14 11:03:35 +00:00
|
|
|
console.log(e);
|
2024-02-02 14:35:02 +00:00
|
|
|
enqueueSnackbar("ответ не был засчитан");
|
2024-01-30 16:49:33 +00:00
|
|
|
}
|
|
|
|
deleteAnswer(currentQuestion.id);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
2024-01-19 11:46:17 +00:00
|
|
|
};
|