@@ -8,7 +8,7 @@ import { WithContext as ReactTags } from 'react-tag-input';
88import { useDropzone } from 'react-dropzone' ;
99import * as yup from 'yup' ;
1010import '../tags.css' ;
11- import { ref , uploadBytes , getDownloadURL } from 'firebase/storage' ;
11+ import { ref , uploadBytesResumable , getDownloadURL } from 'firebase/storage' ;
1212
1313const KeyCodes = {
1414 comma : 188 ,
@@ -28,6 +28,7 @@ function AddProject() {
2828 const [ selectedImages , setSelectedImages ] = useState ( [ ] ) ;
2929 const [ droppedImages , setDroppedImages ] = useState ( [ ] ) ;
3030 const [ errorMessage , setErrorMessage ] = useState ( '' ) ;
31+ const [ uploadProgress , setUploadProgress ] = useState ( 0 ) ; // State for progress indicator
3132 const navigate = useNavigate ( ) ;
3233
3334 const saveData = async ( data ) => {
@@ -42,24 +43,42 @@ function AddProject() {
4243 tags : tagsArr ,
4344 category : category ,
4445 } ;
45-
46+
4647 // Upload the image files to Firebase Storage
4748 const storagePromises = imageFiles . map ( ( imageFile ) => {
4849 const storageRef = ref ( storage , 'images/' + imageFile . name ) ;
49- return uploadBytes ( storageRef , imageFile )
50- . then ( ( snapshot ) => getDownloadURL ( snapshot . ref ) )
51- . catch ( ( error ) => {
52- throw new Error ( 'Error uploading image: ' + error . message ) ;
53- } ) ;
50+
51+ // Create a unique upload task for each file
52+ const uploadTask = uploadBytesResumable ( storageRef , imageFile ) ;
53+
54+ // Track the upload progress
55+ uploadTask . on ( 'state_changed' , ( snapshot ) => {
56+ const progress = Math . round (
57+ ( snapshot . bytesTransferred / snapshot . totalBytes ) * 100
58+ ) ;
59+ setUploadProgress ( progress ) ;
60+ } ) ;
61+
62+ // Return a promise that resolves when the upload is complete
63+ return new Promise ( ( resolve , reject ) => {
64+ uploadTask
65+ . then ( ( snapshot ) => {
66+ // Get the download URL after the upload is complete
67+ getDownloadURL ( snapshot . ref )
68+ . then ( ( downloadUrl ) => resolve ( downloadUrl ) )
69+ . catch ( reject ) ;
70+ } )
71+ . catch ( reject ) ;
72+ } ) ;
5473 } ) ;
55-
74+
5675 try {
5776 // Wait for all image uploads to complete
5877 const downloadUrls = await Promise . all ( storagePromises ) ;
59-
78+
6079 // Add the image URLs to the project data
6180 projectData . imageUrls = downloadUrls ;
62-
81+
6382 // Save the project data to Firestore
6483 const docRef = await addDoc ( collection ( db , 'projects' ) , projectData ) ;
6584 navigate ( '/viewprojects' ) ;
@@ -212,6 +231,17 @@ function AddProject() {
212231 </ div >
213232 </ div >
214233 </ div >
234+ { uploadProgress > 0 && (
235+ < div className = "relative pt-1" >
236+ < div className = "overflow-hidden h-2 mb-4 text-xs flex rounded bg-gray-200" >
237+ < div
238+ style = { { width : `${ uploadProgress } %` } }
239+ className = "shadow-none flex flex-col text-center whitespace-nowrap text-white justify-center bg-green-500"
240+ > </ div >
241+ </ div >
242+ < div className = "text-center" > { uploadProgress } %</ div >
243+ </ div >
244+ ) }
215245 </ form >
216246 </ div >
217247 </ section >
0 commit comments