From 159765a84b75cbdb999fda5117adc68b0c8153d4 Mon Sep 17 00:00:00 2001 From: Antony Raj <539454+antonyr@users.noreply.github.com> Date: Wed, 31 Jul 2024 01:34:23 +0800 Subject: [PATCH] Making sure to open modal automatically for cropping the image being uploaded (#207) * Make sure to open modal automatically for cropping * added functionality to show image by using its id * Fix linting issues --- frontend/src/components/ListingForm.tsx | 1 + frontend/src/components/files/UploadImage.tsx | 40 ++++++++++++------- frontend/src/pages/NewListing.tsx | 2 +- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/frontend/src/components/ListingForm.tsx b/frontend/src/components/ListingForm.tsx index da657ff0..ba7762b1 100644 --- a/frontend/src/components/ListingForm.tsx +++ b/frontend/src/components/ListingForm.tsx @@ -104,6 +104,7 @@ const ListingForm: React.FC = ({ onUploadSuccess={(image_id) => handleImageUploadSuccess(image_id, index) } + imageId={image.id} /> void; + imageId?: string | null; } const ImageUploadComponent: React.FC = ({ onUploadSuccess, + imageId, }) => { const [selectedFile, setSelectedFile] = useState(null); const [compressedFile, setCompressedFile] = useState(null); @@ -33,7 +36,15 @@ const ImageUploadComponent: React.FC = ({ const [crop, setCrop] = useState(); const [completedCrop, setCompletedCrop] = useState(null); const imgRef = useRef(null); + const [imageUrl, setImageUrl] = useState(); + useEffect(() => { + if (selectedFile) setImageUrl(URL.createObjectURL(selectedFile)); + }, [selectedFile]); + useEffect(() => { + if (imageId) + setImageUrl(new URL(`/images/${imageId}/large`, BACKEND_URL).toString()); + }, []); const handleFileChange = async (file: File) => { setUploadStatus(null); @@ -65,6 +76,7 @@ const ImageUploadComponent: React.FC = ({ setFileError("Error compressing the image"); setSelectedFile(null); } + setShowModal(true); }; const onDrop = (acceptedFiles: FileWithPath[]) => { @@ -108,7 +120,7 @@ const ImageUploadComponent: React.FC = ({ const handleClick = (event: React.MouseEvent) => { event.stopPropagation(); - if (selectedFile) { + if (imageUrl) { setShowModal(true); } else { triggerFileInput(); @@ -190,14 +202,14 @@ const ImageUploadComponent: React.FC = ({ }; useEffect(() => { - if (selectedFile) setInitialSetter(true); + if (imageUrl) setInitialSetter(true); }, [showModal]); return ( - {selectedFile ? ( + {imageUrl ? ( <> = ({ onComplete={handleCropComplete} > Crop preview @@ -222,7 +234,11 @@ const ImageUploadComponent: React.FC = ({ > Close - + Done @@ -256,13 +272,13 @@ const ImageUploadComponent: React.FC = ({ alignItems: "center", marginBottom: "10px", overflow: "hidden", - cursor: selectedFile ? "pointer" : "default", + cursor: imageUrl ? "pointer" : "default", }} onClick={handleClick} > - {selectedFile ? ( + {imageUrl ? ( Selected @@ -291,7 +307,7 @@ const ImageUploadComponent: React.FC = ({ event.stopPropagation(); setShowModal(true); }} - disabled={selectedFile ? false : true} + disabled={imageUrl ? false : true} variant={theme === "dark" ? "outline-light" : "outline-dark"} > Edit @@ -307,11 +323,7 @@ const ImageUploadComponent: React.FC = ({ {fileError && {fileError}} - + Upload {uploadStatus && ( diff --git a/frontend/src/pages/NewListing.tsx b/frontend/src/pages/NewListing.tsx index 52ca67d4..6f79734f 100644 --- a/frontend/src/pages/NewListing.tsx +++ b/frontend/src/pages/NewListing.tsx @@ -30,7 +30,7 @@ const NewListing: React.FC = () => { artifact_ids: artifacts.map((artifact) => artifact.id), child_ids, }); - setMessage(`Listing added successfully.`); + setMessage("Listing added successfully."); navigate("/listings/me/1"); } catch (error) { setMessage("Error adding Listing ");