chore: Restrict image uploads to JPG and PNG formats
Updated file input to accept only .jpg, .jpeg, and .png files in the UploadImageModal component.
This commit is contained in:
parent
f8c4d11728
commit
f0dafad472
@ -1,29 +1,17 @@
|
|||||||
import {
|
import { Typography, Box, useTheme, ButtonBase, Modal, TextField, InputAdornment } from "@mui/material";
|
||||||
Typography,
|
|
||||||
Box,
|
|
||||||
useTheme,
|
|
||||||
ButtonBase,
|
|
||||||
Modal,
|
|
||||||
TextField,
|
|
||||||
InputAdornment,
|
|
||||||
} from "@mui/material";
|
|
||||||
import UploadIcon from "../../../assets/icons/UploadIcon";
|
import UploadIcon from "../../../assets/icons/UploadIcon";
|
||||||
import SearchIcon from "../../../assets/icons/SearchIcon";
|
import SearchIcon from "../../../assets/icons/SearchIcon";
|
||||||
import UnsplashIcon from "../../../assets/icons/Unsplash.svg";
|
import UnsplashIcon from "../../../assets/icons/Unsplash.svg";
|
||||||
import { useRef, useState, type DragEvent } from "react";
|
import { useRef, useState, type DragEvent } from "react";
|
||||||
|
|
||||||
|
|
||||||
interface ModalkaProps {
|
interface ModalkaProps {
|
||||||
isOpen: boolean;
|
isOpen: boolean;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
handleImageChange: (file: File) => void;
|
handleImageChange: (file: File) => void;
|
||||||
|
description?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const UploadImageModal: React.FC<ModalkaProps> = ({
|
export const UploadImageModal: React.FC<ModalkaProps> = ({ handleImageChange, isOpen, onClose, description }) => {
|
||||||
handleImageChange,
|
|
||||||
isOpen,
|
|
||||||
onClose,
|
|
||||||
}) => {
|
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const dropZone = useRef<HTMLDivElement>(null);
|
const dropZone = useRef<HTMLDivElement>(null);
|
||||||
const [ready, setReady] = useState(false);
|
const [ready, setReady] = useState(false);
|
||||||
@ -72,24 +60,20 @@ export const UploadImageModal: React.FC<ModalkaProps> = ({
|
|||||||
background: theme.palette.background.default,
|
background: theme.palette.background.default,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Typography
|
<Typography sx={{ marginBottom: "20px", fontWeight: "bold", color: "#4D4D4D" }}>
|
||||||
sx={{ marginBottom: "20px", fontWeight: "bold", color: "#4D4D4D" }}
|
|
||||||
>
|
|
||||||
Добавьте изображение
|
Добавьте изображение
|
||||||
</Typography>
|
</Typography>
|
||||||
<ButtonBase component="label" sx={{ justifyContent: "flex-start" }}>
|
<ButtonBase component="label" sx={{ justifyContent: "flex-start" }}>
|
||||||
<input
|
<input
|
||||||
onChange={(event) => event.target.files?.[0] && handleImageChange(event.target.files[0])}
|
onChange={(event) => event.target.files?.[0] && handleImageChange(event.target.files[0])}
|
||||||
hidden
|
hidden
|
||||||
accept="image/*"
|
accept=".jpg, .jpeg, .png"
|
||||||
multiple
|
multiple
|
||||||
type="file"
|
type="file"
|
||||||
data-cy="upload-image-input"
|
data-cy="upload-image-input"
|
||||||
/>
|
/>
|
||||||
<Box
|
<Box
|
||||||
onDragOver={(event: DragEvent<HTMLDivElement>) =>
|
onDragOver={(event: DragEvent<HTMLDivElement>) => event.preventDefault()}
|
||||||
event.preventDefault()
|
|
||||||
}
|
|
||||||
onDrop={handleDrop}
|
onDrop={handleDrop}
|
||||||
ref={dropZone}
|
ref={dropZone}
|
||||||
sx={{
|
sx={{
|
||||||
@ -110,7 +94,7 @@ export const UploadImageModal: React.FC<ModalkaProps> = ({
|
|||||||
Загрузите или перетяните сюда файл
|
Загрузите или перетяните сюда файл
|
||||||
</Typography>
|
</Typography>
|
||||||
<Typography sx={{ color: "#9A9AAF", fontSize: "16px" }}>
|
<Typography sx={{ color: "#9A9AAF", fontSize: "16px" }}>
|
||||||
Принимает JPG, PNG, и GIF формат — максимум 5mb
|
{description || "Принимает JPG, PNG, и GIF формат — максимум 5mb"}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
@ -146,8 +130,7 @@ export const UploadImageModal: React.FC<ModalkaProps> = ({
|
|||||||
"& .Mui-focused .MuiOutlinedInput-notchedOutline": {
|
"& .Mui-focused .MuiOutlinedInput-notchedOutline": {
|
||||||
border: "1px solid rgba(0, 0, 0, 0.23)",
|
border: "1px solid rgba(0, 0, 0, 0.23)",
|
||||||
},
|
},
|
||||||
"& .MuiInputBase-root.MuiOutlinedInput-root .MuiOutlinedInput-notchedOutline":
|
"& .MuiInputBase-root.MuiOutlinedInput-root .MuiOutlinedInput-notchedOutline": {
|
||||||
{
|
|
||||||
borderColor: "rgba(0, 0, 0, 0.23)",
|
borderColor: "rgba(0, 0, 0, 0.23)",
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
|
|||||||
@ -6,7 +6,6 @@ import { useState } from "react";
|
|||||||
import { UploadImageModal } from "../../pages/Questions/UploadImage/UploadImageModal";
|
import { UploadImageModal } from "../../pages/Questions/UploadImage/UploadImageModal";
|
||||||
import { useDisclosure } from "../../utils/useDisclosure";
|
import { useDisclosure } from "../../utils/useDisclosure";
|
||||||
|
|
||||||
|
|
||||||
const allowedFileTypes = ["image/png", "image/jpeg", "image/gif"];
|
const allowedFileTypes = ["image/png", "image/jpeg", "image/gif"];
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@ -42,19 +41,22 @@ export default function FaviconDropZone({ imageUrl, onImageUploadClick, onDelete
|
|||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box sx={{
|
<Box
|
||||||
|
sx={{
|
||||||
display: "flex",
|
display: "flex",
|
||||||
gap: "10px",
|
gap: "10px",
|
||||||
}}>
|
}}
|
||||||
|
>
|
||||||
<UploadImageModal
|
<UploadImageModal
|
||||||
isOpen={isImageUploadOpen}
|
isOpen={isImageUploadOpen}
|
||||||
onClose={closeImageUploadModal}
|
onClose={closeImageUploadModal}
|
||||||
handleImageChange={handleImageUpload}
|
handleImageChange={handleImageUpload}
|
||||||
|
description="Принимает JPG, PNG — максимум 5mb"
|
||||||
/>
|
/>
|
||||||
<Box
|
<Box
|
||||||
onDragEnter={() => !imageUrl && setIsDropReady(true)}
|
onDragEnter={() => !imageUrl && setIsDropReady(true)}
|
||||||
onDragExit={() => setIsDropReady(false)}
|
onDragExit={() => setIsDropReady(false)}
|
||||||
onDragOver={e => e.preventDefault()}
|
onDragOver={(e) => e.preventDefault()}
|
||||||
onDrop={onDrop}
|
onDrop={onDrop}
|
||||||
sx={{
|
sx={{
|
||||||
width: "48px",
|
width: "48px",
|
||||||
@ -62,7 +64,8 @@ export default function FaviconDropZone({ imageUrl, onImageUploadClick, onDelete
|
|||||||
backgroundColor: theme.palette.background.default,
|
backgroundColor: theme.palette.background.default,
|
||||||
border: `1px solid ${isDropReady ? "red" : theme.palette.grey2.main}`,
|
border: `1px solid ${isDropReady ? "red" : theme.palette.grey2.main}`,
|
||||||
borderRadius: "8px",
|
borderRadius: "8px",
|
||||||
}}>
|
}}
|
||||||
|
>
|
||||||
<ButtonBase
|
<ButtonBase
|
||||||
onClick={imageUrl ? undefined : openImageUploadModal}
|
onClick={imageUrl ? undefined : openImageUploadModal}
|
||||||
sx={{
|
sx={{
|
||||||
@ -75,7 +78,7 @@ export default function FaviconDropZone({ imageUrl, onImageUploadClick, onDelete
|
|||||||
overflow: "hidden",
|
overflow: "hidden",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{imageUrl ?
|
{imageUrl ? (
|
||||||
<img
|
<img
|
||||||
src={imageUrl}
|
src={imageUrl}
|
||||||
style={{
|
style={{
|
||||||
@ -84,17 +87,19 @@ export default function FaviconDropZone({ imageUrl, onImageUploadClick, onDelete
|
|||||||
objectFit: "scale-down",
|
objectFit: "scale-down",
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
:
|
) : (
|
||||||
<UploadIcon />
|
<UploadIcon />
|
||||||
}
|
)}
|
||||||
</ButtonBase>
|
</ButtonBase>
|
||||||
</Box>
|
</Box>
|
||||||
<Box sx={{
|
<Box
|
||||||
|
sx={{
|
||||||
display: "flex",
|
display: "flex",
|
||||||
flexDirection: "column",
|
flexDirection: "column",
|
||||||
alignItems: "start",
|
alignItems: "start",
|
||||||
}}>
|
}}
|
||||||
{imageUrl &&
|
>
|
||||||
|
{imageUrl && (
|
||||||
<ButtonBase onClick={onDeleteClick}>
|
<ButtonBase onClick={onDeleteClick}>
|
||||||
<Typography
|
<Typography
|
||||||
sx={{
|
sx={{
|
||||||
@ -107,7 +112,7 @@ export default function FaviconDropZone({ imageUrl, onImageUploadClick, onDelete
|
|||||||
Удалить
|
Удалить
|
||||||
</Typography>
|
</Typography>
|
||||||
</ButtonBase>
|
</ButtonBase>
|
||||||
}
|
)}
|
||||||
<Typography
|
<Typography
|
||||||
sx={{
|
sx={{
|
||||||
color: theme.palette.orange.main,
|
color: theme.palette.orange.main,
|
||||||
@ -122,4 +127,4 @@ export default function FaviconDropZone({ imageUrl, onImageUploadClick, onDelete
|
|||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
};
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user