Skip to content

Commit

Permalink
Merge branch 'OmG2011-image_upload_progress_track'
Browse files Browse the repository at this point in the history
  • Loading branch information
sumn2u committed Jun 7, 2024
2 parents 0772640 + 86be8ce commit 95740d7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
42 changes: 35 additions & 7 deletions client/src/ImageUpload/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const ImageUpload = ({ onImageUpload }) => {
const { t } = useTranslation();
const { showSnackbar } = useSnackbar();
const [images, setImages] = useState([]);
const [loading, setLoading] = useState(false);
const [progress, setProgress] = useState(0);

const onDrop = useCallback((acceptedFiles, fileRejections) => {
if (fileRejections.length) {
Expand All @@ -19,20 +21,20 @@ const ImageUpload = ({ onImageUpload }) => {
return;
}
}

const totalImages = images.length + acceptedFiles.length;
if (totalImages > 2) {
showSnackbar(t("error.configuration.image_upload.max"), "error");
return;
}

const newImages = acceptedFiles.map((file) => {
return Object.assign(file, {
preview: URL.createObjectURL(file),
imageName: file.name,
});
});

uploadImages(newImages);
}, [images, onImageUpload, showSnackbar]);

Expand All @@ -44,13 +46,19 @@ const ImageUpload = ({ onImageUpload }) => {
});

try {
setLoading(true);
const response = await axios.post(`${import.meta.env.VITE_SERVER_URL}/upload`, formData, {
headers: {
'Content-Type': 'multipart/form-data'
},
onUploadProgress: (progressEvent) => {
const { loaded, total } = progressEvent;
let percentCompleted = Math.floor((loaded * 100) / total);
setProgress(percentCompleted)
}
});
showSnackbar(response.data.message, 'success');

const uploadedFiles = response.data.files;
const uploadedImages = uploadedFiles.map(file => ({
preview: file.url,
Expand All @@ -59,13 +67,16 @@ const ImageUpload = ({ onImageUpload }) => {
setImages(uploadedImages);
onImageUpload(uploadedImages);
} catch (error) {
if(error?.data){
if (error?.data) {
showSnackbar(error.data.message, 'error');
}else {
showSnackbar(t("error.server_connection"), 'error')
}
console.error('Error uploading images:', error);
}
finally {
setLoading(false);
}
};

const deleteImage = async (filename) => {
Expand All @@ -86,7 +97,7 @@ const ImageUpload = ({ onImageUpload }) => {
console.error('Error deleting image:', error);
}
};

const handleRemoveImage = (index) => {
const imageToRemove = images[index];
deleteImage(imageToRemove.filename);
Expand Down Expand Up @@ -122,7 +133,24 @@ const ImageUpload = ({ onImageUpload }) => {
{isDragActive ? (
<Typography sx={{fontSize: "14px", color: "rgb(117, 117, 117)" }}>{t("configuration.image_upload.file_drop")}</Typography>
) : (
<Typography sx={{fontSize: "14px", color: "rgb(117, 117, 117)" }}>{t("configuration.image_upload.description")}</Typography>
<>
{loading ? (
<>
{progress > 0 && progress < 100 ? (
<>
<progress value={progress} max={100} />
<p>{progress}%</p>
</>
) : (
<div className="loading">{t("loading")}</div>
)}
</>
) : (
<Typography sx={{fontSize: "14px", color: "rgb(117, 117, 117)" }}>
{t("configuration.image_upload.description")}
</Typography>
)}
</>
)}
</Box>
<Box display="flex" flexWrap="wrap" gap="1rem">
Expand Down
3 changes: 2 additions & 1 deletion client/src/Localization/translation-en-EN.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ const translationEnEN = {
"helptext_circle": "Add Circle",
"comment_placeholder": "Write comment here...",
"image_tags": "Image Tags",
"image_tags_classification_placeholder": "Image Classification"
"image_tags_classification_placeholder": "Image Classification",
"loading": "Loading...",
};

export default translationEnEN;

0 comments on commit 95740d7

Please sign in to comment.