From b7dfb980b29cde7f171c9c2002166fa1aa3c2ee3 Mon Sep 17 00:00:00 2001 From: "B.E.N.S.O.N" Date: Wed, 31 Dec 2025 13:30:30 +0800 Subject: [PATCH] Update for the user function --- src/app/api/user/actions.ts | 4 ++-- src/app/utils/fetchUtil.ts | 8 ++++++-- src/components/UserSearch/UserSearch.tsx | 4 +++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/app/api/user/actions.ts b/src/app/api/user/actions.ts index 5341269..1fb5495 100644 --- a/src/app/api/user/actions.ts +++ b/src/app/api/user/actions.ts @@ -66,7 +66,7 @@ export const editUser = async (id: number, data: UserInputs) => { }; export const createUser = async (data: UserInputs) => { - const newUser = serverFetchWithNoContent(`${BASE_API_URL}/user/save`, { + const newUser = await serverFetchWithNoContent(`${BASE_API_URL}/user/save`, { method: "POST", body: JSON.stringify(data), headers: { "Content-Type": "application/json" }, @@ -76,7 +76,7 @@ export const createUser = async (data: UserInputs) => { }; export const deleteUser = async (id: number) => { - const newUser = serverFetchWithNoContent(`${BASE_API_URL}/user/${id}`, { + const newUser = await serverFetchWithNoContent(`${BASE_API_URL}/user/${id}`, { method: "DELETE", headers: { "Content-Type": "application/json" }, }); diff --git a/src/app/utils/fetchUtil.ts b/src/app/utils/fetchUtil.ts index d1484f8..571fdab 100644 --- a/src/app/utils/fetchUtil.ts +++ b/src/app/utils/fetchUtil.ts @@ -41,8 +41,12 @@ export async function serverFetchWithNoContent(...args: FetchParams) { case 401: signOutUser(); default: - console.error(await response.text()); - throw Error("Something went wrong fetching data in server."); + const errorText = await response.text(); + console.error(`Server error (${response.status}):`, errorText); + throw new ServerFetchError( + `Server error: ${response.status} ${response.statusText}. ${errorText || "Something went wrong fetching data in server."}`, + response + ); } } } diff --git a/src/components/UserSearch/UserSearch.tsx b/src/components/UserSearch/UserSearch.tsx index 30af9cb..a306278 100644 --- a/src/components/UserSearch/UserSearch.tsx +++ b/src/components/UserSearch/UserSearch.tsx @@ -61,9 +61,11 @@ const UserSearch: React.FC = ({ users }) => { const onDeleteClick = useCallback((users: UserResult) => { deleteDialog(async () => { await deleteUser(users.id); + setFilteredUser(prev => prev.filter(user => user.id !== users.id)); + router.refresh(); successDialog(t("Delete Success"), t); }, t); - }, [t]); + }, [t, router]); const columns = useMemo[]>( () => [