From 368ea80e133751588e082297146057df357dcd7d Mon Sep 17 00:00:00 2001 From: hlokomani-tuks <133780278+hlokomani-tuks@users.noreply.github.com> Date: Tue, 1 Oct 2024 05:19:07 +0200 Subject: [PATCH] Ensured only fields that have been updated are appended --- frontend/lib/api/updateProfile.tsx | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/frontend/lib/api/updateProfile.tsx b/frontend/lib/api/updateProfile.tsx index 17d81a00..7ebec037 100644 --- a/frontend/lib/api/updateProfile.tsx +++ b/frontend/lib/api/updateProfile.tsx @@ -16,9 +16,10 @@ const updateUserProfile = async ( } const formData = new FormData(); - formData.append("fullname", updatedUser.fullname); - formData.append("username", updatedUser.username); - formData.append("bio", updatedUser.bio); + + if (updatedUser.fullname) formData.append("fullname", updatedUser.fullname); + if (updatedUser.username) formData.append("username", updatedUser.username); + if (updatedUser.bio) formData.append("bio", updatedUser.bio); if (updatedUser.location) { formData.append("location", JSON.stringify(updatedUser.location)); } @@ -29,6 +30,11 @@ const updateUserProfile = async ( formData.append("profile_picture", file); } + // Don't send the request if there's nothing to update + if (formData.entries().next().done) { + return user; // Return the original user if no updates + } + const url = `${process.env.NEXT_PUBLIC_BACKEND_URL}/api/users/${user.user_id}`; const response = await fetch(url, { method: "PUT",