From c5fbb1d83fac2d59f9f652b3dedef9dd3bf20c43 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sun, 23 Mar 2025 11:47:10 -0400 Subject: [PATCH] Show better error messages --- src/hooks/AccountHooks.ts | 294 +++++++++++++++++++---------- src/hooks/AuthHooks.ts | 17 +- src/hooks/CalendarHooks.ts | 182 ++++++++++++------ src/hooks/ClassGroupHooks.ts | 146 +++++++++----- src/hooks/GameClassHooks.ts | 150 ++++++++++----- src/hooks/GameHooks.ts | 104 ++++++---- src/hooks/PersonCharacterHooks.ts | 145 +++++++++----- src/hooks/PersonHooks.ts | 121 ++++++++---- src/hooks/RaidGroupHooks.ts | 219 +++++++++++++-------- src/hooks/RaidGroupRequestHooks.ts | 102 ++++++---- src/hooks/RaidInstanceHooks.ts | 184 ++++++++++++------ src/hooks/RaidLayoutHooks.ts | 141 +++++++++----- 12 files changed, 1206 insertions(+), 599 deletions(-) diff --git a/src/hooks/AccountHooks.ts b/src/hooks/AccountHooks.ts index 860cd8a..65dfa5d 100644 --- a/src/hooks/AccountHooks.ts +++ b/src/hooks/AccountHooks.ts @@ -17,16 +17,23 @@ export function useGetAccounts(page: number, pageSize: number, searchTerm?: stri params.append("searchTerm", searchTerm ?? ""); } - const response = await api.get(`/account?${params}`); + try{ + const response = await api.get(`/account?${params}`); - if(response.status !== 200){ - throw new Error("Failed to get accounts"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as Account[]; + return response.data as Account[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -42,16 +49,23 @@ export function useGetAccountsByRaidGroup(raidGroupId: string, page: number, pag params.append("searchTerm", searchTerm ?? ""); } - const response = await api.get(`/account/raidGroup/${raidGroupId}?${params}`); + try{ + const response = await api.get(`/account/raidGroup/${raidGroupId}?${params}`); - if(response.status !== 200){ - throw new Error("Failed to get accounts"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as Account[]; + return response.data as Account[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -60,16 +74,23 @@ export function useGetRaidGroupPermissionsForAccount(raidGroupId?: string, accou return useQuery({ queryKey: ["accounts", "raidGroup", raidGroupId, "account", accountId], queryFn: async () => { - const response = await api.get(`/account/${accountId}/raidGroup/${raidGroupId}/permission`); + try{ + const response = await api.get(`/account/${accountId}/raidGroup/${raidGroupId}/permission`); - if(response.status !== 200){ - throw new Error("Failed to get permissions"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data.permission as RaidGroupPermissionType; + return response.data.permission as RaidGroupPermissionType; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } }, enabled: !!raidGroupId && !!accountId }); @@ -86,17 +107,23 @@ export function useGetAccountsCount(searchTerm?: string){ return useQuery({ queryKey: [ "accounts", "count", searchTerm], queryFn: async () => { + try{ + const response = await api.get(`/account/count?${searchParams}`); - const response = await api.get(`/account/count?${searchParams}`); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - if(response.status !== 200){ - throw new Error("Failed to get accounts count"); + return response.data.count as number; } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } - - return response.data.count as number; } }); } @@ -111,17 +138,23 @@ export function useGetAccountsByRaidGroupCount(raidGroupId: string, searchTerm?: return useQuery({ queryKey: [ "accounts", "raidGroup", raidGroupId, "count", searchTerm], queryFn: async () => { + try{ + const response = await api.get(`/account/raidGroup/${raidGroupId}/count?${searchParams}`); - const response = await api.get(`/account/raidGroup/${raidGroupId}/count?${searchParams}`); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - if(response.status !== 200){ - throw new Error("Failed to get accounts count"); + return response.data.count as number; } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } - - return response.data.count as number; } }); } @@ -130,16 +163,23 @@ export function useGetTutorialsStatus(accountId: string | null){ return useQuery({ queryKey: ["tutorials", "account", accountId], queryFn: async () => { - const response = await api.get(`/account/tutorial`); + try{ + const response = await api.get(`/account/tutorial`); - if(response.status !== 200){ - throw new Error("Failed to get tutorials status"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as AccountTutorialStatus; + return response.data as AccountTutorialStatus; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } }, enabled: !!accountId }); @@ -149,13 +189,20 @@ export function useUpdateTutorialsStatus(){ return useMutation({ mutationKey: ["tutorials", "accounts"], mutationFn: async (tutorials: AccountTutorialStatus) => { - const response = await api.put(`/account/tutorial`, tutorials); + try{ + const response = await api.put(`/account/tutorial`, tutorials); - if(response.status !== 200){ - throw new Error("Failed to update tutorials status"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } } }); @@ -192,13 +239,20 @@ export function useForcePasswordReset(accountId: string){ return useMutation({ mutationKey: ["forcePasswordReset", accountId], mutationFn: async () => { - const response = await api.put(`/account/${accountId}/forcePasswordReset`); + try{ + const response = await api.put(`/account/${accountId}/forcePasswordReset`); - if(response.status !== 200){ - throw new Error("Failed to force password reset"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -214,15 +268,22 @@ export function useResetPassword(accountId: string){ return useMutation({ mutationKey: ["resetPassword", accountId], mutationFn: async (password: string) => { - const response = await api.put(`/account/${accountId}/resetPassword`, { - password - }); + try{ + const response = await api.put(`/account/${accountId}/resetPassword`, { + password + }); - if(response.status !== 200){ - throw new Error("Failed to reset password"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -238,13 +299,20 @@ export function useRevokeRefreshToken(accountId: string){ return useMutation({ mutationKey: ["revokeRefreshToken", accountId], mutationFn: async () => { - const response = await api.put(`/account/${accountId}/revokeRefreshToken`); + try{ + const response = await api.put(`/account/${accountId}/revokeRefreshToken`); - if(response.status !== 200){ - throw new Error("Failed to revoke refresh token"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -260,13 +328,20 @@ export function useCreateAccount(){ return useMutation({ mutationKey: ["createAccount"], mutationFn: async (account: Account) => { - const response = await api.post("/account", account); + try{ + const response = await api.post("/account", account); - if(response.status !== 200){ - throw new Error("Failed to create account"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -282,13 +357,20 @@ export function useUpdateAccount(){ return useMutation({ mutationKey: ["updateAccount"], mutationFn: async (account: Account) => { - const response = await api.put(`/account/${account.accountId}`, account); + try{ + const response = await api.put(`/account/${account.accountId}`, account); - if(response.status !== 200){ - throw new Error("Failed to update account"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -304,15 +386,22 @@ export function useUpdateRaidGroupPermissionsForAccount(raidGroupId?: string, ac return useMutation({ mutationKey: ["updateRaidGroupPermissionsForAccount", raidGroupId, accountId], mutationFn: async (permission: RaidGroupPermissionType) => { - const response = await api.put(`/account/${accountId}/raidGroup/${raidGroupId}/permission`, { - permission - }); + try{ + const response = await api.put(`/account/${accountId}/raidGroup/${raidGroupId}/permission`, { + permission + }); - if(response.status !== 200){ - throw new Error("Failed to update permissions"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -328,13 +417,20 @@ export function useDeleteAccount(accountId: string){ return useMutation({ mutationKey: ["deleteAccount", accountId], mutationFn: async () => { - const response = await api.delete(`/account/${accountId}`); + try{ + const response = await api.delete(`/account/${accountId}`); - if(response.status !== 200){ - throw new Error("Failed to delete account"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -351,18 +447,24 @@ export function useRemoveAccountFromRaidGroup(raidGroupId?: string, accountId?: return useMutation({ mutationKey: ["removeAccountFromRaidGroup", raidGroupId, accountId], mutationFn: async () => { - const response = await api.delete(`/account/${accountId}/raidGroup/${raidGroupId}/permission`); + try{ + const response = await api.delete(`/account/${accountId}/raidGroup/${raidGroupId}/permission`); - if(response.status !== 200){ - throw new Error("Failed to remove account from raid group"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["accounts"] }); } }); - } diff --git a/src/hooks/AuthHooks.ts b/src/hooks/AuthHooks.ts index 0c22d44..60e0bde 100644 --- a/src/hooks/AuthHooks.ts +++ b/src/hooks/AuthHooks.ts @@ -8,13 +8,20 @@ export function useSignup(){ return useMutation({ mutationKey: ["signup"], mutationFn: async (account: Account) => { - const response = await api.post("/auth/signup", account); + try{ + const response = await api.post("/auth/signup", account); - if(response.status !== 200){ - throw new Error("Failed to signup"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } } }); diff --git a/src/hooks/CalendarHooks.ts b/src/hooks/CalendarHooks.ts index c96ca43..27e77c3 100644 --- a/src/hooks/CalendarHooks.ts +++ b/src/hooks/CalendarHooks.ts @@ -1,22 +1,30 @@ import { CalendarEvent } from "@/interface/Calendar"; import { api } from "@/util/AxiosUtil"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { AxiosError } from "axios"; export function useGetGameCalendar(gameId: string){ return useQuery({ queryKey: ["gameCalendar", gameId], queryFn: async () => { - const response = await api.get(`/calendar/game/${gameId}`); + try{ + const response = await api.get(`/calendar/game/${gameId}`); - if(response.status !== 200){ - throw new Error("Failed to get game calendar"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as CalendarEvent[]; + return response.data as CalendarEvent[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -25,16 +33,23 @@ export function useGetRaidGroupCalendar(raidGroupId: string){ return useQuery({ queryKey: ["raidGroupCalendar", raidGroupId], queryFn: async () => { - const response = await api.get(`/calendar/raidGroup/${raidGroupId}`); + try{ + const response = await api.get(`/calendar/raidGroup/${raidGroupId}`); - if(response.status !== 200){ - throw new Error("Failed to get raid group calendar"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as CalendarEvent[]; + return response.data as CalendarEvent[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -46,13 +61,20 @@ export function useCreateGameCalendarEvent(gameId: string){ return useMutation({ mutationFn: async (calendarEvent: CalendarEvent) => { - const response = await api.post(`/calendar/game/${gameId}`, {...calendarEvent, gameCalendarEventId: calendarEvent.calendarEventId, calendarEventId: undefined}); + try{ + const response = await api.post(`/calendar/game/${gameId}`, {...calendarEvent, gameCalendarEventId: calendarEvent.calendarEventId, calendarEventId: undefined}); - if(response.status !== 200){ - throw new Error("Failed to create calendar event"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -67,13 +89,25 @@ export function useUpdateGameCalendarEvent(gameId: string){ return useMutation({ mutationFn: async (calendarEvent: CalendarEvent) => { - const response = await api.put(`/calendar/game/${gameId}`, {...calendarEvent, gameCalendarEventId: calendarEvent.calendarEventId, calendarEventId: undefined}); + try{ + const response = await api.put(`/calendar/game/${gameId}`, + { + ...calendarEvent, + gameCalendarEventId: calendarEvent.calendarEventId, + calendarEventId: undefined + }); - if(response.status !== 200){ - throw new Error("Failed to update calendar event"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -88,13 +122,20 @@ export function useDeleteGameCalendarEvent(gameId: string){ return useMutation({ mutationFn: async (calendarEvent: CalendarEvent) => { - const response = await api.delete(`/calendar/game/${gameId}/${calendarEvent.calendarEventId}`); + try{ + const response = await api.delete(`/calendar/game/${gameId}/${calendarEvent.calendarEventId}`); - if(response.status !== 200){ - throw new Error("Failed to delete calendar event"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -109,13 +150,25 @@ export function useCreateRaidGroupCalendarEvent(raidGroupId: string){ return useMutation({ mutationFn: async (calendarEvent: CalendarEvent) => { - const response = await api.post(`/calendar/raidGroup/${raidGroupId}`, {...calendarEvent, raidGroupCalendarEventId: calendarEvent.calendarEventId, calendarEventId: undefined}); + try{ + const response = await api.post(`/calendar/raidGroup/${raidGroupId}`, + { + ...calendarEvent, + raidGroupCalendarEventId: calendarEvent.calendarEventId, + calendarEventId: undefined + }); - if(response.status !== 200){ - throw new Error("Failed to create calendar event"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -130,13 +183,20 @@ export function useUpdateRaidGroupCalendarEvent(raidGroupId: string){ return useMutation({ mutationFn: async (calendarEvent: CalendarEvent) => { - const response = await api.put(`/calendar/raidGroup/${raidGroupId}`, {...calendarEvent, raidGroupCalendarEventId: calendarEvent.calendarEventId, calendarEventId: undefined}); + try{ + const response = await api.put(`/calendar/raidGroup/${raidGroupId}`, {...calendarEvent, raidGroupCalendarEventId: calendarEvent.calendarEventId, calendarEventId: undefined}); - if(response.status !== 200){ - throw new Error("Failed to update calendar event"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -151,13 +211,20 @@ export function useDeleteRaidGroupCalendarEvent(raidGroupId: string){ return useMutation({ mutationFn: async (calendarEvent: CalendarEvent) => { - const response = await api.delete(`/calendar/raidGroup/${raidGroupId}/${calendarEvent.calendarEventId}`); + try{ + const response = await api.delete(`/calendar/raidGroup/${raidGroupId}/${calendarEvent.calendarEventId}`); - if(response.status !== 200){ - throw new Error("Failed to delete calendar event"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -170,16 +237,23 @@ export function useGetRaidInstanceCalendarEvents(raidGroupId?: string){ return useQuery({ queryKey: ["raidInstanceCalendarEvents", raidGroupId], queryFn: async () => { - const response = await api.get(`/calendar/raidGroup/${raidGroupId}/raidInstance`); + try{ + const response = await api.get(`/calendar/raidGroup/${raidGroupId}/raidInstance`); - if(response.status !== 200){ - throw new Error("Failed to get raid instance calendar events"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as CalendarEvent[]; + return response.data as CalendarEvent[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } }, enabled: !!raidGroupId && raidGroupId !== "" }); diff --git a/src/hooks/ClassGroupHooks.ts b/src/hooks/ClassGroupHooks.ts index f2292d1..6d95b7d 100644 --- a/src/hooks/ClassGroupHooks.ts +++ b/src/hooks/ClassGroupHooks.ts @@ -1,6 +1,7 @@ import { ClassGroup } from "@/interface/ClassGroup"; import { api } from "@/util/AxiosUtil"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { AxiosError } from "axios"; export function useGetClassGroups(raidGroupId: string, page: number, pageSize: number, searchTerm?: string){ @@ -14,16 +15,23 @@ export function useGetClassGroups(raidGroupId: string, page: number, pageSize: n params.append("searchTerm", searchTerm); } - const response = await api.get(`/raidGroup/${raidGroupId}/classGroup?${params}`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/classGroup?${params}`); - if(response.status !== 200){ - throw new Error("Failed to get class groups"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as ClassGroup[]; + return response.data as ClassGroup[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } }, enabled: !!raidGroupId }); @@ -39,16 +47,23 @@ export function useGetClassGroupsCount(raidGroupId: string, searchTerm?: string) return useQuery({ queryKey: ["classGroups", "count", searchTerm], queryFn: async () => { - const response = await api.get(`/raidGroup/${raidGroupId}/classGroup/count?${searchParams}`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/classGroup/count?${searchParams}`); - if(response.status !== 200){ - throw new Error("Failed to get class groups count"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data.count as number; + return response.data.count as number; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -57,16 +72,23 @@ export function useGetClassGroupsByRaidLayout(raidGroupId: string, raidLayoutId: return useQuery({ queryKey: ["classGroups", "raidLayout", raidLayoutId], queryFn: async () => { - const response = await api.get(`/raidGroup/${raidGroupId}/classGroup/raidLayout/${raidLayoutId}`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/classGroup/raidLayout/${raidLayoutId}`); - if(response.status !== 200){ - throw new Error("Failed to get class groups"); - } - else if(response.data.error){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.error){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as (ClassGroup | null)[]; + return response.data as (ClassGroup | null)[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } }, enabled: !!raidGroupId && !!raidLayoutId }) @@ -80,21 +102,27 @@ export function useCreateClassGroup(raidGroupId: string){ return useMutation({ mutationKey: ["createClassGroup"], mutationFn: async ({classGroupName, gameClassIds}:{classGroupName: string; gameClassIds: string[];}) => { - const response = await api.post(`/raidGroup/${raidGroupId}/classGroup`, - { - classGroup: { - classGroupName: classGroupName, - raidGroupId: raidGroupId - }, - gameClassIds - } - ); + try{ + const response = await api.post(`/raidGroup/${raidGroupId}/classGroup`, + { + classGroup: { + classGroupName: classGroupName, + raidGroupId: raidGroupId + }, + gameClassIds + }); - if(response.status !== 200){ - throw new Error("Failed to create class group"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -110,18 +138,25 @@ export function useUpdateClassGroup(raidGroupId: string){ return useMutation({ mutationKey: ["updateClassGroup"], mutationFn: async ({classGroup, gameClassIds}:{classGroup: ClassGroup; gameClassIds: string[];}) => { - const response = await api.put(`/raidGroup/${raidGroupId}/classGroup/${classGroup.classGroupId}`, - { - classGroup, - gameClassIds - } - ); + try{ + const response = await api.put(`/raidGroup/${raidGroupId}/classGroup/${classGroup.classGroupId}`, + { + classGroup, + gameClassIds + } + ); - if(response.status !== 200){ - throw new Error("Failed to update class group"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -138,13 +173,20 @@ export function useDeleteClassGroup(raidGroupId: string, classGroupId: string){ return useMutation({ mutationKey: ["deleteClassGroup", classGroupId, raidGroupId], mutationFn: async () => { - const response = await api.delete(`/raidGroup/${raidGroupId}/classGroup/${classGroupId}`); + try{ + const response = await api.delete(`/raidGroup/${raidGroupId}/classGroup/${classGroupId}`); - if(response.status !== 200){ - throw new Error("Failed to delete class group"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { diff --git a/src/hooks/GameClassHooks.ts b/src/hooks/GameClassHooks.ts index bd41f87..8f380c6 100644 --- a/src/hooks/GameClassHooks.ts +++ b/src/hooks/GameClassHooks.ts @@ -1,22 +1,30 @@ import { GameClass } from "@/interface/GameClass"; import { api } from "@/util/AxiosUtil"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { AxiosError } from "axios"; export function useGetGameClass(gameClassId: string){ return useQuery({ queryKey: ["gameClasses", gameClassId], queryFn: async () => { - const response = await api.get(`/gameClass/${gameClassId}`); + try{ + const response = await api.get(`/gameClass/${gameClassId}`); - if(response.status !== 200){ - throw new Error("Failed to get game class"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as GameClass; + return response.data as GameClass; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } }, enabled: !!gameClassId }) @@ -33,16 +41,23 @@ export function useGetGameClasses(gameId: string, page: number, pageSize: number params.append("searchTerm", searchTerm); } - const response = await api.get(`/gameClass/game/${gameId}?${params}`); + try{ + const response = await api.get(`/gameClass/game/${gameId}?${params}`); - if(response.status !== 200){ - throw new Error("Failed to get game classes"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as GameClass[]; + return response.data as GameClass[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -51,16 +66,23 @@ export function useGetGameClassesByClassGroup(classGroupId?: string){ return useQuery({ queryKey: ["gameClasses", "classGroups", classGroupId], queryFn: async () => { - const response = await api.get(`/gameClass/classGroup/${classGroupId}`); + try{ + const response = await api.get(`/gameClass/classGroup/${classGroupId}`); - if(response.status !== 200){ - throw new Error("Failed to get game classes"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as GameClass[]; + return response.data as GameClass[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } }, enabled: !!classGroupId }); @@ -77,16 +99,23 @@ export function useGetGameClassesCount(gameId: string, searchTerm?: string){ return useQuery({ queryKey: ["gameClasses", gameId, "count", searchTerm], queryFn: async () => { - const response = await api.get(`/gameClass/game/${gameId}/count?${searchParams}`); + try{ + const response = await api.get(`/gameClass/game/${gameId}/count?${searchParams}`); - if(response.status !== 200){ - throw new Error("Failed to get game classes count"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data.count as number; + return response.data.count as number; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -106,16 +135,23 @@ export function useCreateGameClass(){ formData.append("gameClassName", gameClassName); formData.append("gameId", gameId); - const response = await api.post( - `/gameClass/game/${gameId}`, - formData - ); + try{ + const response = await api.post( + `/gameClass/game/${gameId}`, + formData + ); - if(response.status !== 200){ - throw new Error("Failed to create game class"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -141,13 +177,20 @@ export function useUpdateGameClass(){ formData.append("gameClassIcon", gameClass.gameClassIcon); } - const response = await api.put(`/gameClass/${gameClass.gameClassId}/game/${gameClass.gameId}`, formData); + try{ + const response = await api.put(`/gameClass/${gameClass.gameClassId}/game/${gameClass.gameId}`, formData); - if(response.status !== 200){ - throw new Error("Failed to update game class"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -163,13 +206,20 @@ export function useDeleteGameClass(){ return useMutation({ mutationKey: ["deleteGameClass"], mutationFn: async (gameClass: GameClass) => { - const response = await api.delete(`/gameClass/${gameClass.gameClassId}/game/${gameClass.gameId}`); + try{ + const response = await api.delete(`/gameClass/${gameClass.gameClassId}/game/${gameClass.gameId}`); - if(response.status !== 200){ - throw new Error("Failed to delete game class"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { diff --git a/src/hooks/GameHooks.ts b/src/hooks/GameHooks.ts index f726e3b..1201ac8 100644 --- a/src/hooks/GameHooks.ts +++ b/src/hooks/GameHooks.ts @@ -8,16 +8,23 @@ export function useGetGame(gameId: string, disabled: boolean){ return useQuery({ queryKey: ["games", gameId], queryFn: async () => { - const response = await api.get(`/game/${gameId}`); + try{ + const response = await api.get(`/game/${gameId}`); - if(response.status !== 200){ - throw new Error("Failed to get game"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data?.gameId ? response.data as Game : undefined; + return response.data?.gameId ? response.data as Game : undefined; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } }, enabled: !disabled }); @@ -34,7 +41,6 @@ export function useGetGames(page: number, pageSize: number, searchTerm?: string) params.append("searchTerm", searchTerm); } - //TODO: Change all queries to follow this pattern try{ const response = await api.get(`/game?${params}`); @@ -62,16 +68,23 @@ export function useGetGamesCount(searchTerm?: string){ return useQuery({ queryKey: ["games", "count", searchTerm], queryFn: async () => { - const response = await api.get(`/game/count?${searchParams}`); + try{ + const response = await api.get(`/game/count?${searchParams}`); - if(response.status !== 200){ - throw new Error("Failed to get games count"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data.count as number; + return response.data.count as number; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -89,16 +102,23 @@ export function useCreateGame(){ } formData.append("gameName", gameName); - const response = await api.post( - "/game", - formData - ); + try{ + const response = await api.post( + "/game", + formData + ); - if(response.status !== 200){ - throw new Error("Failed to create game"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -123,13 +143,20 @@ export function useUpdateGame(){ formData.append("gameIcon", game.gameIcon); } - const response = await api.put(`/game/${game.gameId}`, formData); + try{ + const response = await api.put(`/game/${game.gameId}`, formData); - if(response.status !== 200){ - throw new Error("Failed to update game"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -145,13 +172,20 @@ export function useDeleteGame(){ return useMutation({ mutationKey: ["deleteGame"], mutationFn: async (gameId: string) => { - const response = await api.delete(`/game/${gameId}`); + try{ + const response = await api.delete(`/game/${gameId}`); - if(response.status !== 200){ - throw new Error("Failed to delete game"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { diff --git a/src/hooks/PersonCharacterHooks.ts b/src/hooks/PersonCharacterHooks.ts index b3a2c26..0030de9 100644 --- a/src/hooks/PersonCharacterHooks.ts +++ b/src/hooks/PersonCharacterHooks.ts @@ -1,22 +1,30 @@ import { PersonCharacter } from "@/interface/PersonCharacter"; import { api } from "@/util/AxiosUtil"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { AxiosError } from "axios"; export function useGetPersonCharactersByPersonId(personId: string, raidGroupId: string){ return useQuery({ queryKey: ["personCharacters", personId], queryFn: async () => { - const response = await api.get(`/raidGroup/${raidGroupId}/person/${personId}/character`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/person/${personId}/character`); - if(response.status !== 200){ - throw new Error("Failed to get person characters"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as PersonCharacter[]; + return response.data as PersonCharacter[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -33,19 +41,23 @@ export function useGetPersonCharactersByPersonIdSearch(personId: string, raidGro return useQuery({ queryKey: ["personCharacters", personId, { page, pageSize, searchTerm }], queryFn: async () => { - const response = await api.get(`/raidGroup/${raidGroupId}/person/${personId}/character/page?${params}`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/person/${personId}/character/page?${params}`); - if(response.status !== 200){ - throw new Error("Failed to get person characters"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } + + return response.data as PersonCharacter[]; } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } - - console.log("person characters"); - console.log(response.data); - - return response.data as PersonCharacter[]; } }); } @@ -59,16 +71,23 @@ export function useGetPersonCharactersCountByPersonIdSearch(personId: string, ra return useQuery({ queryKey: ["personCharactersCount", personId, { searchTerm }], queryFn: async () => { - const response = await api.get(`/raidGroup/${raidGroupId}/person/${personId}/character/count?${params}`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/person/${personId}/character/count?${params}`); - if(response.status !== 200){ - throw new Error("Failed to get person characters count"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data.count as number; + return response.data.count as number; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -77,16 +96,23 @@ export function useGetPersonCharactersByRaidGroup(raidGroupId: string){ return useQuery({ queryKey: ["personCharacters", raidGroupId], queryFn: async () => { - const response = await api.get(`/raidGroup/${raidGroupId}/person/character`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/person/character`); - if(response.status !== 200){ - throw new Error("Failed to get person characters"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as PersonCharacter[]; + return response.data as PersonCharacter[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } }, enabled: !!raidGroupId }); @@ -99,13 +125,20 @@ export function useCreatePersonCharacter(raidGroupId: string, personId: string){ return useMutation({ mutationKey: ["createPersonCharacter"], mutationFn: async (personCharacter: PersonCharacter) => { - const response = await api.post(`/raidGroup/${raidGroupId}/person/${personId}/character`, personCharacter); + try{ + const response = await api.post(`/raidGroup/${raidGroupId}/person/${personId}/character`, personCharacter); - if(response.status !== 200){ - throw new Error("Failed to create person character"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -121,13 +154,20 @@ export function useUpdatePersonCharacter(raidGroupId: string, personId: string){ return useMutation({ mutationKey: ["updatePersonCharacter"], mutationFn: async (personCharacter: PersonCharacter) => { - const response = await api.put(`/raidGroup/${raidGroupId}/person/${personId}/character/${personCharacter.personCharacterId}`, personCharacter); + try{ + const response = await api.put(`/raidGroup/${raidGroupId}/person/${personId}/character/${personCharacter.personCharacterId}`, personCharacter); - if(response.status !== 200){ - throw new Error("Failed to update person character"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -143,13 +183,20 @@ export function useDeletePersonCharacter(raidGroupId: string, personId: string){ return useMutation({ mutationKey: ["deletePersonCharacter"], mutationFn: async (personCharacterId: string) => { - const response = await api.delete(`/raidGroup/${raidGroupId}/person/${personId}/character/${personCharacterId}`); + try{ + const response = await api.delete(`/raidGroup/${raidGroupId}/person/${personId}/character/${personCharacterId}`); - if(response.status !== 200){ - throw new Error("Failed to delete person character"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { diff --git a/src/hooks/PersonHooks.ts b/src/hooks/PersonHooks.ts index cfbd177..f5bc982 100644 --- a/src/hooks/PersonHooks.ts +++ b/src/hooks/PersonHooks.ts @@ -1,22 +1,30 @@ import { Person } from "@/interface/Person"; import { api } from "@/util/AxiosUtil"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { AxiosError } from "axios"; export function useGetPerson(raidGroupId: string, personId: string, disabled: boolean){ return useQuery({ queryKey: ["people", raidGroupId, personId], queryFn: async () => { - const response = await api.get(`/raidGroup/${raidGroupId}/person/${personId}`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/person/${personId}`); - if(response.status !== 200){ - throw new Error("Failed to get person"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as Person; + return response.data as Person; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } }, enabled: !disabled }); @@ -33,16 +41,23 @@ export function useGetPeopleByRaidGroup(raidGroupId: string, page: number, pageS searchParams.append("searchTerm", searchTerm); } - const response = await api.get(`/raidGroup/${raidGroupId}/person?${searchParams}`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/person?${searchParams}`); - if(response.status !== 200){ - throw new Error("Failed to get people"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as Person[]; + return response.data as Person[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -57,16 +72,23 @@ export function useGetPeopleByRaidGroupCount(raidGroupId: string, searchTerm?: s return useQuery({ queryKey: ["people", raidGroupId, "count", searchTerm], queryFn: async () => { - const response = await api.get(`/raidGroup/${raidGroupId}/person/count?${searchParams}`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/person/count?${searchParams}`); - if(response.status !== 200){ - throw new Error("Failed to get people count"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data.count as number; + return response.data.count as number; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -78,13 +100,20 @@ export function useCreatePerson(){ return useMutation({ mutationKey: ["createPerson"], mutationFn: async ({raidGroupId, personName, discordId}:{raidGroupId: string; personName: string; discordId?: string;}) => { - const response = await api.post(`/raidGroup/${raidGroupId}/person`, {raidGroupId, personName, discordId}); + try{ + const response = await api.post(`/raidGroup/${raidGroupId}/person`, {raidGroupId, personName, discordId}); - if(response.status !== 200){ - throw new Error("Failed to create person"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -100,13 +129,20 @@ export function useUpdatePerson(){ return useMutation({ mutationKey: ["updatePerson"], mutationFn: async (person: Person) => { - const response = await api.put(`/raidGroup/${person.raidGroupId}/person/${person.personId}`, person); + try{ + const response = await api.put(`/raidGroup/${person.raidGroupId}/person/${person.personId}`, person); - if(response.status !== 200){ - throw new Error("Failed to update person"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -122,13 +158,20 @@ export function useDeletePerson(){ return useMutation({ mutationKey: ["deletePerson"], mutationFn: async ({raidGroupId, personId}:{raidGroupId: string; personId: string;}) => { - const response = await api.delete(`/raidGroup/${raidGroupId}/person/${personId}`); + try{ + const response = await api.delete(`/raidGroup/${raidGroupId}/person/${personId}`); - if(response.status !== 200){ - throw new Error("Failed to delete person"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { diff --git a/src/hooks/RaidGroupHooks.ts b/src/hooks/RaidGroupHooks.ts index a8f8a1d..acc6ecb 100644 --- a/src/hooks/RaidGroupHooks.ts +++ b/src/hooks/RaidGroupHooks.ts @@ -1,22 +1,30 @@ import { RaidGroup } from "@/interface/RaidGroup"; import { api } from "@/util/AxiosUtil"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { AxiosError } from "axios"; export function useGetRaidGroup(raidGroupId: string, disabled: boolean){ return useQuery({ queryKey: ["raidGroups", raidGroupId], queryFn: async () => { - const response = await api.get(`/raidGroup/${raidGroupId}`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}`); - if(response.status !== 200){ - throw new Error("Failed to get raid group"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data?.raidGroupId ? response.data as RaidGroup : undefined; + return response.data?.raidGroupId ? response.data as RaidGroup : undefined; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } }, enabled: !disabled }); @@ -33,16 +41,23 @@ export function useGetRaidGroups(page: number, pageSize: number, searchTerm?: st params.append("searchTerm", searchTerm); } - const response = await api.get(`/raidGroup?${params}`); + try{ + const response = await api.get(`/raidGroup?${params}`); - if(response.status !== 200){ - throw new Error("Failed to get raid groups"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as RaidGroup[]; + return response.data as RaidGroup[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -57,16 +72,23 @@ export function useGetRaidGroupsCount(searchTerm?: string){ return useQuery({ queryKey: ["raidGroups", "count", searchTerm], queryFn: async () => { - const response = await api.get(`/raidGroup/count?${searchParams}`); + try{ + const response = await api.get(`/raidGroup/count?${searchParams}`); - if(response.status !== 200){ - throw new Error("Failed to get raid groups count"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data.count as number; + return response.data.count as number; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -82,16 +104,23 @@ export function useGetRaidGroupsByGame(gameId: string, page: number, pageSize: n params.append("searchTerm", searchTerm); } - const response = await api.get(`/raidGroup/game/${gameId}?${params}`); + try{ + const response = await api.get(`/raidGroup/game/${gameId}?${params}`); - if(response.status !== 200){ - throw new Error("Failed to get raid groups"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as RaidGroup[]; + return response.data as RaidGroup[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -106,16 +135,23 @@ export function useGetRaidGroupsByGameCount(gameId: string, searchTerm?: string) return useQuery({ queryKey: ["raidGroups", gameId, "count", searchTerm], queryFn: async () => { - const response = await api.get(`/raidGroup/game/${gameId}/count?${searchParams}`); + try{ + const response = await api.get(`/raidGroup/game/${gameId}/count?${searchParams}`); - if(response.status !== 200){ - throw new Error("Failed to get raid groups count"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data.count as number; + return response.data.count as number; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -131,16 +167,23 @@ export function useGetRaidGroupsByAccount(accountId: string, page: number, pageS params.append("searchTerm", searchTerm); } - const response = await api.get(`/raidGroup/account/${accountId}?${params}`); + try{ + const response = await api.get(`/raidGroup/account/${accountId}?${params}`); - if(response.status !== 200){ - throw new Error("Failed to get raid groups"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as RaidGroup[]; + return response.data as RaidGroup[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -155,16 +198,23 @@ export function useGetRaidGroupsCountByAccount(accountId: string, searchTerm?: s return useQuery({ queryKey: ["raidGroups", accountId, "count", searchTerm], queryFn: async () => { - const response = await api.get(`/raidGroup/account/${accountId}/count?${searchParams}`); + try{ + const response = await api.get(`/raidGroup/account/${accountId}/count?${searchParams}`); - if(response.status !== 200){ - throw new Error("Failed to get raid groups count"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data.count as number; + return response.data.count as number; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); @@ -185,16 +235,23 @@ export function useCreateRaidGroup(){ formData.append("raidGroupName", raidGroupName); formData.append("gameId", gameId); - const response = await api.post( - "/raidGroup", - formData - ); + try{ + const response = await api.post( + "/raidGroup", + formData + ); - if(response.status !== 200){ - throw new Error("Failed to create raid group"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -220,13 +277,20 @@ export function useUpdateRaidGroup(){ formData.append("raidGroupIcon", raidGroup.raidGroupIcon); } - const response = await api.put(`/raidGroup/${raidGroup.raidGroupId}`, formData); + try{ + const response = await api.put(`/raidGroup/${raidGroup.raidGroupId}`, formData); - if(response.status !== 200){ - throw new Error("Failed to update raid group"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -242,13 +306,20 @@ export function useDeleteRaidGroup(){ return useMutation({ mutationKey: ["deleteRaidGroup"], mutationFn: async (raidGroupId: string) => { - const response = await api.delete(`/raidGroup/${raidGroupId}`); + try{ + const response = await api.delete(`/raidGroup/${raidGroupId}`); - if(response.status !== 200){ - throw new Error("Failed to delete raid group"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { diff --git a/src/hooks/RaidGroupRequestHooks.ts b/src/hooks/RaidGroupRequestHooks.ts index 0697a8a..0ad8417 100644 --- a/src/hooks/RaidGroupRequestHooks.ts +++ b/src/hooks/RaidGroupRequestHooks.ts @@ -2,6 +2,7 @@ import { RaidGroupPermissionType } from "@/interface/RaidGroup"; import { RaidGroupRequest } from "@/interface/RaidGroupRequest"; import { api } from "@/util/AxiosUtil"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { AxiosError } from "axios"; export function useGetRaidGroupRequests(raidGroupId: string, page: number, pageSize: number, searchTerm?: string){ @@ -15,16 +16,23 @@ export function useGetRaidGroupRequests(raidGroupId: string, page: number, pageS return useQuery({ queryKey: ["raidGroupRequest", raidGroupId, {page, pageSize, searchTerm}], queryFn: async () => { - const response = await api.get(`/raidGroup/${raidGroupId}/raidGroupRequest?${searchParams}`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/raidGroupRequest?${searchParams}`); - if(response.status !== 200){ - throw new Error("Failed to get raid group requests"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as RaidGroupRequest[]; + return response.data as RaidGroupRequest[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } }, enabled: !!raidGroupId && raidGroupId !== "" }); @@ -39,16 +47,23 @@ export function useGetRaidGroupRequestCount(raidGroupId?: string, searchTerm?: s return useQuery({ queryKey: ["raidGroupRequest", raidGroupId, "count", searchTerm], queryFn: async () => { - const response = await api.get(`/raidGroup/${raidGroupId}/raidGroupRequest/count?${searchParams}`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/raidGroupRequest/count?${searchParams}`); - if(response.status !== 200){ - throw new Error("Failed to get raid group request count"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data.count as number; + return response.data.count as number; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } }, enabled: !!raidGroupId && raidGroupId !== "" }); @@ -61,15 +76,20 @@ export function useCreateRaidGroupRequest(raidGroupId: string){ return useMutation({ mutationKey: ["raidGroupRequest", raidGroupId], mutationFn: async (requestMessage: string) => { - const response = await api.post(`/raidGroup/${raidGroupId}/raidGroupRequest`, { - requestMessage - }); + try{ + const response = await api.post(`/raidGroup/${raidGroupId}/raidGroupRequest`, {requestMessage}); - if(response.status !== 200){ - throw new Error("Failed to create raid group request"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -84,15 +104,20 @@ export function useResolveRaidGroupRequest(raidGroupId: string, raidGroupRequest return useMutation({ mutationKey: ["raidGroupRequest", raidGroupId, raidGroupRequestId], mutationFn: async (permission: RaidGroupPermissionType) => { - const response = await api.put(`/raidGroup/${raidGroupId}/raidGroupRequest/${raidGroupRequestId}/resolve`, { - resolution: permission - }); + try{ + const response = await api.put(`/raidGroup/${raidGroupId}/raidGroupRequest/${raidGroupRequestId}/resolve`, {resolution: permission}); - if(response.status !== 200){ - throw new Error("Failed to resolve raid group request"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -108,13 +133,20 @@ export function useDeleteRaidGroupRequest(raidGroupId: string, raidGroupRequestI return useMutation({ mutationKey: ["raidGroupRequest", raidGroupId, raidGroupRequestId], mutationFn: async () => { - const response = await api.delete(`/raidGroup/${raidGroupId}/raidGroupRequest/${raidGroupRequestId}`); + try{ + const response = await api.delete(`/raidGroup/${raidGroupId}/raidGroupRequest/${raidGroupRequestId}`); - if(response.status !== 200){ - throw new Error("Failed to delete raid group request"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { diff --git a/src/hooks/RaidInstanceHooks.ts b/src/hooks/RaidInstanceHooks.ts index 35fcc10..8e2e3a4 100644 --- a/src/hooks/RaidInstanceHooks.ts +++ b/src/hooks/RaidInstanceHooks.ts @@ -2,22 +2,30 @@ import { RaidInstance } from "@/interface/RaidInstance"; import { RaidInstancePersonCharacterXref } from "@/interface/RaidInstancePersonCharacterXref"; import { api } from "@/util/AxiosUtil"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { AxiosError } from "axios"; export function useGetRaidInstance(raidInstanceId: string, raidGroupId: string){ return useQuery({ queryKey: ["raidInstances", raidInstanceId, raidGroupId], queryFn: async () => { - const response = await api.get(`/raidGroup/${raidGroupId}/raidInstance/${raidInstanceId}`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/raidInstance/${raidInstanceId}`); - if(response.status !== 200){ - throw new Error("Failed to get raid instance"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as RaidInstance; + return response.data as RaidInstance; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } }, enabled: !!raidInstanceId && !!raidGroupId }); @@ -34,16 +42,23 @@ export function useGetRaidInstancesByRaidGroup(raidGroupId: string, page: number params.append("searchTerm", searchTerm); } - const response = await api.get(`/raidGroup/${raidGroupId}/raidInstance?${params}`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/raidInstance?${params}`); - if(response.status !== 200){ - throw new Error("Failed to get accounts"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as RaidInstance[]; + return response.data as RaidInstance[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -58,16 +73,23 @@ export function useGetRaidInstancesByRaidGroupCount(raidGroupId: string, searchT params.append("searchTerm", searchTerm); } - const response = await api.get(`/raidGroup/${raidGroupId}/raidInstance/count?${params}`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/raidInstance/count?${params}`); - if(response.status !== 200){ - throw new Error("Failed to get accounts"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data.count as number; + return response.data.count as number; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -80,17 +102,23 @@ export function useCreateRaidInstance(raidGroupId: string){ return useMutation({ mutationKey: ["createRaidInstance", raidGroupId], mutationFn: async (raidInstance: RaidInstance) => { - console.log("raidInstance2", raidInstance); + try{ const response = await api.post(`/raidGroup/${raidGroupId}/raidInstance`, raidInstance); - if(response.status !== 200){ - throw new Error("Failed to create raid instance"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data.raidInstanceId as string; + return response.data.raidInstanceId as string; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["raidInstances"] }); @@ -105,13 +133,20 @@ export function useUpdateRaidInstance(raidGroupId: string){ return useMutation({ mutationKey: ["updateRaidInstance", raidGroupId], mutationFn: async (raidInstance: RaidInstance) => { - const response = await api.put(`/raidGroup/${raidGroupId}/raidInstance/${raidInstance.raidInstanceId}`, raidInstance); + try{ + const response = await api.put(`/raidGroup/${raidGroupId}/raidInstance/${raidInstance.raidInstanceId}`, raidInstance); - if(response.status !== 200){ - throw new Error("Failed to update raid instance"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -124,13 +159,20 @@ export function useUpdateRaidInstanceNoInvalidation(raidGroupId: string){ return useMutation({ mutationKey: ["updateRaidInstance", raidGroupId], mutationFn: async (raidInstance: RaidInstance) => { - const response = await api.put(`/raidGroup/${raidGroupId}/raidInstance/${raidInstance.raidInstanceId}`, raidInstance); + try{ + const response = await api.put(`/raidGroup/${raidGroupId}/raidInstance/${raidInstance.raidInstanceId}`, raidInstance); - if(response.status !== 200){ - throw new Error("Failed to update raid instance"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } } }); @@ -143,13 +185,20 @@ export function useDeleteRaidInstance(raidGroupId: string, raidInstanceId: strin return useMutation({ mutationKey: ["deleteRaidInstance", raidGroupId, raidInstanceId], mutationFn: async () => { - const response = await api.delete(`/raidGroup/${raidGroupId}/raidInstance/${raidInstanceId}`); + try{ + const response = await api.delete(`/raidGroup/${raidGroupId}/raidInstance/${raidInstanceId}`); - if(response.status !== 200){ - throw new Error("Failed to delete raid instance"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -163,16 +212,23 @@ export function useGetRaidInstancePersonCharacterXrefs(raidGroupId?: string, rai return useQuery({ queryKey: ["raidInstancePersonCharacterXrefs", raidGroupId, raidInstanceId], queryFn: async () => { - const response = await api.get(`/raidGroup/${raidGroupId}/raidInstance/${raidInstanceId}/personCharacterXref`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/raidInstance/${raidInstanceId}/personCharacterXref`); - if(response.status !== 200){ - throw new Error("Failed to get raid instance person character xrefs"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as RaidInstancePersonCharacterXref[]; + return response.data as RaidInstancePersonCharacterXref[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } }, enabled: !!raidGroupId && !!raidInstanceId }); @@ -185,18 +241,24 @@ export function useUpdateRaidInstancePersonCharacterXrefs(raidGroupId?: string, return useMutation({ mutationKey: ["updateRaidInstancePersonCharacterXrefs", raidGroupId, raidInstanceId], mutationFn: async (raidInstancePersonCharacterXrefs: RaidInstancePersonCharacterXref[]) => { - const response = await api.post(`/raidGroup/${raidGroupId}/raidInstance/${raidInstanceId}/personCharacterXref`, raidInstancePersonCharacterXrefs); + try{ + const response = await api.post(`/raidGroup/${raidGroupId}/raidInstance/${raidInstanceId}/personCharacterXref`, raidInstancePersonCharacterXrefs); - if(response.status !== 200){ - throw new Error("Failed to update raid instance person character xrefs"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["raidInstancePersonCharacterXrefs", raidGroupId, raidInstanceId] }); } }); - } diff --git a/src/hooks/RaidLayoutHooks.ts b/src/hooks/RaidLayoutHooks.ts index b59de6b..99fa43f 100644 --- a/src/hooks/RaidLayoutHooks.ts +++ b/src/hooks/RaidLayoutHooks.ts @@ -1,22 +1,30 @@ import { RaidLayout, RaidLayoutClassGroupXref } from "@/interface/RaidLayout"; import { api } from "@/util/AxiosUtil"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { AxiosError } from "axios"; export function useGetRaidLayout(raidGroupId?: string, raidLayoutId?: string){ return useQuery({ queryKey: ["raidLayout", raidGroupId, raidLayoutId], queryFn: async () => { - const response = await api.get(`/raidGroup/${raidGroupId}/raidLayout/${raidLayoutId}`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/raidLayout/${raidLayoutId}`); - if(response.status !== 200){ - throw new Error("Failed to get raid layout"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as RaidLayout; + return response.data as RaidLayout; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } }, enabled: !!raidGroupId && !!raidLayoutId }); @@ -33,16 +41,23 @@ export function useGetRaidLayoutsByRaidGroup(raidGroupId: string, page: number, params.append("searchTerm", searchTerm); } - const response = await api.get(`/raidGroup/${raidGroupId}/raidLayout?${params}`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/raidLayout?${params}`); - if(response.status !== 200){ - throw new Error("Failed to get accounts"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data as RaidLayout[]; + return response.data as RaidLayout[]; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -56,16 +71,23 @@ export function useGetRaidLayoutsByRaidGroupCount(raidGroupId: string, searchTer params.append("searchTerm", searchTerm); } - const response = await api.get(`/raidGroup/${raidGroupId}/raidLayout/count?${params}`); + try{ + const response = await api.get(`/raidGroup/${raidGroupId}/raidLayout/count?${params}`); - if(response.status !== 200){ - throw new Error("Failed to get accounts"); - } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); - } + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } - return response.data.count as number; + return response.data.count as number; + } + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } + } } }); } @@ -77,18 +99,25 @@ export function useCreateRaidLayout(raidGroupId: string){ return useMutation({ mutationKey: ["createRaidLayout", raidGroupId], mutationFn: async ({raidLayout, raidLayoutClassGroupXrefs}:{raidLayout: RaidLayout; raidLayoutClassGroupXrefs: RaidLayoutClassGroupXref[];}) => { - const response = await api.post(`/raidGroup/${raidGroupId}/raidLayout`, - { - raidLayout, - raidLayoutClassGroupXrefs - } - ); + try{ + const response = await api.post(`/raidGroup/${raidGroupId}/raidLayout`, + { + raidLayout, + raidLayoutClassGroupXrefs + } + ); - if(response.status !== 200){ - throw new Error("Failed to create raid layout"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -104,18 +133,25 @@ export function useUpdateRaidLayout(raidGroupId: string){ return useMutation({ mutationKey: ["updateRaidLayout", raidGroupId], mutationFn: async ({raidLayout, raidLayoutClassGroupXrefs}:{raidLayout: RaidLayout; raidLayoutClassGroupXrefs: RaidLayoutClassGroupXref[];}) => { - const response = await api.put(`/raidGroup/${raidGroupId}/raidLayout/${raidLayout.raidLayoutId}`, - { - raidLayout, - raidLayoutClassGroupXrefs - } - ); + try{ + const response = await api.put(`/raidGroup/${raidGroupId}/raidLayout/${raidLayout.raidLayoutId}`, + { + raidLayout, + raidLayoutClassGroupXrefs + } + ); - if(response.status !== 200){ - throw new Error("Failed to update raid layout"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => { @@ -132,13 +168,20 @@ export function useDeleteRaidLayout(raidGroupId: string, raidLayoutId: string){ return useMutation({ mutationKey: ["deleteRaidLayout", raidGroupId, raidLayoutId], mutationFn: async () => { - const response = await api.delete(`/raidGroup/${raidGroupId}/raidLayout/${raidLayoutId}`); + try{ + const response = await api.delete(`/raidGroup/${raidGroupId}/raidLayout/${raidLayoutId}`); - if(response.status !== 200){ - throw new Error("Failed to delete raid layout"); + if(response.data.errors){ + throw new Error(response.data.errors.join(", ")); + } } - else if(response.data.errors){ - throw new Error(response.data.errors.join(", ")); + catch(error){ + if(error instanceof AxiosError && error.response?.data.errors){ + throw new Error(error.response?.data.errors.join(", ")); + } + else{ + throw error; + } } }, onSuccess: () => {