diff --git a/src/pages/Questions/UploadImage.tsx b/src/pages/Questions/UploadImage.tsx index e59f8a15..062cadc4 100644 --- a/src/pages/Questions/UploadImage.tsx +++ b/src/pages/Questions/UploadImage.tsx @@ -1,3 +1,4 @@ +import { useState } from "react"; import { Typography, Box, @@ -8,6 +9,7 @@ import { InputAdornment, } from "@mui/material"; import UploadBox from "@ui_kit/UploadBox"; +import { CroppingModal } from "@ui_kit/Modal/CroppingModal"; import UploadIcon from "../../assets/icons/UploadIcon"; import SearchIcon from "../../assets/icons/SearchIcon"; import * as React from "react"; @@ -169,9 +171,11 @@ export default function UploadImage() { const [file] = fileArray; setImg(URL.createObjectURL(file)); handleClose(); + setOpened(true); } }; - const [img, setImg] = React.useState(""); + const [opened, setOpened] = useState(false); + const [img, setImg] = useState(""); const handleDrop = (event: DragEvent) => { event.preventDefault(); @@ -196,20 +200,12 @@ export default function UploadImage() { onClick={handleOpen} sx={{ width: "100%", maxWidth: "260px" }} > - {img ? ( - img - ) : ( - } - text="5 MB максимум" - /> - )} + } + text="5 MB максимум" + /> + setOpened(false)} + picture={img} + /> ); } diff --git a/src/ui_kit/Modal/CroppingModal.tsx b/src/ui_kit/Modal/CroppingModal.tsx index 0da3cf5e..2c59daa6 100644 --- a/src/ui_kit/Modal/CroppingModal.tsx +++ b/src/ui_kit/Modal/CroppingModal.tsx @@ -1,8 +1,16 @@ -import React, { FC, useRef, useState } from "react"; +import React, { FC, useEffect, useRef, useState } from "react"; import { saveAs } from "file-saver"; import ReactCrop, { Crop } from "react-image-crop"; import "react-image-crop/dist/ReactCrop.css"; -import { Box, Button, Modal, Slider, Typography, useMediaQuery, useTheme } from "@mui/material"; +import { + Box, + Button, + Modal, + Slider, + Typography, + useMediaQuery, + useTheme, +} from "@mui/material"; import quiz from "../../assets/quiz-template-6.png"; import { ResetIcon } from "@icons/ResetIcon"; @@ -10,9 +18,10 @@ import { ResetIcon } from "@icons/ResetIcon"; interface Iprops { opened: boolean; onClose: () => void; + picture?: string | ArrayBuffer; } -export const CroppingModal: FC = ({ opened, onClose }) => { +export const CroppingModal: FC = ({ opened, onClose, picture }) => { const theme = useTheme(); const isMobile = useMediaQuery(theme.breakpoints.down(786)); @@ -54,7 +63,13 @@ export const CroppingModal: FC = ({ opened, onClose }) => { }; const [src, setSrc] = useState(quiz); - const [crop, setCrop] = useState({ unit: "px", y: 0, x: 0, width: 100, height: 100 }); + const [crop, setCrop] = useState({ + unit: "px", + y: 0, + x: 0, + width: 100, + height: 100, + }); const [completedCrop, setCompletedCrop] = useState(null); const [imageSize, setImageSize] = useState(580); const [darken, setDarken] = useState(0); @@ -62,6 +77,12 @@ export const CroppingModal: FC = ({ opened, onClose }) => { console.log(src); + useEffect(() => { + if (picture) { + setSrc(picture); + } + }, [picture]); + const onCropComplete = (crop: Crop) => { setCompletedCrop(crop); }; @@ -81,7 +102,12 @@ export const CroppingModal: FC = ({ opened, onClose }) => { const handleDownloadClick = async () => { if (completedCrop && src) { - const croppedImageUrl = await getCroppedAndDarkenedImg(src, completedCrop, "cropped.jpeg", darken); + const croppedImageUrl = await getCroppedAndDarkenedImg( + src, + completedCrop, + "cropped.jpeg", + darken + ); saveAs(croppedImageUrl, "cropped-image.jpeg"); } }; @@ -178,7 +204,11 @@ export const CroppingModal: FC = ({ opened, onClose }) => { justifyContent: "center", }} > - setCrop(newCrop)} onComplete={onCropComplete}> + setCrop(newCrop)} + onComplete={onCropComplete} + > {src && ( = ({ opened, onClose }) => { marginBottom: "50px", }} > - {Math.round(crop.width)}x - {Math.round(crop.width)} + + {Math.round(crop.width)} + + x + + {Math.round(crop.width)} + px - + { setCrop((prevCrop: Crop) => ({ @@ -228,7 +269,9 @@ export const CroppingModal: FC = ({ opened, onClose }) => { style={{ marginBottom: "10px", cursor: "pointer" }} /> - Размер + + Размер + = ({ opened, onClose }) => { /> - Затемнение + + Затемнение + = ({ opened, onClose }) => { /> - +