Skip to content

Commit

Permalink
refactor to fire notifications from uploadFile utility method
Browse files Browse the repository at this point in the history
  • Loading branch information
rithviknishad committed Sep 27, 2024
1 parent b5f92aa commit 419d9f5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
12 changes: 1 addition & 11 deletions src/Components/Facility/CoverImageEditModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import Webcam from "react-webcam";
import { FacilityModel } from "./models";
import useWindowDimensions from "../../Common/hooks/useWindowDimensions";
import CareIcon from "../../CAREUI/icons/CareIcon";
import * as Notification from "../../Utils/Notifications.js";
import { useTranslation } from "react-i18next";
import { LocalStorageKeys } from "../../Common/constants";
import DialogModal from "../Common/Dialog";
Expand Down Expand Up @@ -130,26 +129,17 @@ const CoverImageEditModal = ({
"Bearer " + localStorage.getItem(LocalStorageKeys.accessToken),
},
async (xhr: XMLHttpRequest) => {
setIsProcessing(false);
if (xhr.status === 200) {
Success({ msg: "Cover image updated." });
setIsProcessing(false);
setIsCaptureImgBeingUploaded(false);
await sleep(1000);
onSave?.();
closeModal();
} else {
const error = JSON.parse(xhr.responseText);
Notification.Error({
msg: error?.cover_image?.join(" ") || "Something went wrong!",
});
setIsProcessing(false);
}
},
null,
() => {
Notification.Error({
msg: "Network Failure. Please check your internet connectivity.",
});
setIsProcessing(false);
},
);
Expand Down
14 changes: 14 additions & 0 deletions src/Utils/request/uploadFile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Dispatch, SetStateAction } from "react";
import { handleUploadPercentage } from "./utils";
import * as Notification from "../../Utils/Notifications.js";

const uploadFile = (
url: string,
Expand All @@ -19,6 +20,16 @@ const uploadFile = (

xhr.onload = () => {
onLoad(xhr);
if (400 <= xhr.status && xhr.status <= 499) {
const error = JSON.parse(xhr.responseText);
if (typeof error === "object" && !Array.isArray(error)) {
Object.values(error).forEach((msg) => {
Notification.Error({ msg: msg || "Something went wrong!" });
});
} else {
Notification.Error({ msg: error || "Something went wrong!" });
}
}
};

if (setUploadPercent != null) {
Expand All @@ -28,6 +39,9 @@ const uploadFile = (
}

xhr.onerror = () => {
Notification.Error({
msg: "Network Failure. Please check your internet connectivity.",
});
onError();
};
xhr.send(file);
Expand Down

0 comments on commit 419d9f5

Please sign in to comment.