2024-09-12 08:43:50 +00:00
|
|
|
import { CheckboxIcon } from "@/assets/icons/Checkbox";
|
2024-11-14 21:29:28 +00:00
|
|
|
import type { QuestionVariant, QuestionVariantWithEditedImages } from "@/model/questionTypes/shared";
|
2025-05-21 05:48:32 +00:00
|
|
|
import {
|
|
|
|
Box,
|
|
|
|
Checkbox,
|
|
|
|
FormControlLabel,
|
|
|
|
Input,
|
|
|
|
Radio,
|
|
|
|
TextareaAutosize,
|
|
|
|
Typography,
|
|
|
|
useTheme,
|
|
|
|
ButtonBase,
|
|
|
|
} from "@mui/material";
|
2024-04-23 14:45:49 +00:00
|
|
|
import { useQuizViewStore } from "@stores/quizView";
|
|
|
|
import RadioCheck from "@ui_kit/RadioCheck";
|
|
|
|
import RadioIcon from "@ui_kit/RadioIcon";
|
2024-06-29 09:32:16 +00:00
|
|
|
import { quizThemes } from "@utils/themes/Publication/themePublication";
|
2025-05-21 05:48:32 +00:00
|
|
|
import { useMemo, type MouseEvent, useRef, useEffect, useState } from "react";
|
2024-11-14 21:29:28 +00:00
|
|
|
import { useRootContainerSize } from "@contexts/RootContainerWidthContext";
|
2025-05-01 13:15:54 +00:00
|
|
|
import { useQuizStore } from "@/stores/useQuizStore";
|
2025-04-20 15:16:22 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2025-05-21 05:48:32 +00:00
|
|
|
import { sendAnswer, sendFile } from "@api/quizRelase";
|
|
|
|
import { enqueueSnackbar } from "notistack";
|
|
|
|
import { ACCEPT_SEND_FILE_TYPES_MAP, MAX_FILE_SIZE } from "@/components/ViewPublicationPage/tools/fileUpload";
|
|
|
|
import UploadIcon from "@icons/UploadIcon";
|
|
|
|
import { uploadFile } from "@/utils/fileUpload";
|
2025-05-31 17:32:13 +00:00
|
|
|
import { ImageCard } from "./ImageCard";
|
2024-04-23 14:45:49 +00:00
|
|
|
|
|
|
|
type ImagesProps = {
|
2024-06-29 09:32:16 +00:00
|
|
|
questionId: string;
|
2024-11-14 21:29:28 +00:00
|
|
|
variant: QuestionVariantWithEditedImages;
|
2024-04-23 14:45:49 +00:00
|
|
|
index: number;
|
2024-09-12 08:43:50 +00:00
|
|
|
answer: string | string[] | undefined;
|
2024-09-12 15:55:25 +00:00
|
|
|
isMulti: boolean;
|
|
|
|
own: boolean;
|
|
|
|
questionLargeCheck: boolean;
|
|
|
|
ownPlaceholder: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
interface OwnInputProps {
|
|
|
|
variant: QuestionVariant;
|
|
|
|
largeCheck: boolean;
|
|
|
|
ownPlaceholder: string;
|
|
|
|
}
|
2025-05-31 17:32:13 +00:00
|
|
|
const OwnInput = ({ variant, largeCheck, ownPlaceholder }: OwnInputProps) => {
|
2024-09-12 15:55:25 +00:00
|
|
|
const theme = useTheme();
|
|
|
|
const ownVariants = useQuizViewStore((state) => state.ownVariants);
|
|
|
|
const { updateOwnVariant } = useQuizViewStore((state) => state);
|
|
|
|
|
|
|
|
const ownAnswer = ownVariants[ownVariants.findIndex((v) => v.id === variant.id)]?.variant.answer || "";
|
|
|
|
|
|
|
|
return largeCheck ? (
|
|
|
|
<Box sx={{ overflow: "auto" }}>
|
|
|
|
<TextareaAutosize
|
2024-10-08 20:14:02 +00:00
|
|
|
placeholder={ownPlaceholder || "|"}
|
2024-09-12 15:55:25 +00:00
|
|
|
style={{
|
|
|
|
resize: "none",
|
|
|
|
width: "100%",
|
|
|
|
fontSize: "16px",
|
2024-11-24 23:42:33 +00:00
|
|
|
color: ownAnswer.length === 0 ? "ownPlaceholder" : theme.palette.text.primary,
|
2024-09-12 15:55:25 +00:00
|
|
|
letterSpacing: "-0.4px",
|
|
|
|
wordSpacing: "-3px",
|
|
|
|
outline: "0px none",
|
2024-10-08 20:14:02 +00:00
|
|
|
backgroundColor: "inherit",
|
|
|
|
border: "none",
|
2024-09-25 16:52:22 +00:00
|
|
|
//@ts-ignore
|
|
|
|
"&::-webkit-scrollbar": {
|
|
|
|
width: "4px",
|
|
|
|
},
|
|
|
|
"&::-webkit-scrollbar-thumb": {
|
2024-09-25 17:20:52 +00:00
|
|
|
backgroundColor: theme.palette.primary.main,
|
2024-09-25 16:52:22 +00:00
|
|
|
},
|
2024-09-26 19:12:11 +00:00
|
|
|
scrollbarColor: theme.palette.primary.main,
|
2024-09-12 15:55:25 +00:00
|
|
|
}}
|
|
|
|
value={ownAnswer}
|
|
|
|
onClick={(e: React.MouseEvent<HTMLTextAreaElement>) => e.stopPropagation()}
|
|
|
|
onChange={(e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
|
|
updateOwnVariant(variant.id, e.target.value);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
) : (
|
|
|
|
<Input
|
2024-10-08 20:14:02 +00:00
|
|
|
placeholder={ownPlaceholder || "|"}
|
2024-09-12 15:55:25 +00:00
|
|
|
sx={{
|
2024-10-08 20:14:02 +00:00
|
|
|
backgroundColor: "inherit",
|
2024-09-12 15:55:25 +00:00
|
|
|
width: "100%",
|
|
|
|
fontSize: "18px",
|
2024-11-24 23:42:33 +00:00
|
|
|
color: ownAnswer.length === 0 ? "ownPlaceholder" : theme.palette.text.primary,
|
2024-09-12 15:55:25 +00:00
|
|
|
}}
|
|
|
|
value={ownAnswer}
|
|
|
|
disableUnderline
|
|
|
|
onClick={(e: React.MouseEvent<HTMLElement>) => e.stopPropagation()}
|
|
|
|
onChange={(e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
|
|
updateOwnVariant(variant.id, e.target.value);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
2024-04-23 14:45:49 +00:00
|
|
|
};
|
|
|
|
|
2024-09-12 15:55:25 +00:00
|
|
|
export const ImageVariant = ({
|
|
|
|
questionId,
|
|
|
|
answer,
|
|
|
|
isMulti,
|
|
|
|
variant,
|
|
|
|
index,
|
|
|
|
own,
|
|
|
|
questionLargeCheck,
|
|
|
|
ownPlaceholder,
|
|
|
|
}: ImagesProps) => {
|
2025-05-01 13:15:54 +00:00
|
|
|
const { settings } = useQuizStore();
|
2024-04-23 14:45:49 +00:00
|
|
|
const { deleteAnswer, updateAnswer } = useQuizViewStore((state) => state);
|
|
|
|
const theme = useTheme();
|
2025-04-20 15:16:22 +00:00
|
|
|
const { t } = useTranslation();
|
2024-09-12 08:43:50 +00:00
|
|
|
const answers = useQuizViewStore((state) => state.answers);
|
2024-11-14 21:29:28 +00:00
|
|
|
const isMobile = useRootContainerSize() < 450;
|
|
|
|
const isTablet = useRootContainerSize() < 850;
|
2025-05-21 05:48:32 +00:00
|
|
|
const [isSending, setIsSending] = useState(false);
|
2025-05-31 17:32:13 +00:00
|
|
|
|
2025-05-21 05:48:32 +00:00
|
|
|
const { quizId, preview } = useQuizStore();
|
2024-04-23 14:45:49 +00:00
|
|
|
|
2024-12-21 18:44:57 +00:00
|
|
|
const canvasRef = useRef<HTMLCanvasElement | null>(null);
|
|
|
|
|
2024-04-23 14:45:49 +00:00
|
|
|
const onVariantClick = async (event: MouseEvent<HTMLDivElement>) => {
|
|
|
|
event.preventDefault();
|
|
|
|
|
2025-05-31 17:32:13 +00:00
|
|
|
if (own) return;
|
|
|
|
|
2024-09-12 08:43:50 +00:00
|
|
|
const variantId = variant.id;
|
|
|
|
if (isMulti) {
|
|
|
|
const currentAnswer = typeof answer !== "string" ? answer || [] : [];
|
|
|
|
|
|
|
|
return updateAnswer(
|
|
|
|
questionId,
|
|
|
|
currentAnswer.includes(variantId)
|
|
|
|
? currentAnswer?.filter((item) => item !== variantId)
|
|
|
|
: [...currentAnswer, variantId],
|
|
|
|
variant.points || 0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAnswer(questionId, variantId, variant.points || 0);
|
2024-04-23 14:45:49 +00:00
|
|
|
|
2024-09-12 08:43:50 +00:00
|
|
|
if (answer === variantId) {
|
2024-06-29 09:32:16 +00:00
|
|
|
deleteAnswer(questionId);
|
2024-04-23 14:45:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-05-21 05:48:32 +00:00
|
|
|
const handleFileUpload = async (file: File | undefined) => {
|
|
|
|
if (isSending || !file) return;
|
|
|
|
|
|
|
|
const result = await uploadFile({
|
|
|
|
file,
|
|
|
|
questionId,
|
|
|
|
quizId,
|
|
|
|
fileType: "picture",
|
|
|
|
preview,
|
|
|
|
onSuccess: (fileUrl) => {
|
|
|
|
setImageUrl(URL.createObjectURL(file));
|
|
|
|
},
|
|
|
|
onError: (error) => {
|
|
|
|
console.error(error);
|
|
|
|
enqueueSnackbar(t(error.message));
|
|
|
|
},
|
|
|
|
onProgress: () => {
|
|
|
|
setIsSending(true);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
setIsSending(false);
|
|
|
|
};
|
|
|
|
|
2024-11-14 21:29:28 +00:00
|
|
|
const choiceImgUrl = useMemo(() => {
|
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;
|
|
|
|
}
|
2025-05-31 17:32:13 +00:00
|
|
|
}, [variant.editedUrlImagesList, isMobile, isTablet, variant.extendedText]);
|
2024-11-14 21:29:28 +00:00
|
|
|
|
2024-12-21 18:44:57 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (canvasRef.current !== null) {
|
|
|
|
const canvas = canvasRef.current;
|
|
|
|
const ctx = canvas.getContext("2d");
|
|
|
|
if (ctx !== null) {
|
|
|
|
const img = new Image();
|
|
|
|
img.src = choiceImgUrl;
|
|
|
|
|
|
|
|
img.onload = () => {
|
|
|
|
canvas.width = img.width;
|
|
|
|
canvas.height = img.height;
|
|
|
|
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2024-04-23 14:45:49 +00:00
|
|
|
return (
|
|
|
|
<Box
|
|
|
|
sx={{
|
2024-09-26 19:51:46 +00:00
|
|
|
position: "relative",
|
2025-05-31 17:32:13 +00:00
|
|
|
cursor: own ? "default" : "pointer",
|
2024-04-23 14:45:49 +00:00
|
|
|
borderRadius: "12px",
|
|
|
|
border: `1px solid`,
|
2025-05-31 17:32:13 +00:00
|
|
|
borderColor: !own && !!answer?.includes(variant.id) ? theme.palette.primary.main : "#9A9AAF",
|
|
|
|
"&:hover": { borderColor: !own ? theme.palette.primary.main : "#9A9AAF" },
|
2024-04-23 14:45:49 +00:00
|
|
|
background:
|
|
|
|
settings.cfg.design && !quizThemes[settings.cfg.theme].isLight
|
|
|
|
? "rgba(255,255,255, 0.3)"
|
2024-05-31 16:41:18 +00:00
|
|
|
: (settings.cfg.design && quizThemes[settings.cfg.theme].isLight) || quizThemes[settings.cfg.theme].isLight
|
|
|
|
? "#FFFFFF"
|
|
|
|
: "transparent",
|
2024-04-23 14:45:49 +00:00
|
|
|
}}
|
|
|
|
onClick={onVariantClick}
|
|
|
|
>
|
|
|
|
<Box sx={{ display: "flex", alignItems: "center", gap: "10px" }}>
|
2025-05-31 17:32:13 +00:00
|
|
|
{variant.extendedText && (
|
|
|
|
<ImageCard
|
|
|
|
questionId={questionId}
|
|
|
|
imageUrl={choiceImgUrl}
|
|
|
|
isOwn={own}
|
|
|
|
/>
|
|
|
|
)}
|
2024-04-23 14:45:49 +00:00
|
|
|
</Box>
|
2024-09-26 19:51:46 +00:00
|
|
|
{own && (
|
|
|
|
<Typography
|
|
|
|
sx={{
|
2024-10-08 20:14:02 +00:00
|
|
|
color: theme.palette.text.primary,
|
2024-09-26 19:51:46 +00:00
|
|
|
fontSize: "14px",
|
|
|
|
pl: "15px",
|
|
|
|
}}
|
|
|
|
>
|
2025-04-20 15:16:22 +00:00
|
|
|
{t("Enter your answer")}
|
2024-09-26 19:51:46 +00:00
|
|
|
</Typography>
|
|
|
|
)}
|
2024-04-23 14:45:49 +00:00
|
|
|
<FormControlLabel
|
|
|
|
key={variant.id}
|
|
|
|
sx={{
|
|
|
|
textAlign: "center",
|
|
|
|
color: theme.palette.text.primary,
|
|
|
|
marginTop: "10px",
|
|
|
|
marginLeft: 0,
|
|
|
|
padding: "10px",
|
|
|
|
display: "flex",
|
|
|
|
alignItems: variant.answer.length <= 60 ? "center" : "flex-start",
|
|
|
|
justifyContent: "center",
|
|
|
|
position: "relative",
|
|
|
|
height: "80px",
|
|
|
|
"& .MuiFormControlLabel-label": {
|
|
|
|
wordBreak: "break-word",
|
|
|
|
height: variant.answer.length <= 60 ? undefined : "60px",
|
|
|
|
lineHeight: "normal",
|
2024-09-25 16:52:22 +00:00
|
|
|
overflow: "auto",
|
|
|
|
maxHeight: "100%",
|
|
|
|
width: "100%",
|
2024-04-23 14:45:49 +00:00
|
|
|
"&::-webkit-scrollbar": {
|
|
|
|
width: "4px",
|
|
|
|
},
|
|
|
|
"&::-webkit-scrollbar-thumb": {
|
2024-09-25 17:20:52 +00:00
|
|
|
backgroundColor: theme.palette.primary.main,
|
2024-04-23 14:45:49 +00:00
|
|
|
},
|
2024-09-26 19:12:11 +00:00
|
|
|
scrollbarColor: theme.palette.primary.main,
|
2024-04-23 14:45:49 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
value={index}
|
|
|
|
control={
|
2024-09-12 08:43:50 +00:00
|
|
|
isMulti ? (
|
|
|
|
<Checkbox
|
|
|
|
checked={!!answer?.includes(variant.id)}
|
|
|
|
checkedIcon={<RadioCheck color={theme.palette.primary.main} />}
|
|
|
|
icon={<RadioIcon />}
|
|
|
|
sx={{
|
|
|
|
position: "absolute",
|
|
|
|
top: "-297px",
|
|
|
|
right: 0,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Radio
|
|
|
|
checkedIcon={<RadioCheck color={theme.palette.primary.main} />}
|
|
|
|
icon={<RadioIcon />}
|
|
|
|
sx={{
|
|
|
|
position: "absolute",
|
|
|
|
top: "-297px",
|
|
|
|
right: 0,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)
|
2024-04-23 14:45:49 +00:00
|
|
|
}
|
2024-09-12 15:55:25 +00:00
|
|
|
label={
|
|
|
|
own ? (
|
|
|
|
<OwnInput
|
|
|
|
variant={variant}
|
|
|
|
largeCheck={questionLargeCheck}
|
2024-10-08 20:14:02 +00:00
|
|
|
ownPlaceholder={ownPlaceholder || "|"}
|
2024-09-12 15:55:25 +00:00
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
variant.answer
|
|
|
|
)
|
|
|
|
}
|
2024-04-23 14:45:49 +00:00
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
};
|