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 () => { try{ const response = await api.get(`/calendar/game/${gameId}`); if(response.data.errors){ throw new Error(response.data.errors.join(", ")); } 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; } } } }); } export function useGetRaidGroupCalendar(raidGroupId: string){ return useQuery({ queryKey: ["raidGroupCalendar", raidGroupId], queryFn: async () => { try{ const response = await api.get(`/calendar/raidGroup/${raidGroupId}`); if(response.data.errors){ throw new Error(response.data.errors.join(", ")); } 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; } } } }); } export function useCreateGameCalendarEvent(gameId: string){ const queryClient = useQueryClient(); return useMutation({ mutationFn: async (calendarEvent: CalendarEvent) => { try{ const response = await api.post(`/calendar/game/${gameId}`, {...calendarEvent, gameCalendarEventId: calendarEvent.calendarEventId, calendarEventId: undefined}); 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: ["gameCalendar"]}) } }); } export function useUpdateGameCalendarEvent(gameId: string){ const queryClient = useQueryClient(); return useMutation({ mutationFn: async (calendarEvent: CalendarEvent) => { try{ const response = await api.put(`/calendar/game/${gameId}`, { ...calendarEvent, gameCalendarEventId: calendarEvent.calendarEventId, calendarEventId: undefined }); 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: ["gameCalendar"]}) } }); } export function useDeleteGameCalendarEvent(gameId: string){ const queryClient = useQueryClient(); return useMutation({ mutationFn: async (calendarEvent: CalendarEvent) => { try{ const response = await api.delete(`/calendar/game/${gameId}/${calendarEvent.calendarEventId}`); 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: ["gameCalendar"]}) } }); } export function useCreateRaidGroupCalendarEvent(raidGroupId: string){ const queryClient = useQueryClient(); return useMutation({ mutationFn: async (calendarEvent: CalendarEvent) => { try{ const response = await api.post(`/calendar/raidGroup/${raidGroupId}`, { ...calendarEvent, raidGroupCalendarEventId: calendarEvent.calendarEventId, calendarEventId: undefined }); 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: ["raidGroupCalendar"]}) } }); } export function useUpdateRaidGroupCalendarEvent(raidGroupId: string){ const queryClient = useQueryClient(); return useMutation({ mutationFn: async (calendarEvent: CalendarEvent) => { try{ const response = await api.put(`/calendar/raidGroup/${raidGroupId}`, {...calendarEvent, raidGroupCalendarEventId: calendarEvent.calendarEventId, calendarEventId: undefined}); 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: ["raidGroupCalendar"]}) } }); } export function useDeleteRaidGroupCalendarEvent(raidGroupId: string){ const queryClient = useQueryClient(); return useMutation({ mutationFn: async (calendarEvent: CalendarEvent) => { try{ const response = await api.delete(`/calendar/raidGroup/${raidGroupId}/${calendarEvent.calendarEventId}`); 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: ["raidGroupCalendar"]}) } }); } export function useGetRaidInstanceCalendarEvents(raidGroupId?: string){ return useQuery({ queryKey: ["raidInstanceCalendarEvents", raidGroupId], queryFn: async () => { try{ const response = await api.get(`/calendar/raidGroup/${raidGroupId}/raidInstance`); if(response.data.errors){ throw new Error(response.data.errors.join(", ")); } 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 !== "" }); }