import { useState } from "react"; import { useParams } from "react-router-dom"; import { Box, ButtonBase, useTheme, Typography, SxProps, Theme, } from "@mui/material"; import { SnackbarProvider, enqueueSnackbar } from "notistack"; import { quizStore } from "@root/quizes"; import UploadIcon from "@icons/UploadIcon"; interface Props { text?: string; sx?: SxProps; heightImg: string; widthImg?: string; } //Научи функцию принимать данные для валидации export const DropZone = ({ text, sx, heightImg, widthImg }: Props) => { const quizId = Number(useParams().quizId); const { listQuizes, updateQuizesList } = quizStore(); const [ready, setReady] = useState(false); const theme = useTheme(); const quiz = listQuizes[quizId]; console.log(quiz.config.startpage.background.desktop); const imgHC = (imgInp: HTMLInputElement) => { const file = imgInp.files?.[0]; if (file) { if (file.size < 5242880) { updateQuizesList(quizId, { config: { ...quiz.config, startpage: { ...quiz.config.startpage, background: { ...quiz.config.startpage.background, desktop: URL.createObjectURL(file), }, }, }, }); } else { enqueueSnackbar("Размер картинки слишком велик"); } } }; const dragenterHC = () => { setReady(true); }; const dragexitHC = () => { setReady(false); }; const dropHC = (event: React.DragEvent) => { event.preventDefault(); setReady(false); const file = event.dataTransfer.files[0]; if (file.size < 5242880) { updateQuizesList(quizId, { config: { ...quiz.config, startpage: { ...quiz.config.startpage, background: { ...quiz.config.startpage.background, desktop: URL.createObjectURL(file), }, }, }, }); } else { enqueueSnackbar("Размер картинки слишком велик"); } }; const dragOverHC = (event: React.DragEvent) => { event.preventDefault(); }; return ( imgHC(event.target)} hidden accept="image/*" multiple type="file" /> {text} {quiz.config.startpage.background.desktop && ( img )} ); };