frontPanel/src/pages/ViewPublicationPage/questions/Variant.tsx

129 lines
3.8 KiB
TypeScript
Raw Normal View History

2023-11-30 17:39:57 +00:00
import {
Box,
Typography,
RadioGroup,
2023-12-12 12:48:57 +00:00
FormGroup,
2023-11-30 17:39:57 +00:00
FormControlLabel,
Radio,
2023-12-12 12:48:57 +00:00
Checkbox,
2023-11-30 17:39:57 +00:00
useTheme,
} from "@mui/material";
2023-12-11 14:26:17 +00:00
import { useQuizViewStore, updateAnswer, deleteAnswer } from "@root/quizView";
2023-11-30 17:39:57 +00:00
import RadioCheck from "@ui_kit/RadioCheck";
import RadioIcon from "@ui_kit/RadioIcon";
import type { QuizQuestionVariant } from "../../../model/questionTypes/variant";
type VariantProps = {
stepNumber: number;
currentQuestion: QuizQuestionVariant;
2023-11-30 17:39:57 +00:00
};
export const Variant = ({ currentQuestion }: VariantProps) => {
2023-11-30 17:39:57 +00:00
const { answers } = useQuizViewStore();
const theme = useTheme();
2023-12-11 13:07:41 +00:00
const { answer } =
answers.find(
({ questionId }) => questionId === currentQuestion.content.id
) ?? {};
2023-11-30 17:39:57 +00:00
2023-12-12 12:48:57 +00:00
const Group = currentQuestion.content.multi ? FormGroup : RadioGroup;
2023-11-30 17:39:57 +00:00
return (
<Box>
<Typography variant="h5">{currentQuestion.title}</Typography>
2023-11-30 17:39:57 +00:00
<Box sx={{ display: "flex" }}>
2023-12-12 12:48:57 +00:00
<Group
name={currentQuestion.id}
2023-12-11 13:07:41 +00:00
value={currentQuestion.content.variants.findIndex(
({ id }) => answer === id
)}
2023-11-30 17:39:57 +00:00
sx={{
display: "flex",
flexWrap: "wrap",
flexDirection: "row",
justifyContent: "space-between",
flexBasis: "100%",
marginTop: "20px",
}}
>
2023-12-11 13:07:41 +00:00
<Box
sx={{
display: "flex",
flexDirection: "row",
flexWrap: "wrap",
width: "100%",
gap: "20px",
}}
>
2023-12-11 14:26:17 +00:00
{currentQuestion.content.variants.map((variant, index) => (
2023-11-30 17:39:57 +00:00
<FormControlLabel
2023-12-11 14:26:17 +00:00
key={variant.id}
2023-11-30 17:39:57 +00:00
sx={{
margin: "0",
borderRadius: "12px",
2023-11-30 17:39:57 +00:00
padding: "15px",
border: `1px solid ${theme.palette.grey2.main}`,
display: "flex",
maxWidth: "685px",
justifyContent: "space-between",
2023-12-11 13:07:41 +00:00
width: "100%",
2023-11-30 17:39:57 +00:00
}}
value={index}
labelPlacement="start"
2023-11-30 17:39:57 +00:00
control={
2023-12-12 12:48:57 +00:00
currentQuestion.content.multi ? (
<Checkbox
checked={!!answer?.includes(variant.id)}
checkedIcon={<RadioCheck />}
icon={<RadioIcon />}
/>
) : (
<Radio checkedIcon={<RadioCheck />} icon={<RadioIcon />} />
)
2023-11-30 17:39:57 +00:00
}
2023-12-11 14:26:17 +00:00
label={variant.answer}
onClick={(event) => {
event.preventDefault();
2023-12-12 12:48:57 +00:00
const variantId = currentQuestion.content.variants[index].id;
if (currentQuestion.content.multi) {
const currentAnswer =
typeof answer !== "string" ? answer || [] : [];
updateAnswer(
currentQuestion.content.id,
currentAnswer?.includes(variantId)
? currentAnswer?.filter((item) => item !== variantId)
: [...currentAnswer, variantId]
);
return;
}
2023-12-11 14:26:17 +00:00
2023-12-12 12:48:57 +00:00
updateAnswer(currentQuestion.content.id, variantId);
2023-12-11 14:26:17 +00:00
2023-12-12 12:48:57 +00:00
if (answer === variantId) {
2023-12-11 14:26:17 +00:00
deleteAnswer(currentQuestion.content.id);
}
}}
2023-11-30 17:39:57 +00:00
/>
))}
</Box>
2023-12-12 12:48:57 +00:00
</Group>
{currentQuestion.content.back && (
2023-11-30 17:39:57 +00:00
<Box sx={{ maxWidth: "400px", width: "100%", height: "300px" }}>
<img
src={currentQuestion.content.back}
2023-11-30 17:39:57 +00:00
style={{ width: "100%", height: "100%", objectFit: "cover" }}
alt=""
/>
</Box>
)}
</Box>
</Box>
);
};