import { CheckboxIcon } from "@/assets/icons/Checkbox"; import type { QuestionVariant, QuestionVariantWithEditedImages } from "@/model/questionTypes/shared"; import { Box, Checkbox, FormControlLabel, Input, Radio, TextareaAutosize, Typography, useTheme, ButtonBase, } from "@mui/material"; import { useQuizViewStore } from "@stores/quizView"; import RadioCheck from "@ui_kit/RadioCheck"; import RadioIcon from "@ui_kit/RadioIcon"; import { quizThemes } from "@utils/themes/Publication/themePublication"; import { useMemo, type MouseEvent, useRef, useEffect, useState } from "react"; import { useRootContainerSize } from "@contexts/RootContainerWidthContext"; import { useQuizStore } from "@/stores/useQuizStore"; import { useTranslation } from "react-i18next"; 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"; type ImagesProps = { questionId: string; variant: QuestionVariantWithEditedImages; index: number; answer: string | string[] | undefined; isMulti: boolean; own: boolean; questionLargeCheck: boolean; ownPlaceholder: string; }; interface OwnInputProps { questionId: string; variant: QuestionVariant; largeCheck: boolean; ownPlaceholder: string; } const OwnInput = ({ questionId, variant, largeCheck, ownPlaceholder }: OwnInputProps) => { 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 ? ( ) => e.stopPropagation()} onChange={(e: React.ChangeEvent) => { updateOwnVariant(variant.id, e.target.value); }} /> ) : ( ) => e.stopPropagation()} onChange={(e: React.ChangeEvent) => { updateOwnVariant(variant.id, e.target.value); }} /> ); }; export const ImageVariant = ({ questionId, answer, isMulti, variant, index, own, questionLargeCheck, ownPlaceholder, }: ImagesProps) => { const { settings } = useQuizStore(); const { deleteAnswer, updateAnswer } = useQuizViewStore((state) => state); const theme = useTheme(); const { t } = useTranslation(); const answers = useQuizViewStore((state) => state.answers); const isMobile = useRootContainerSize() < 450; const isTablet = useRootContainerSize() < 850; const [isSending, setIsSending] = useState(false); const [isDropzoneHighlighted, setIsDropzoneHighlighted] = useState(false); const { quizId, preview } = useQuizStore(); const [imageUrl, setImageUrl] = useState(null); const canvasRef = useRef(null); const containerCanvasRef = useRef(null); const onVariantClick = async (event: MouseEvent) => { event.preventDefault(); 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); if (answer === variantId) { deleteAnswer(questionId); } }; 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); }; const onDrop = (event: React.DragEvent) => { event.preventDefault(); setIsDropzoneHighlighted(false); const file = event.dataTransfer.files[0]; handleFileUpload(file); }; const choiceImgUrl = useMemo(() => { if (variant.editedUrlImagesList !== undefined && variant.editedUrlImagesList !== null) { return variant.editedUrlImagesList[isMobile ? "mobile" : isTablet ? "tablet" : "desktop"]; } else { return variant.extendedText; } }, []); 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); }; } } }, []); return ( {variant.extendedText && ( <> {own && ( handleFileUpload(target.files?.[0])} hidden accept={ACCEPT_SEND_FILE_TYPES_MAP.picture.join(",")} type="file" /> )} )} {own && ( {t("Enter your answer")} )} } icon={} sx={{ position: "absolute", top: "-297px", right: 0, }} /> ) : ( } icon={} sx={{ position: "absolute", top: "-297px", right: 0, }} /> ) } label={ own ? ( ) : ( variant.answer ) } /> ); };