import { Counter } from "@/interface/Counters"; import { RaidGroup } from "@/interface/RaidGroup"; import api from "@/util/AxiosUtil"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; export function useGetRaidGroup(raidGroupId: string, disabled: boolean){ return useQuery({ queryKey: ["raidGroups", raidGroupId], queryFn: async () => { const response = await api.get(`/raidGroup/${raidGroupId}`); return (response.data as RaidGroup)?.raidGroupId ? response.data as RaidGroup : undefined; }, enabled: !disabled }); } export function useGetRaidGroups(page: number, pageSize: number, searchTerm?: string){ return useQuery({ queryKey: ["raidGroups", { 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?${params}`); return response.data as RaidGroup[]; } }); } export function useGetRaidGroupsCount(searchTerm?: string){ const searchParams = new URLSearchParams(); if(searchTerm){ searchParams.append("searchTerm", searchTerm); } return useQuery({ queryKey: ["raidGroups", "count", searchTerm], queryFn: async () => { const response = await api.get(`/raidGroup/count?${searchParams}`); return (response.data as Counter).count; } }); } 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}`); 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}`); return (response.data as Counter).count; } }); } export function useGetRaidGroupsByAccount(accountId: string, page: number, pageSize: number, searchTerm?: string){ return useQuery({ queryKey: ["raidGroups", accountId, { 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/account/${accountId}?${params}`); return response.data as RaidGroup[]; } }); } export function useGetRaidGroupsCountByAccount(accountId: string, searchTerm?: string){ const searchParams = new URLSearchParams(); if(searchTerm){ searchParams.append("searchTerm", searchTerm); } return useQuery({ queryKey: ["raidGroups", accountId, "count", searchTerm], queryFn: async () => { const response = await api.get(`/raidGroup/account/${accountId}/count?${searchParams}`); return (response.data as Counter).count; } }); } export function useCreateRaidGroup(){ const queryClient = useQueryClient(); return useMutation({ mutationKey: ["createRaidGroup"], mutationFn: async ({raidGroupName, gameId, iconFile}:{raidGroupName: string; gameId: string; iconFile: File | null;}) => { const formData = new FormData(); if(iconFile){ formData.append("iconFile", iconFile); } formData.append("raidGroupName", raidGroupName); formData.append("gameId", gameId); await api.post( "/raidGroup", formData ); }, onSuccess: () => { void queryClient.invalidateQueries({ queryKey: ["raidGroups"] }); } }); } export function useUpdateRaidGroup(){ const queryClient = useQueryClient(); return useMutation({ mutationKey: ["updateRaidGroup"], mutationFn: async ({raidGroup, iconFile}:{raidGroup: RaidGroup; iconFile: File | null;}) => { const formData = new FormData(); if(iconFile){ formData.append("iconFile", iconFile); } formData.append("raidGroupName", raidGroup.raidGroupName); formData.append("gameId", raidGroup.gameId); if(raidGroup.raidGroupIcon){ formData.append("raidGroupIcon", raidGroup.raidGroupIcon); } await api.put(`/raidGroup/${raidGroup.raidGroupId}`, formData); }, onSuccess: () => { void queryClient.invalidateQueries({ queryKey: ["raidGroups"] }); } }); } export function useDeleteRaidGroup(){ const queryClient = useQueryClient(); return useMutation({ mutationKey: ["deleteRaidGroup"], mutationFn: async (raidGroupId: string) => { await api.delete(`/raidGroup/${raidGroupId}`); }, onSuccess: () => { void queryClient.invalidateQueries({ queryKey: ["raidGroups"] }); } }); }