Game calendar working
This commit is contained in:
167
src/hooks/CalendarHooks.ts
Normal file
167
src/hooks/CalendarHooks.ts
Normal 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"]})
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
export function useGetGame(gameId: string, disabled: boolean){
|
||||
return useQuery({
|
||||
queryKey: ["game", gameId],
|
||||
queryKey: ["games", gameId],
|
||||
queryFn: async () => {
|
||||
const response = await api.get(`/game/${gameId}`);
|
||||
|
||||
@@ -16,7 +16,7 @@ export function useGetGame(gameId: string, disabled: boolean){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
|
||||
return response.data as Game;
|
||||
return response.data?.gameId ? response.data as Game : undefined;
|
||||
},
|
||||
enabled: !disabled
|
||||
});
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { CalendarEvent } from "@/interface/Calendar";
|
||||
import { RaidGroup } from "@/interface/RaidGroup";
|
||||
import { api } from "@/util/AxiosUtil";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
@@ -52,6 +53,74 @@ export function useGetRaidGroupsCount(searchTerm?: string){
|
||||
});
|
||||
}
|
||||
|
||||
export function useGetRaidGroupsByGame(gameId: string, page: number, pageSize: number, searchTerm?: string){
|
||||
return useQuery({
|
||||
queryKey: ["raidGroups", gameId, { page, pageSize, searchTerm }],
|
||||
queryFn: async () => {
|
||||
const params = new URLSearchParams();
|
||||
params.append("page", page.toString());
|
||||
params.append("pageSize", pageSize.toString());
|
||||
if(searchTerm){
|
||||
params.append("searchTerm", searchTerm);
|
||||
}
|
||||
|
||||
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(", "));
|
||||
}
|
||||
|
||||
return response.data as RaidGroup[];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useGetRaidGroupsByGameCount(gameId: string, searchTerm?: string){
|
||||
const searchParams = new URLSearchParams();
|
||||
if(searchTerm){
|
||||
searchParams.append("searchTerm", searchTerm);
|
||||
}
|
||||
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["raidGroups", gameId, "count", searchTerm],
|
||||
queryFn: async () => {
|
||||
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(", "));
|
||||
}
|
||||
|
||||
return response.data.count as number;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useGetRaidGroupCalendar(raidGroupId: string){
|
||||
return useQuery({
|
||||
queryKey: ["raidGroups", raidGroupId, "calendar"],
|
||||
queryFn: async () => {
|
||||
const response = await api.get(`/raidGroup/${raidGroupId}/calendar`);
|
||||
|
||||
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 useCreateRaidGroup(){
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user