2024-05-31 16:41:18 +00:00
|
|
|
import { Box, FormGroup, RadioGroup, Typography, useTheme } from "@mui/material";
|
2024-11-14 21:29:28 +00:00
|
|
|
import { useEffect, useMemo } from "react";
|
2024-04-23 14:45:49 +00:00
|
|
|
|
|
|
|
import { VariantItem } from "./VariantItem";
|
|
|
|
|
|
|
|
import { useRootContainerSize } from "@contexts/RootContainerWidthContext";
|
2024-06-29 09:32:16 +00:00
|
|
|
import { useQuizViewStore } from "@stores/quizView";
|
2024-04-23 14:45:49 +00:00
|
|
|
|
|
|
|
import type { QuizQuestionVariant } from "@model/questionTypes/variant";
|
2024-05-01 10:30:05 +00:00
|
|
|
import moment from "moment";
|
2024-04-23 14:45:49 +00:00
|
|
|
|
|
|
|
type VariantProps = {
|
|
|
|
currentQuestion: QuizQuestionVariant;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Variant = ({ currentQuestion }: VariantProps) => {
|
|
|
|
const theme = useTheme();
|
|
|
|
const isMobile = useRootContainerSize() < 650;
|
2024-11-14 21:29:28 +00:00
|
|
|
const isTablet = useRootContainerSize() < 850;
|
2024-06-29 09:32:16 +00:00
|
|
|
const answers = useQuizViewStore((state) => state.answers);
|
|
|
|
const ownVariants = useQuizViewStore((state) => state.ownVariants);
|
|
|
|
const updateOwnVariant = useQuizViewStore((state) => state.updateOwnVariant);
|
2024-04-23 14:45:49 +00:00
|
|
|
|
2024-06-29 09:32:16 +00:00
|
|
|
const answer = answers.find(({ questionId }) => questionId === currentQuestion.id)?.answer;
|
2024-05-31 16:41:18 +00:00
|
|
|
const ownVariant = ownVariants.find((variant) => variant.id === currentQuestion.id);
|
2024-04-23 14:45:49 +00:00
|
|
|
|
|
|
|
const Group = currentQuestion.content.multi ? FormGroup : RadioGroup;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!ownVariant) {
|
|
|
|
updateOwnVariant(currentQuestion.id, "");
|
|
|
|
}
|
2024-06-29 09:32:16 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2024-04-23 14:45:49 +00:00
|
|
|
}, []);
|
|
|
|
|
2024-11-14 21:29:28 +00:00
|
|
|
const choiceImgUrlQuestion = useMemo(() => {
|
2024-11-16 19:22:50 +00:00
|
|
|
if (
|
|
|
|
currentQuestion.content.editedUrlImagesList !== undefined &&
|
|
|
|
currentQuestion.content.editedUrlImagesList !== null
|
|
|
|
) {
|
2024-11-14 21:29:28 +00:00
|
|
|
return currentQuestion.content.editedUrlImagesList[isMobile ? "mobile" : isTablet ? "tablet" : "desktop"];
|
|
|
|
} else {
|
|
|
|
return currentQuestion.content.back;
|
|
|
|
}
|
|
|
|
}, [currentQuestion]);
|
2024-05-01 10:30:05 +00:00
|
|
|
if (moment.isMoment(answer)) throw new Error("Answer is Moment in Variant question");
|
2024-04-23 14:45:49 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Box>
|
2024-06-29 09:32:16 +00:00
|
|
|
<Typography
|
|
|
|
variant="h5"
|
|
|
|
color={theme.palette.text.primary}
|
|
|
|
sx={{ wordBreak: "break-word" }}
|
|
|
|
>
|
2024-04-23 14:45:49 +00:00
|
|
|
{currentQuestion.title}
|
|
|
|
</Typography>
|
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
display: "flex",
|
|
|
|
gap: "20px",
|
|
|
|
flexDirection: isMobile ? "column-reverse" : undefined,
|
|
|
|
alignItems: isMobile ? "center" : undefined,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Group
|
|
|
|
name={currentQuestion.id.toString()}
|
2024-05-31 16:41:18 +00:00
|
|
|
value={currentQuestion.content.variants.findIndex(({ id }) => answer === id)}
|
2024-04-23 14:45:49 +00:00
|
|
|
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",
|
|
|
|
}}
|
|
|
|
>
|
2024-09-12 11:14:54 +00:00
|
|
|
{currentQuestion.content.variants
|
|
|
|
.filter((v) => {
|
|
|
|
if (!v.isOwn) return true;
|
|
|
|
return v.isOwn && currentQuestion.content.own;
|
|
|
|
})
|
|
|
|
.map((variant, index) => (
|
|
|
|
<VariantItem
|
|
|
|
key={variant.id}
|
|
|
|
questionId={currentQuestion.id}
|
|
|
|
isMulti={currentQuestion.content.multi}
|
|
|
|
variant={variant}
|
|
|
|
answer={answer}
|
|
|
|
index={index}
|
2024-09-12 15:55:25 +00:00
|
|
|
own={Boolean(variant.isOwn)}
|
2024-09-12 11:14:54 +00:00
|
|
|
questionLargeCheck={currentQuestion.content.largeCheck}
|
|
|
|
ownPlaceholder={currentQuestion.content?.ownPlaceholder || ""}
|
|
|
|
/>
|
|
|
|
))}
|
2024-04-23 14:45:49 +00:00
|
|
|
</Box>
|
|
|
|
</Group>
|
2024-11-14 21:29:28 +00:00
|
|
|
{choiceImgUrlQuestion && choiceImgUrlQuestion !== " " && choiceImgUrlQuestion !== null && (
|
2024-12-21 18:44:57 +00:00
|
|
|
<Box
|
|
|
|
sx={{ maxWidth: "400px", width: "100%", height: "300px" }}
|
|
|
|
onClick={(event) => event.preventDefault()}
|
|
|
|
>
|
2024-05-31 16:41:18 +00:00
|
|
|
<img
|
|
|
|
key={currentQuestion.id}
|
2024-11-14 21:29:28 +00:00
|
|
|
src={choiceImgUrlQuestion}
|
2024-05-31 16:41:18 +00:00
|
|
|
style={{ width: "100%", height: "100%", objectFit: "cover" }}
|
|
|
|
alt=""
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
)}
|
2024-04-23 14:45:49 +00:00
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
};
|