2023-06-21 02:38:13 +00:00
|
|
|
import { useState } from "react";
|
2023-11-01 13:31:15 +00:00
|
|
|
import {
|
2023-11-14 13:13:10 +00:00
|
|
|
Box,
|
|
|
|
ButtonBase,
|
|
|
|
useTheme,
|
|
|
|
Typography,
|
|
|
|
SxProps,
|
|
|
|
Theme,
|
2023-11-01 13:31:15 +00:00
|
|
|
} from "@mui/material";
|
2023-07-30 21:25:47 +00:00
|
|
|
import { SnackbarProvider, enqueueSnackbar } from "notistack";
|
2023-11-01 13:31:15 +00:00
|
|
|
import UploadIcon from "@icons/UploadIcon";
|
2023-11-14 13:13:10 +00:00
|
|
|
import { useCurrentQuiz } from "@root/quizes/hooks";
|
|
|
|
|
2023-11-01 13:31:15 +00:00
|
|
|
|
2023-06-21 21:00:38 +00:00
|
|
|
interface Props {
|
2023-11-14 13:13:10 +00:00
|
|
|
text?: string;
|
|
|
|
sx?: SxProps<Theme>;
|
|
|
|
heightImg: string;
|
|
|
|
widthImg?: string;
|
2023-06-21 21:00:38 +00:00
|
|
|
}
|
2023-06-21 02:38:13 +00:00
|
|
|
|
|
|
|
//Научи функцию принимать данные для валидации
|
2023-11-01 13:31:15 +00:00
|
|
|
export const DropZone = ({ text, sx, heightImg, widthImg }: Props) => {
|
2023-11-14 13:13:10 +00:00
|
|
|
const theme = useTheme();
|
|
|
|
const { quiz, updateQuiz } = useCurrentQuiz();
|
|
|
|
const [ready, setReady] = useState(false);
|
|
|
|
|
|
|
|
if (!quiz) return null; // TODO throw and catch with error boundary
|
2023-06-21 02:38:13 +00:00
|
|
|
|
2023-11-14 13:13:10 +00:00
|
|
|
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("Размер картинки слишком велик");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-06-21 21:00:38 +00:00
|
|
|
|
2023-11-14 13:13:10 +00:00
|
|
|
const dragenterHC = () => {
|
|
|
|
setReady(true);
|
|
|
|
};
|
2023-06-21 02:38:13 +00:00
|
|
|
|
2023-11-14 13:13:10 +00:00
|
|
|
const dragexitHC = () => {
|
|
|
|
setReady(false);
|
|
|
|
};
|
2023-06-21 02:38:13 +00:00
|
|
|
|
2023-11-14 13:13:10 +00:00
|
|
|
const dropHC = (event: React.DragEvent<HTMLDivElement>) => {
|
|
|
|
event.preventDefault();
|
|
|
|
setReady(false);
|
2023-06-21 21:00:38 +00:00
|
|
|
|
2023-11-14 13:13:10 +00:00
|
|
|
const file = event.dataTransfer.files[0];
|
|
|
|
if (file.size < 5242880) {
|
|
|
|
updateQuiz(quiz => {
|
|
|
|
quiz.config.startpage.background.desktop = URL.createObjectURL(file);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
enqueueSnackbar("Размер картинки слишком велик");
|
|
|
|
}
|
|
|
|
};
|
2023-06-21 02:38:13 +00:00
|
|
|
|
2023-11-14 13:13:10 +00:00
|
|
|
const dragOverHC = (event: React.DragEvent<HTMLDivElement>) => {
|
|
|
|
event.preventDefault();
|
|
|
|
};
|
2023-06-21 02:38:13 +00:00
|
|
|
|
2023-11-14 13:13:10 +00:00
|
|
|
return (
|
|
|
|
<ButtonBase component="label" sx={{ justifyContent: "flex-start" }}>
|
|
|
|
<input
|
|
|
|
onChange={(event) => imgHC(event.target)}
|
|
|
|
hidden
|
|
|
|
accept="image/*"
|
|
|
|
multiple
|
|
|
|
type="file"
|
|
|
|
/>
|
|
|
|
<Box
|
|
|
|
onDragEnter={dragenterHC}
|
|
|
|
onDragExit={dragexitHC}
|
|
|
|
onDrop={dropHC}
|
|
|
|
onDragOver={dragOverHC}
|
|
|
|
sx={{
|
|
|
|
width: "100%",
|
|
|
|
height: "120px",
|
|
|
|
position: "relative",
|
|
|
|
display: "flex",
|
|
|
|
justifyContent: "center",
|
|
|
|
alignItems: "center",
|
|
|
|
backgroundColor: theme.palette.background.default,
|
|
|
|
border: `1px solid ${ready ? "red" : theme.palette.grey2.main}`,
|
|
|
|
borderRadius: "8px",
|
|
|
|
opacity: quiz.config.startpage.background.desktop ? "0.5" : 1,
|
|
|
|
...sx,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<UploadIcon />
|
|
|
|
<Typography
|
|
|
|
sx={{
|
|
|
|
position: "absolute",
|
|
|
|
right: "10px",
|
|
|
|
bottom: "10px",
|
|
|
|
color: theme.palette.orange.main,
|
|
|
|
fontSize: "16px",
|
|
|
|
lineHeight: "19px",
|
|
|
|
textDecoration: "underline",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{text}
|
|
|
|
</Typography>
|
|
|
|
<SnackbarProvider
|
|
|
|
style={{ backgroundColor: theme.palette.brightPurple.main }}
|
|
|
|
/>
|
|
|
|
{quiz.config.startpage.background.desktop && (
|
|
|
|
<img
|
|
|
|
height={heightImg}
|
|
|
|
width={widthImg}
|
|
|
|
src={quiz.config.startpage.background.desktop}
|
|
|
|
style={{
|
|
|
|
position: "absolute",
|
|
|
|
zIndex: "-1",
|
|
|
|
objectFit: "revert-layer",
|
|
|
|
}}
|
|
|
|
alt="img"
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
</ButtonBase>
|
|
|
|
);
|
2023-07-30 21:25:47 +00:00
|
|
|
};
|