112 lines
3.5 KiB
TypeScript
112 lines
3.5 KiB
TypeScript
import { Box, FormGroup, RadioGroup, Typography, useTheme } from "@mui/material";
|
|
import { useEffect } from "react";
|
|
|
|
import { VariantItem } from "./VariantItem";
|
|
|
|
import { useRootContainerSize } from "@contexts/RootContainerWidthContext";
|
|
import { useQuizViewStore } from "@stores/quizView";
|
|
|
|
import type { QuizQuestionVariant } from "@model/questionTypes/variant";
|
|
import moment from "moment";
|
|
|
|
type VariantProps = {
|
|
currentQuestion: QuizQuestionVariant;
|
|
};
|
|
|
|
export const Variant = ({ currentQuestion }: VariantProps) => {
|
|
const theme = useTheme();
|
|
const isMobile = useRootContainerSize() < 650;
|
|
const answers = useQuizViewStore((state) => state.answers);
|
|
const ownVariants = useQuizViewStore((state) => state.ownVariants);
|
|
const updateOwnVariant = useQuizViewStore((state) => state.updateOwnVariant);
|
|
|
|
const answer = answers.find(({ questionId }) => questionId === currentQuestion.id)?.answer;
|
|
const ownVariant = ownVariants.find((variant) => variant.id === currentQuestion.id);
|
|
|
|
const Group = currentQuestion.content.multi ? FormGroup : RadioGroup;
|
|
|
|
useEffect(() => {
|
|
if (!ownVariant) {
|
|
updateOwnVariant(currentQuestion.id, "");
|
|
}
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
|
|
if (moment.isMoment(answer)) throw new Error("Answer is Moment in Variant question");
|
|
|
|
return (
|
|
<Box>
|
|
<Typography
|
|
variant="h5"
|
|
color={theme.palette.text.primary}
|
|
sx={{ wordBreak: "break-word" }}
|
|
>
|
|
{currentQuestion.title}
|
|
</Typography>
|
|
<Box
|
|
sx={{
|
|
display: "flex",
|
|
gap: "20px",
|
|
flexDirection: isMobile ? "column-reverse" : undefined,
|
|
alignItems: isMobile ? "center" : undefined,
|
|
}}
|
|
>
|
|
<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",
|
|
width: isMobile ? "100%" : undefined,
|
|
}}
|
|
>
|
|
<Box
|
|
sx={{
|
|
display: "flex",
|
|
flexDirection: "row",
|
|
flexWrap: "wrap",
|
|
width: "100%",
|
|
gap: "20px",
|
|
}}
|
|
>
|
|
{currentQuestion.content.variants.map((variant, index) => (
|
|
<VariantItem
|
|
key={variant.id}
|
|
questionId={currentQuestion.id}
|
|
isMulti={currentQuestion.content.multi}
|
|
variant={variant}
|
|
answer={answer}
|
|
index={index}
|
|
/>
|
|
))}
|
|
{currentQuestion.content.own && ownVariant && (
|
|
<VariantItem
|
|
own
|
|
questionId={currentQuestion.id}
|
|
isMulti={currentQuestion.content.multi}
|
|
variant={ownVariant.variant}
|
|
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>
|
|
);
|
|
};
|