Skip to content

Commit e1efad9

Browse files
committed
Added a progress tracking bar using the built-in browser <progress> component.
1 parent 1b4ef62 commit e1efad9

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

client/src/ImageUpload/index.jsx

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { useSnackbar } from '../SnackbarContext';
88
const ImageUpload = ({ onImageUpload }) => {
99
const { showSnackbar } = useSnackbar();
1010
const [images, setImages] = useState([]);
11+
const [loading, setLoading] = useState(false);
12+
const [progress, setProgress] = useState(0);
1113

1214
const onDrop = useCallback((acceptedFiles, fileRejections) => {
1315
if (fileRejections.length) {
@@ -17,20 +19,20 @@ const ImageUpload = ({ onImageUpload }) => {
1719
return;
1820
}
1921
}
20-
22+
2123
const totalImages = images.length + acceptedFiles.length;
2224
if (totalImages > 2) {
2325
showSnackbar("You can only upload up to 2 images", "error");
2426
return;
2527
}
26-
28+
2729
const newImages = acceptedFiles.map((file) => {
2830
return Object.assign(file, {
2931
preview: URL.createObjectURL(file),
3032
imageName: file.name,
3133
});
3234
});
33-
35+
3436
uploadImages(newImages);
3537
}, [images, onImageUpload, showSnackbar]);
3638

@@ -42,13 +44,19 @@ const ImageUpload = ({ onImageUpload }) => {
4244
});
4345

4446
try {
47+
setLoading(true);
4548
const response = await axios.post(`${import.meta.env.VITE_SERVER_URL}/upload`, formData, {
4649
headers: {
4750
'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)
4856
}
4957
});
5058
showSnackbar(response.data.message, 'success');
51-
59+
5260
const uploadedFiles = response.data.files;
5361
const uploadedImages = uploadedFiles.map(file => ({
5462
preview: file.url,
@@ -57,13 +65,16 @@ const ImageUpload = ({ onImageUpload }) => {
5765
setImages(uploadedImages);
5866
onImageUpload(uploadedImages);
5967
} catch (error) {
60-
if(error?.data){
68+
if (error?.data) {
6169
showSnackbar(error.data.message, 'error');
62-
}else {
70+
} else {
6371
showSnackbar("Couldn't connect server", 'error')
6472
}
6573
console.error('Error uploading images:', error);
6674
}
75+
finally {
76+
setLoading(false);
77+
}
6778
};
6879

6980
const deleteImage = async (filename) => {
@@ -84,7 +95,7 @@ const ImageUpload = ({ onImageUpload }) => {
8495
console.error('Error deleting image:', error);
8596
}
8697
};
87-
98+
8899
const handleRemoveImage = (index) => {
89100
const imageToRemove = images[index];
90101
deleteImage(imageToRemove.filename);
@@ -120,7 +131,17 @@ const ImageUpload = ({ onImageUpload }) => {
120131
{isDragActive ? (
121132
<Typography>Drop the files here...</Typography>
122133
) : (
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>
124145
)}
125146
</Box>
126147
<Box display="flex" flexWrap="wrap" gap="1rem">

0 commit comments

Comments
 (0)