diff --git a/src/components/Create/BasicInformation.jsx b/src/components/Create/BasicInformation.jsx index 47a63f1..7f6c93e 100644 --- a/src/components/Create/BasicInformation.jsx +++ b/src/components/Create/BasicInformation.jsx @@ -1,7 +1,6 @@ import React, { useState, useRef, useEffect } from "react"; -import Inputtag from "../InputTag/Usertag"; -import Upload from "../../assets/images/upload.svg"; -import axios from "axios"; +import InputTag from "../InputTag/Usertag"; +import DragAndDropImageUpload from "../../pages/DragAndDropImageUpload"; const BasicInformation = ({ value, @@ -12,94 +11,32 @@ const BasicInformation = ({ setFile, bannerImage, setbannerImage, + DragAndDropImageUpload, }) => { const inputRef = useRef(); const handleChange = (e) => { setValue({ ...value, [e.target.name]: e.target.value }); }; - - const UploadFile = async () => { - const file = inputRef.current.files[0]; - setFile(URL.createObjectURL(file)); - - const formData = new FormData(); - formData.append("image", file); - - try { - const response = await axios.post( - "https://api.imgbb.com/1/upload?key=cc540dc0e2847dccaa0d727a71651587", - formData, - ); - setbannerImage(response.data.data.display_url); - console.log("Image uploaded: ", response.data.data.display_url); - } catch (error) { - console.log("Error uploading image: ", error); - } - }; - + const UserImage = () => { return ( <> -

- Banner Image -

-
-
-
- {file ? ( - - ) : ( - { - inputRef.current.click(); - e.preventDefault(); - }} - /> - )} -
-
-

- {file ? null : ( - <> -

- JPG, JPEG, PNG file size no more than 10MB -

-

- Keep the image ratio to 280x180 px -

- - )} -

- {file && ( -
setFile(null)} - > - Remove × -
- )} - Banner Image +
+
); }; + return ( -
+

@@ -150,7 +87,6 @@ const BasicInformation = ({ type="text" name="company" id="name" - list="companySuggestions" placeholder="Company's name" value={value.company} onChange={handleChange} @@ -181,27 +117,23 @@ const BasicInformation = ({

Title

-
-
- -
-
+

- +
diff --git a/src/pages/Create.jsx b/src/pages/Create.jsx index 507423b..be766c9 100644 --- a/src/pages/Create.jsx +++ b/src/pages/Create.jsx @@ -12,6 +12,8 @@ import WriteHere from "../components/Create/WriteHere"; import SubmittedCard from "../components/Create/SubmittedCard"; import PreviewPage from "../components/Create/PreviewPage"; import useErrorToast from "../hooks/useErrorToast"; +import DragAndDropImageUpload from "../pages/DragAndDropImageUpload"; + const Create = () => { const initialState = { @@ -192,6 +194,7 @@ const Create = () => { setFile={setFile} bannerImage={bannerImage} setbannerImage={setbannerImage} + DragAndDropImageUpload={DragAndDropImageUpload} /> )} {step === 2 && } diff --git a/src/pages/DragAndDropImageUpload.jsx b/src/pages/DragAndDropImageUpload.jsx new file mode 100644 index 0000000..f92a2c3 --- /dev/null +++ b/src/pages/DragAndDropImageUpload.jsx @@ -0,0 +1,128 @@ +import React, { useState } from "react"; +import Upload from "../../src/assets/images/upload.svg"; + +const DragAndDropImageUpload = ({ file, setFile, setbannerImage }) => { + const [isDragging, setIsDragging] = useState(false); + const inputRef = React.useRef(); + + const handleDragEnter = (e) => { + e.preventDefault(); + e.stopPropagation(); + setIsDragging(true); + }; + + const handleDragOver = (e) => { + e.preventDefault(); + e.stopPropagation(); + setIsDragging(true); + }; + + const handleDragLeave = (e) => { + e.preventDefault(); + e.stopPropagation(); + setIsDragging(false); + }; + + const handleDrop = (e) => { + e.preventDefault(); + e.stopPropagation(); + setIsDragging(false); + + const droppedFile = e.dataTransfer.files[0]; + if (droppedFile && droppedFile.type.startsWith("image/")) { + setFile(droppedFile); + const reader = new FileReader(); + reader.onloadend = () => { + setbannerImage(reader.result); // Set the image preview using base64 + }; + reader.readAsDataURL(droppedFile); + } else { + alert("Please drop an image file."); + } + }; + + const handleFileChange = (e) => { + const selectedFile = e.target.files[0]; + if (selectedFile && selectedFile.type.startsWith("image/")) { + setFile(selectedFile); + const reader = new FileReader(); + reader.onloadend = () => { + setbannerImage(reader.result); + }; + reader.readAsDataURL(selectedFile); + } else { + alert("Please select an image file."); + } + }; + + return ( +
+
+
+ {file ? ( + Preview + ) : ( + { + inputRef.current.click(); + e.preventDefault(); + }} + alt="Upload" + /> + )} +
+
+

+ {file ? null : ( + <> +

+ JPG, JPEG, PNG file size no more than 10MB +

+

+ Keep the image ratio to 280x180 px +

+ + )} +

+ {file && ( +
setFile(null)} + > + + Remove + + × +
+ )} + + + +
+ ); +}; + +export default DragAndDropImageUpload;