125 lines
3.8 KiB
TypeScript
125 lines
3.8 KiB
TypeScript
import { RaidGroupPermissionType } from "@/interface/RaidGroup";
|
|
import { RaidGroupRequest } from "@/interface/RaidGroupRequest";
|
|
import { api } from "@/util/AxiosUtil";
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
|
|
|
|
export function useGetRaidGroupRequests(raidGroupId: string, page: number, pageSize: number, searchTerm?: string){
|
|
const searchParams = new URLSearchParams();
|
|
searchParams.append("page", page.toString());
|
|
searchParams.append("pageSize", pageSize.toString());
|
|
if(searchTerm){
|
|
searchParams.append("searchTerm", searchTerm);
|
|
}
|
|
|
|
return useQuery({
|
|
queryKey: ["raidGroupRequest", raidGroupId, {page, pageSize, searchTerm}],
|
|
queryFn: async () => {
|
|
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(", "));
|
|
}
|
|
|
|
return response.data as RaidGroupRequest[];
|
|
},
|
|
enabled: !!raidGroupId && raidGroupId !== ""
|
|
});
|
|
}
|
|
|
|
export function useGetRaidGroupRequestCount(raidGroupId?: string, searchTerm?: string){
|
|
const searchParams = new URLSearchParams();
|
|
if(searchTerm){
|
|
searchParams.append("searchTerm", searchTerm);
|
|
}
|
|
|
|
return useQuery({
|
|
queryKey: ["raidGroupRequest", raidGroupId, "count", searchTerm],
|
|
queryFn: async () => {
|
|
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(", "));
|
|
}
|
|
|
|
return response.data.count as number;
|
|
},
|
|
enabled: !!raidGroupId && raidGroupId !== ""
|
|
});
|
|
}
|
|
|
|
|
|
export function useCreateRaidGroupRequest(raidGroupId: string){
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationKey: ["raidGroupRequest", raidGroupId],
|
|
mutationFn: async (requestMessage: string) => {
|
|
const response = await api.post(`/raidGroup/${raidGroupId}/raidGroupRequest`, {
|
|
requestMessage
|
|
});
|
|
|
|
if(response.status !== 200){
|
|
throw new Error("Failed to create raid group request");
|
|
}
|
|
else if(response.data.errors){
|
|
throw new Error(response.data.errors.join(", "));
|
|
}
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["raidGroupRequest", raidGroupId] });
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useResolveRaidGroupRequest(raidGroupId: string, raidGroupRequestId: string){
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationKey: ["raidGroupRequest", raidGroupId, raidGroupRequestId],
|
|
mutationFn: async (permission: RaidGroupPermissionType) => {
|
|
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");
|
|
}
|
|
else if(response.data.errors){
|
|
throw new Error(response.data.errors.join(", "));
|
|
}
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["raidGroupRequest", raidGroupId] });
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
export function useDeleteRaidGroupRequest(raidGroupId: string, raidGroupRequestId: string){
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationKey: ["raidGroupRequest", raidGroupId, raidGroupRequestId],
|
|
mutationFn: async () => {
|
|
const response = await api.delete(`/raidGroup/${raidGroupId}/raidGroupRequest/${raidGroupRequestId}`);
|
|
|
|
if(response.status !== 200){
|
|
throw new Error("Failed to delete raid group request");
|
|
}
|
|
else if(response.data.errors){
|
|
throw new Error(response.data.errors.join(", "));
|
|
}
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["raidGroupRequest", raidGroupId] });
|
|
}
|
|
});
|
|
}
|