import { useState } from "react"; import { Box, ButtonBase, useTheme, Typography, SxProps, Theme, } from "@mui/material"; import { SnackbarProvider, enqueueSnackbar } from "notistack"; import UploadIcon from "@icons/UploadIcon"; import { useCurrentQuiz } from "@root/quizes/hooks"; interface Props { text?: string; sx?: SxProps; heightImg: string; widthImg?: string; } //Научи функцию принимать данные для валидации export const DropZone = ({ text, sx, heightImg, widthImg }: Props) => { const theme = useTheme(); const { quiz, updateQuiz } = useCurrentQuiz(); const [ready, setReady] = useState(false); if (!quiz) return null; // TODO throw and catch with error boundary const imgHC = (imgInp: HTMLInputElement) => { const file = imgInp.files?.[0]; if (file) { if (file.size < 5242880) { updateQuiz(quiz => { 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) { updateQuiz(quiz => { 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 )} ); };