frontPanel/src/pages/startPage/dropZone.tsx

152 lines
3.9 KiB
TypeScript
Raw Normal View History

import { useState } from "react";
2023-11-01 13:31:15 +00:00
import { useParams } from "react-router-dom";
import {
Box,
ButtonBase,
useTheme,
Typography,
SxProps,
Theme,
} from "@mui/material";
import { SnackbarProvider, enqueueSnackbar } from "notistack";
2023-11-01 13:31:15 +00:00
import { quizStore } from "@root/quizes";
import UploadIcon from "@icons/UploadIcon";
interface Props {
text?: string;
sx?: SxProps<Theme>;
heightImg: string;
widthImg?: string;
}
//Научи функцию принимать данные для валидации
2023-11-01 13:31:15 +00:00
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();
2023-11-01 13:31:15 +00:00
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) {
2023-11-01 13:31:15 +00:00
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<HTMLDivElement>) => {
event.preventDefault();
setReady(false);
const file = event.dataTransfer.files[0];
if (file.size < 5242880) {
2023-11-01 13:31:15 +00:00
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<HTMLDivElement>) => {
event.preventDefault();
};
return (
<ButtonBase component="label" sx={{ justifyContent: "flex-start" }}>
2023-11-01 13:31:15 +00:00
<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",
2023-11-01 13:31:15 +00:00
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>
2023-11-01 13:31:15 +00:00
<SnackbarProvider
style={{ backgroundColor: theme.palette.brightPurple.main }}
/>
{quiz.config.startpage.background.desktop && (
<img
height={heightImg}
width={widthImg}
2023-11-01 13:31:15 +00:00
src={quiz.config.startpage.background.desktop}
style={{
position: "absolute",
zIndex: "-1",
objectFit: "revert-layer",
}}
2023-11-01 13:31:15 +00:00
alt="img"
/>
2023-11-01 13:31:15 +00:00
)}
</Box>
</ButtonBase>
);
};