Game calendar working

This commit is contained in:
2025-03-06 19:49:03 -05:00
parent ef6da3ea64
commit 28462776ac
30 changed files with 1025 additions and 67 deletions

167
src/hooks/CalendarHooks.ts Normal file
View File

@@ -0,0 +1,167 @@
import { CalendarEvent } from "@/interface/Calendar";
import { api } from "@/util/AxiosUtil";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
export function useGetGameCalendar(gameId: string){
return useQuery({
queryKey: ["gameCalendar", gameId],
queryFn: async () => {
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(", "));
}
return response.data as CalendarEvent[];
}
});
}
export function useGetRaidGroupCalendar(raidGroupId: string){
return useQuery({
queryKey: ["raidGroupCalendar", raidGroupId],
queryFn: async () => {
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(", "));
}
return response.data as CalendarEvent[];
}
});
}
export function useCreateGameCalendarEvent(gameId: string){
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (calendarEvent: CalendarEvent) => {
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");
}
else if(response.data.errors){
throw new Error(response.data.errors.join(", "));
}
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["gameCalendar"]})
}
});
}
export function useUpdateGameCalendarEvent(gameId: string){
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (calendarEvent: CalendarEvent) => {
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");
}
else if(response.data.errors){
throw new Error(response.data.errors.join(", "));
}
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["gameCalendar"]})
}
});
}
export function useDeleteGameCalendarEvent(gameId: string){
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (calendarEvent: CalendarEvent) => {
const response = await api.delete(`/calendar/game/${gameId}/${calendarEvent.calendarEventId}`);
if(response.status !== 200){
throw new Error("Failed to delete calendar event");
}
else if(response.data.errors){
throw new Error(response.data.errors.join(", "));
}
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["gameCalendar"]})
}
});
}
export function useCreateRaidGroupCalendarEvent(raidGroupId: string){
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (calendarEvent: CalendarEvent) => {
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");
}
else if(response.data.errors){
throw new Error(response.data.errors.join(", "));
}
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["raidGroupCalendar"]})
}
});
}
export function useUpdateRaidGroupCalendarEvent(raidGroupId: string){
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (calendarEvent: CalendarEvent) => {
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");
}
else if(response.data.errors){
throw new Error(response.data.errors.join(", "));
}
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["raidGroupCalendar"]})
}
});
}
export function useDeleteRaidGroupCalendarEvent(raidGroupId: string){
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (calendarEvent: CalendarEvent) => {
const response = await api.delete(`/calendar/raidGroup/${raidGroupId}/${calendarEvent.calendarEventId}`);
if(response.status !== 200){
throw new Error("Failed to delete calendar event");
}
else if(response.data.errors){
throw new Error(response.data.errors.join(", "));
}
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["raidGroupCalendar"]})
}
});
}