Skip to content

Commit

Permalink
Fix Bugs in User and Facility Avatar Upload and Deletion Workflow (#1…
Browse files Browse the repository at this point in the history
…0547)

Co-authored-by: Mohammed Nihal <57055998+nihal467@users.noreply.github.com>
  • Loading branch information
rajku-dev and nihal467 authored Feb 21, 2025
1 parent 52923a4 commit 57ad12d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
40 changes: 28 additions & 12 deletions src/components/Common/AvatarEditModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ interface Props {
open: boolean;
onOpenChange: (open: boolean) => void;
imageUrl?: string;
handleUpload: (file: File, onError: () => void) => Promise<void>;
handleDelete: (onError: () => void) => Promise<void>;
handleUpload: (
file: File,
onSuccess: () => void,
onError: () => void,
) => Promise<void>;
handleDelete: (onSuccess: () => void, onError: () => void) => Promise<void>;
hint?: React.ReactNode;
}

Expand Down Expand Up @@ -147,24 +151,36 @@ const AvatarEditModal = ({

setIsProcessing(true);
setIsCaptureImgBeingUploaded(true);
await handleUpload(selectedFile, () => {
setSelectedFile(undefined);
setPreview(undefined);
setPreviewImage(null);
setIsCaptureImgBeingUploaded(false);
setIsProcessing(false);
});
await handleUpload(
selectedFile,
() => {
setPreview(undefined);
},
() => {
setPreview(undefined);
setPreviewImage(null);
setIsCaptureImgBeingUploaded(false);
setIsProcessing(false);
},
);
} finally {
setPreview(undefined);
setIsCaptureImgBeingUploaded(false);
setIsProcessing(false);
setSelectedFile(undefined);
}
};

const deleteAvatar = async () => {
setIsProcessing(true);
await handleDelete(() => {
setIsProcessing(false);
});
await handleDelete(
() => {
setIsProcessing(false);
setPreview(undefined);
setPreviewImage(null);
},
() => setIsProcessing(false),
);
};

const dragProps = useDragAndDrop();
Expand Down
17 changes: 13 additions & 4 deletions src/components/Facility/FacilityHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,29 @@ export const FacilityHome = ({ facilityId }: Props) => {
},
});

const handleCoverImageUpload = async (file: File, onError: () => void) => {
const handleCoverImageUpload = async (
file: File,
onSuccess: () => void,
onError: () => void,
) => {
const formData = new FormData();
formData.append("cover_image", file);
const url = `${careConfig.apiUrl}/api/v1/facility/${facilityId}/cover_image/`;

uploadFile(
await uploadFile(
url,
formData,
"POST",
{ Authorization: getAuthorizationHeader() },
async (xhr: XMLHttpRequest) => {
if (xhr.status === 200) {
setEditCoverImage(false);
await sleep(1000);
queryClient.invalidateQueries({
queryKey: ["facility", facilityId],
});
toast.success(t("cover_image_updated"));
setEditCoverImage(false);
onSuccess();
} else {
onError();
}
Expand All @@ -138,9 +143,13 @@ export const FacilityHome = ({ facilityId }: Props) => {
},
);
};
const handleCoverImageDelete = async (onError: () => void) => {
const handleCoverImageDelete = async (
onSuccess: () => void,
onError: () => void,
) => {
try {
await deleteAvatar();
onSuccess();
} catch {
onError();
}
Expand Down
19 changes: 14 additions & 5 deletions src/components/Users/UserAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function UserAvatar({ username }: { username: string }) {
const authUser = useAuthUser();
const queryClient = useQueryClient();

const { mutate: mutateAvatarDelete } = useMutation({
const { mutateAsync: mutateAvatarDelete } = useMutation({
mutationFn: mutate(routes.deleteProfilePicture, {
pathParams: { username },
}),
Expand All @@ -52,7 +52,11 @@ export default function UserAvatar({ username }: { username: string }) {
return <Loading />;
}

const handleAvatarUpload = async (file: File, onError: () => void) => {
const handleAvatarUpload = async (
file: File,
onSuccess: () => void,
onError: () => void,
) => {
const formData = new FormData();
formData.append("profile_picture", file);
const url = `${careConfig.apiUrl}/api/v1/users/${userData.username}/profile_picture/`;
Expand All @@ -64,6 +68,7 @@ export default function UserAvatar({ username }: { username: string }) {
{ Authorization: getAuthorizationHeader() },
async (xhr: XMLHttpRequest) => {
if (xhr.status === 200) {
setEditAvatar(false);
await sleep(1000);
queryClient.invalidateQueries({
queryKey: ["getUserDetails", username],
Expand All @@ -72,7 +77,6 @@ export default function UserAvatar({ username }: { username: string }) {
queryClient.invalidateQueries({ queryKey: ["currentUser"] });
}
toast.success(t("avatar_updated_success"));
setEditAvatar(false);
}
},
null,
Expand All @@ -82,13 +86,18 @@ export default function UserAvatar({ username }: { username: string }) {
);
};

const handleAvatarDelete = async (onError: () => void) => {
const handleAvatarDelete = async (
onSuccess: () => void,
onError: () => void,
) => {
try {
mutateAvatarDelete();
await mutateAvatarDelete();
onSuccess();
} catch {
onError();
}
};

return (
<>
<AvatarEditModal
Expand Down

0 comments on commit 57ad12d

Please sign in to comment.