@@ -8,6 +8,8 @@ import { useSnackbar } from '../SnackbarContext';
8
8
const ImageUpload = ( { onImageUpload } ) => {
9
9
const { showSnackbar } = useSnackbar ( ) ;
10
10
const [ images , setImages ] = useState ( [ ] ) ;
11
+ const [ loading , setLoading ] = useState ( false ) ;
12
+ const [ progress , setProgress ] = useState ( 0 ) ;
11
13
12
14
const onDrop = useCallback ( ( acceptedFiles , fileRejections ) => {
13
15
if ( fileRejections . length ) {
@@ -17,20 +19,20 @@ const ImageUpload = ({ onImageUpload }) => {
17
19
return ;
18
20
}
19
21
}
20
-
22
+
21
23
const totalImages = images . length + acceptedFiles . length ;
22
24
if ( totalImages > 2 ) {
23
25
showSnackbar ( "You can only upload up to 2 images" , "error" ) ;
24
26
return ;
25
27
}
26
-
28
+
27
29
const newImages = acceptedFiles . map ( ( file ) => {
28
30
return Object . assign ( file , {
29
31
preview : URL . createObjectURL ( file ) ,
30
32
imageName : file . name ,
31
33
} ) ;
32
34
} ) ;
33
-
35
+
34
36
uploadImages ( newImages ) ;
35
37
} , [ images , onImageUpload , showSnackbar ] ) ;
36
38
@@ -42,13 +44,19 @@ const ImageUpload = ({ onImageUpload }) => {
42
44
} ) ;
43
45
44
46
try {
47
+ setLoading ( true ) ;
45
48
const response = await axios . post ( `${ import . meta. env . VITE_SERVER_URL } /upload` , formData , {
46
49
headers : {
47
50
'Content-Type' : 'multipart/form-data'
51
+ } ,
52
+ onUploadProgress : ( progressEvent ) => {
53
+ const { loaded, total } = progressEvent ;
54
+ let percentCompleted = Math . floor ( ( loaded * 100 ) / total ) ;
55
+ setProgress ( percentCompleted )
48
56
}
49
57
} ) ;
50
58
showSnackbar ( response . data . message , 'success' ) ;
51
-
59
+
52
60
const uploadedFiles = response . data . files ;
53
61
const uploadedImages = uploadedFiles . map ( file => ( {
54
62
preview : file . url ,
@@ -57,13 +65,16 @@ const ImageUpload = ({ onImageUpload }) => {
57
65
setImages ( uploadedImages ) ;
58
66
onImageUpload ( uploadedImages ) ;
59
67
} catch ( error ) {
60
- if ( error ?. data ) {
68
+ if ( error ?. data ) {
61
69
showSnackbar ( error . data . message , 'error' ) ;
62
- } else {
70
+ } else {
63
71
showSnackbar ( "Couldn't connect server" , 'error' )
64
72
}
65
73
console . error ( 'Error uploading images:' , error ) ;
66
74
}
75
+ finally {
76
+ setLoading ( false ) ;
77
+ }
67
78
} ;
68
79
69
80
const deleteImage = async ( filename ) => {
@@ -84,7 +95,7 @@ const ImageUpload = ({ onImageUpload }) => {
84
95
console . error ( 'Error deleting image:' , error ) ;
85
96
}
86
97
} ;
87
-
98
+
88
99
const handleRemoveImage = ( index ) => {
89
100
const imageToRemove = images [ index ] ;
90
101
deleteImage ( imageToRemove . filename ) ;
@@ -120,7 +131,17 @@ const ImageUpload = ({ onImageUpload }) => {
120
131
{ isDragActive ? (
121
132
< Typography > Drop the files here...</ Typography >
122
133
) : (
123
- < Typography > Drag 'n' drop some files here, or click to select files (up to 2)</ Typography >
134
+ loading ?
135
+ progress > 0 && progress < 100 ? (
136
+ < div >
137
+ < progress value = { progress } max = { 100 } />
138
+ < p > { progress } %</ p >
139
+ </ div >
140
+ ) : (
141
+ < div className = "loading" > Loading...</ div >
142
+ )
143
+ :
144
+ < Typography > Drag 'n' drop some files here, or click to select files (up to 2)</ Typography >
124
145
) }
125
146
</ Box >
126
147
< Box display = "flex" flexWrap = "wrap" gap = "1rem" >
0 commit comments