2024-11-14 21:29:28 +00:00
|
|
|
import { useEffect, useMemo, useState } from "react";
|
2024-04-23 14:45:49 +00:00
|
|
|
import { Box, RadioGroup, Typography, useTheme } from "@mui/material";
|
|
|
|
|
|
|
|
import { VarimgVariant } from "./VarimgVariant";
|
|
|
|
|
|
|
|
import { useQuizViewStore } from "@stores/quizView";
|
|
|
|
import { useRootContainerSize } from "@contexts/RootContainerWidthContext";
|
|
|
|
|
|
|
|
import BlankImage from "@icons/BlankImage";
|
|
|
|
|
|
|
|
import type { QuizQuestionVarImg } from "@model/questionTypes/varimg";
|
2024-09-12 08:43:50 +00:00
|
|
|
import moment from "moment";
|
2025-04-20 15:16:22 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2024-04-23 14:45:49 +00:00
|
|
|
|
|
|
|
type VarimgProps = {
|
|
|
|
currentQuestion: QuizQuestionVarImg;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Varimg = ({ currentQuestion }: VarimgProps) => {
|
|
|
|
const [isSending, setIsSending] = useState<boolean>(false);
|
|
|
|
const answers = useQuizViewStore((state) => state.answers);
|
2024-09-12 08:43:50 +00:00
|
|
|
const ownVariants = useQuizViewStore((state) => state.ownVariants);
|
|
|
|
const updateOwnVariant = useQuizViewStore((state) => state.updateOwnVariant);
|
2025-04-20 15:16:22 +00:00
|
|
|
const { t } = useTranslation();
|
2024-04-23 14:45:49 +00:00
|
|
|
|
|
|
|
const theme = useTheme();
|
|
|
|
const isMobile = useRootContainerSize() < 650;
|
2024-11-14 21:29:28 +00:00
|
|
|
const isTablet = useRootContainerSize() < 850;
|
2024-04-23 14:45:49 +00:00
|
|
|
|
2024-05-31 16:41:18 +00:00
|
|
|
const { answer } = answers.find(({ questionId }) => questionId === currentQuestion.id) ?? {};
|
2024-09-12 08:43:50 +00:00
|
|
|
const ownVariant = ownVariants.find((variant) => variant.id === currentQuestion.id);
|
2024-05-31 16:41:18 +00:00
|
|
|
const variant = currentQuestion.content.variants.find(({ id }) => answer === id);
|
2024-04-23 14:45:49 +00:00
|
|
|
|
2024-09-12 08:43:50 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!ownVariant) {
|
|
|
|
updateOwnVariant(currentQuestion.id, "");
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, []);
|
|
|
|
|
2024-11-14 21:29:28 +00:00
|
|
|
const choiceImgUrlAnswer = useMemo(() => {
|
|
|
|
if (variant !== undefined) {
|
2024-11-16 19:22:50 +00:00
|
|
|
if (variant.editedUrlImagesList !== undefined && variant.editedUrlImagesList !== null) {
|
2024-11-14 21:29:28 +00:00
|
|
|
return variant.editedUrlImagesList[isMobile ? "mobile" : isTablet ? "tablet" : "desktop"];
|
|
|
|
} else {
|
|
|
|
return variant.extendedText;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [variant]);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}, [variant]);
|
2024-09-12 08:43:50 +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",
|
|
|
|
marginTop: "20px",
|
|
|
|
flexDirection: isMobile ? "column-reverse" : undefined,
|
|
|
|
gap: "30px",
|
|
|
|
alignItems: isMobile ? "center" : undefined,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<RadioGroup
|
|
|
|
name={currentQuestion.id}
|
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%",
|
|
|
|
width: isMobile ? "100%" : undefined,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
display: "flex",
|
|
|
|
flexDirection: "column",
|
|
|
|
width: "100%",
|
|
|
|
gap: "20px",
|
|
|
|
"&:focus": { color: theme.palette.text.primary },
|
|
|
|
"&:active": { color: theme.palette.text.primary },
|
|
|
|
}}
|
|
|
|
>
|
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) => (
|
|
|
|
<VarimgVariant
|
|
|
|
key={variant.id}
|
|
|
|
questionId={currentQuestion.id}
|
|
|
|
variant={variant}
|
|
|
|
isSending={isSending}
|
|
|
|
setIsSending={setIsSending}
|
|
|
|
index={index}
|
|
|
|
questionLargeCheck={currentQuestion.content.largeCheck}
|
|
|
|
ownPlaceholder={currentQuestion.content?.ownPlaceholder || ""}
|
|
|
|
isMulti={Boolean(currentQuestion.content?.multi)}
|
|
|
|
answer={answer}
|
|
|
|
/>
|
|
|
|
))}
|
2024-04-23 14:45:49 +00:00
|
|
|
</Box>
|
|
|
|
</RadioGroup>
|
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
maxWidth: "450px",
|
|
|
|
width: "100%",
|
|
|
|
height: "450px",
|
|
|
|
border: "1px solid #9A9AAF",
|
|
|
|
borderRadius: "12px",
|
|
|
|
overflow: "hidden",
|
|
|
|
display: "flex",
|
|
|
|
alignItems: "center",
|
|
|
|
justifyContent: "center",
|
|
|
|
backgroundColor: "#9A9AAF30",
|
|
|
|
color: theme.palette.text.primary,
|
|
|
|
textAlign: "center",
|
|
|
|
}}
|
2024-12-21 18:44:57 +00:00
|
|
|
onClick={(event) => event.preventDefault()}
|
2024-04-23 14:45:49 +00:00
|
|
|
>
|
|
|
|
{answer ? (
|
2024-11-14 21:29:28 +00:00
|
|
|
choiceImgUrlAnswer ? (
|
2024-04-23 14:45:49 +00:00
|
|
|
<img
|
2024-11-14 21:29:28 +00:00
|
|
|
key={choiceImgUrlAnswer}
|
|
|
|
src={choiceImgUrlAnswer}
|
2024-04-23 14:45:49 +00:00
|
|
|
style={{ width: "100%", height: "100%", objectFit: "cover" }}
|
|
|
|
alt=""
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<BlankImage />
|
|
|
|
)
|
2024-11-14 21:29:28 +00:00
|
|
|
) : choiceImgUrlQuestion !== " " && choiceImgUrlQuestion !== null && choiceImgUrlQuestion.length > 0 ? (
|
2024-04-23 14:45:49 +00:00
|
|
|
<img
|
2024-11-14 21:29:28 +00:00
|
|
|
src={choiceImgUrlQuestion}
|
2024-04-23 14:45:49 +00:00
|
|
|
style={{ width: "100%", height: "100%", objectFit: "cover" }}
|
|
|
|
alt=""
|
|
|
|
/>
|
2024-05-31 16:41:18 +00:00
|
|
|
) : currentQuestion.content.replText !== " " && currentQuestion.content.replText.length > 0 ? (
|
2024-04-23 14:45:49 +00:00
|
|
|
currentQuestion.content.replText
|
|
|
|
) : variant?.extendedText || isMobile ? (
|
2025-04-20 15:16:22 +00:00
|
|
|
t("Select an answer option below")
|
2024-04-23 14:45:49 +00:00
|
|
|
) : (
|
2025-04-20 15:16:22 +00:00
|
|
|
t("Select an answer option on the left")
|
2024-04-23 14:45:49 +00:00
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
};
|