Skip to content

Commit

Permalink
refactor: improve deleteUser error handling and remove wait
Browse files Browse the repository at this point in the history
  • Loading branch information
paabloLC committed Feb 19, 2025
1 parent dd4294b commit 52804be
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions ui/actions/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,24 +157,42 @@ export const updateUserRole = async (formData: FormData) => {
export const deleteUser = async (formData: FormData) => {
const session = await auth();
const keyServer = process.env.API_BASE_URL;

const userId = formData.get("userId");

if (!userId) {
return { error: "User ID is required" };
}

const url = new URL(`${keyServer}/users/${userId}`);

try {
const response = await fetch(url.toString(), {
method: "DELETE",
headers: {
Authorization: `Bearer ${session?.accessToken}`,
},
});
const data = await response.json();
await wait(1000);

if (!response.ok) {
try {
const errorData = await response.json();
throw new Error(errorData?.message || "Failed to delete the user");
} catch {
throw new Error("Failed to delete the user");
}
}

let data = null;
if (response.status !== 204) {
data = await response.json();
}

revalidatePath("/users");
return parseStringify(data);
return data || { success: true };
} catch (error) {
return {
error: getErrorMessage(error),
};
// eslint-disable-next-line no-console
console.error("Error deleting user:", error);
return { error: getErrorMessage(error) };
}
};

Expand Down

0 comments on commit 52804be

Please sign in to comment.