Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Bugs in User and Facility Avatar Upload and Deletion Workflow #10547

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
baf7c34
Fix User Avatar Delete Workflow
rajku-dev Feb 10, 2025
47fc60d
Fix Lint
rajku-dev Feb 11, 2025
4f01089
Merge branch 'develop' into issue/10525/user-avatat-delete-bug
rajku-dev Feb 11, 2025
b4a21bb
Disable `Save` button after upading/uploading cover facility cover photo
rajku-dev Feb 11, 2025
a794120
fix `Uplaoding` not showing on clicking save button
rajku-dev Feb 11, 2025
d94134d
fix uplaoding button glitch after deleting facility avatar
rajku-dev Feb 11, 2025
d000c7d
Merge branch 'develop' into issue/10525/user-avatat-delete-bug
rajku-dev Feb 11, 2025
f7c6426
Merge branch 'develop' into issue/10525/user-avatat-delete-bug
rajku-dev Feb 12, 2025
ffd91d9
fix preview not showing
rajku-dev Feb 13, 2025
9b1034e
Merge branch 'issue/10525/user-avatat-delete-bug' of https://github.c…
rajku-dev Feb 13, 2025
c0ecfc5
fix preview not showing in FacilityAvatar
rajku-dev Feb 13, 2025
bd87f6f
fix lint
rajku-dev Feb 13, 2025
85c872a
remove console log
rajku-dev Feb 13, 2025
b42c183
Merge branch 'develop' into issue/10525/user-avatat-delete-bug
rajku-dev Feb 13, 2025
c04c9f8
Merge branch 'develop' into issue/10525/user-avatat-delete-bug
rajku-dev Feb 13, 2025
0467610
fix merge conflict errors
rajku-dev Feb 13, 2025
2d89b35
Merge branch 'develop' into issue/10525/user-avatat-delete-bug
rajku-dev Feb 13, 2025
7202c2a
Merge branch 'develop' into issue/10525/user-avatat-delete-bug
nihal467 Feb 18, 2025
e4cf424
Merge branch 'develop' into issue/10525/user-avatat-delete-bug
rajku-dev Feb 18, 2025
f7dcfc7
Merge branch 'develop' into issue/10525/user-avatat-delete-bug
rajku-dev Feb 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -128,24 +132,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 @@ -142,24 +142,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 @@ -170,9 +175,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
Loading