2023-12-31 02:53:25 +00:00
|
|
|
|
import {
|
|
|
|
|
Typography,
|
|
|
|
|
Box,
|
|
|
|
|
useTheme,
|
|
|
|
|
ButtonBase,
|
|
|
|
|
Modal,
|
|
|
|
|
TextField,
|
|
|
|
|
InputAdornment,
|
2024-01-04 15:18:29 +00:00
|
|
|
|
useMediaQuery,
|
2023-12-31 02:53:25 +00:00
|
|
|
|
} from "@mui/material";
|
2023-09-05 13:54:41 +00:00
|
|
|
|
import UploadIcon from "../../../assets/icons/UploadIcon";
|
|
|
|
|
import SearchIcon from "../../../assets/icons/SearchIcon";
|
|
|
|
|
import UnsplashIcon from "../../../assets/icons/Unsplash.svg";
|
2023-12-04 11:57:54 +00:00
|
|
|
|
import { useRef, useState, type DragEvent } from "react";
|
2023-09-05 13:54:41 +00:00
|
|
|
|
|
2023-12-16 10:03:23 +00:00
|
|
|
|
type ImageFormat = "jpg" | "jpeg" | "png" | "gif";
|
|
|
|
|
|
2023-09-05 13:54:41 +00:00
|
|
|
|
interface ModalkaProps {
|
2023-12-15 19:27:53 +00:00
|
|
|
|
isOpen: boolean;
|
|
|
|
|
onClose: () => void;
|
|
|
|
|
handleImageChange: (file: File) => void;
|
|
|
|
|
description?: string;
|
2023-12-16 10:03:23 +00:00
|
|
|
|
accept?: ImageFormat[];
|
2023-09-05 13:54:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-16 10:03:23 +00:00
|
|
|
|
export const UploadImageModal: React.FC<ModalkaProps> = ({
|
|
|
|
|
handleImageChange,
|
|
|
|
|
isOpen,
|
|
|
|
|
onClose,
|
|
|
|
|
accept,
|
|
|
|
|
description,
|
|
|
|
|
}) => {
|
2023-12-15 19:27:53 +00:00
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const dropZone = useRef<HTMLDivElement>(null);
|
|
|
|
|
const [ready, setReady] = useState(false);
|
2024-01-04 15:18:29 +00:00
|
|
|
|
const isMobile = useMediaQuery(theme.breakpoints.down(600));
|
2023-12-15 19:27:53 +00:00
|
|
|
|
const handleDragEnter = (event: DragEvent<HTMLDivElement>) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
setReady(true);
|
|
|
|
|
};
|
2023-09-05 13:54:41 +00:00
|
|
|
|
|
2023-12-15 19:27:53 +00:00
|
|
|
|
const handleDrop = (event: DragEvent<HTMLDivElement>) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
2023-09-05 13:54:41 +00:00
|
|
|
|
|
2023-12-15 19:27:53 +00:00
|
|
|
|
const file = event.dataTransfer.files[0];
|
|
|
|
|
if (!file) return;
|
2023-09-05 13:54:41 +00:00
|
|
|
|
|
2023-12-15 19:27:53 +00:00
|
|
|
|
handleImageChange(file);
|
|
|
|
|
};
|
2023-09-05 13:54:41 +00:00
|
|
|
|
|
2023-12-31 02:53:25 +00:00
|
|
|
|
const acceptedFormats = accept
|
|
|
|
|
? accept.map((format) => "." + format).join(", ")
|
|
|
|
|
: "";
|
2023-12-16 10:03:23 +00:00
|
|
|
|
|
2023-12-15 19:27:53 +00:00
|
|
|
|
return (
|
|
|
|
|
<Modal
|
|
|
|
|
open={isOpen}
|
|
|
|
|
onClose={onClose}
|
|
|
|
|
aria-labelledby="modal-modal-title"
|
|
|
|
|
aria-describedby="modal-modal-description"
|
|
|
|
|
>
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
position: "absolute",
|
|
|
|
|
top: "50%",
|
|
|
|
|
left: "50%",
|
|
|
|
|
transform: "translate(-50%, -50%)",
|
2024-01-04 15:18:29 +00:00
|
|
|
|
maxWidth: isMobile ? "300px" : "690px",
|
2023-12-15 19:27:53 +00:00
|
|
|
|
bgcolor: "background.paper",
|
|
|
|
|
borderRadius: "12px",
|
|
|
|
|
boxShadow: 24,
|
|
|
|
|
p: 0,
|
|
|
|
|
overflow: "hidden",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
padding: "20px",
|
|
|
|
|
background: theme.palette.background.default,
|
|
|
|
|
}}
|
2023-09-05 13:54:41 +00:00
|
|
|
|
>
|
2023-12-31 02:53:25 +00:00
|
|
|
|
<Typography
|
|
|
|
|
sx={{ marginBottom: "20px", fontWeight: "bold", color: "#4D4D4D" }}
|
|
|
|
|
>
|
2023-12-15 19:27:53 +00:00
|
|
|
|
Добавьте изображение
|
|
|
|
|
</Typography>
|
|
|
|
|
<ButtonBase component="label" sx={{ justifyContent: "flex-start" }}>
|
|
|
|
|
<input
|
2023-12-31 02:53:25 +00:00
|
|
|
|
onChange={(event) =>
|
|
|
|
|
event.target.files?.[0] &&
|
|
|
|
|
handleImageChange(event.target.files[0])
|
|
|
|
|
}
|
2023-12-15 19:27:53 +00:00
|
|
|
|
hidden
|
2023-12-16 10:03:23 +00:00
|
|
|
|
accept={acceptedFormats || ".jpg, .jpeg, .png , .gif"}
|
2023-12-15 19:27:53 +00:00
|
|
|
|
multiple
|
|
|
|
|
type="file"
|
|
|
|
|
data-cy="upload-image-input"
|
|
|
|
|
/>
|
2023-09-05 13:54:41 +00:00
|
|
|
|
<Box
|
2023-12-31 02:53:25 +00:00
|
|
|
|
onDragOver={(event: DragEvent<HTMLDivElement>) =>
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
}
|
2023-12-15 19:27:53 +00:00
|
|
|
|
onDrop={handleDrop}
|
|
|
|
|
ref={dropZone}
|
|
|
|
|
sx={{
|
|
|
|
|
width: "580px",
|
2024-01-04 15:18:29 +00:00
|
|
|
|
padding: isMobile ? "33px" : "33px 10px 33px 55px",
|
2023-12-15 19:27:53 +00:00
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
backgroundColor: theme.palette.background.default,
|
|
|
|
|
border: `1px solid ${ready ? "red" : theme.palette.grey2.main}`,
|
|
|
|
|
borderRadius: "8px",
|
|
|
|
|
gap: "55px",
|
2024-01-04 15:18:29 +00:00
|
|
|
|
flexDirection: isMobile ? "column" : undefined,
|
2023-12-15 19:27:53 +00:00
|
|
|
|
}}
|
|
|
|
|
onDragEnter={handleDragEnter} // Применяем обработчик onDragEnter напрямую
|
2023-09-05 13:54:41 +00:00
|
|
|
|
>
|
2023-12-15 19:27:53 +00:00
|
|
|
|
<UploadIcon />
|
|
|
|
|
<Box>
|
|
|
|
|
<Typography sx={{ color: "#9A9AAF", fontWeight: "bold" }}>
|
|
|
|
|
Загрузите или перетяните сюда файл
|
|
|
|
|
</Typography>
|
|
|
|
|
<Typography sx={{ color: "#9A9AAF", fontSize: "16px" }}>
|
2023-12-31 02:53:25 +00:00
|
|
|
|
{description ||
|
|
|
|
|
"Принимает JPG, PNG, и GIF формат — максимум 5mb"}
|
2023-12-15 19:27:53 +00:00
|
|
|
|
</Typography>
|
|
|
|
|
</Box>
|
2023-12-04 11:57:54 +00:00
|
|
|
|
</Box>
|
2023-12-15 19:27:53 +00:00
|
|
|
|
</ButtonBase>
|
2023-12-31 02:53:25 +00:00
|
|
|
|
{/* <Box*/}
|
|
|
|
|
{/* sx={{*/}
|
|
|
|
|
{/* display: "flex",*/}
|
|
|
|
|
{/* justifyContent: "space-between",*/}
|
|
|
|
|
{/* margin: "20px 0",*/}
|
|
|
|
|
{/* }}*/}
|
|
|
|
|
{/* >*/}
|
|
|
|
|
{/* <Typography*/}
|
|
|
|
|
{/* sx={{*/}
|
|
|
|
|
{/* fontWeight: "bold",*/}
|
|
|
|
|
{/* color: "#4D4D4D",*/}
|
|
|
|
|
{/* }}*/}
|
|
|
|
|
{/* >*/}
|
|
|
|
|
{/* Или выберите на фотостоке*/}
|
|
|
|
|
{/* </Typography>*/}
|
|
|
|
|
{/* <img src={UnsplashIcon} alt="" />*/}
|
|
|
|
|
{/* </Box>*/}
|
|
|
|
|
{/* <TextField*/}
|
|
|
|
|
{/* id="search-in-unsplash"*/}
|
|
|
|
|
{/* placeholder="Ищите изображения на английском языка"*/}
|
|
|
|
|
{/* sx={{*/}
|
|
|
|
|
{/* "& .MuiInputBase-input": {*/}
|
|
|
|
|
{/* height: "48px",*/}
|
|
|
|
|
{/* padding: "0 10px 0 0",*/}
|
|
|
|
|
{/* },*/}
|
|
|
|
|
{/* "& .MuiOutlinedInput-notchedOutline": {*/}
|
|
|
|
|
{/* borderRadius: "8px",*/}
|
|
|
|
|
{/* },*/}
|
|
|
|
|
{/* "& .Mui-focused .MuiOutlinedInput-notchedOutline": {*/}
|
|
|
|
|
{/* border: "1px solid rgba(0, 0, 0, 0.23)",*/}
|
|
|
|
|
{/* },*/}
|
|
|
|
|
{/* "& .MuiInputBase-root.MuiOutlinedInput-root .MuiOutlinedInput-notchedOutline": {*/}
|
|
|
|
|
{/* borderColor: "rgba(0, 0, 0, 0.23)",*/}
|
|
|
|
|
{/* },*/}
|
|
|
|
|
{/* }}*/}
|
|
|
|
|
{/* InputProps={{*/}
|
|
|
|
|
{/* startAdornment: (*/}
|
|
|
|
|
{/* <InputAdornment*/}
|
|
|
|
|
{/* position="start"*/}
|
|
|
|
|
{/* sx={{*/}
|
|
|
|
|
{/* outline: "none",*/}
|
|
|
|
|
{/* "& svg > path": { stroke: "#9A9AAF" },*/}
|
|
|
|
|
{/* }}*/}
|
|
|
|
|
{/* >*/}
|
|
|
|
|
{/* <SearchIcon />*/}
|
|
|
|
|
{/* </InputAdornment>*/}
|
|
|
|
|
{/* ),*/}
|
|
|
|
|
{/* }}*/}
|
|
|
|
|
{/* />*/}
|
2023-12-15 19:27:53 +00:00
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
2023-09-05 13:54:41 +00:00
|
|
|
|
};
|