139 lines
3.8 KiB
TypeScript
139 lines
3.8 KiB
TypeScript
import { Person } from "@/interface/Person";
|
|
import { api } from "@/util/AxiosUtil";
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
|
|
|
|
export function useGetPerson(raidGroupId: string, personId: string, disabled: boolean){
|
|
return useQuery({
|
|
queryKey: ["people", raidGroupId, personId],
|
|
queryFn: async () => {
|
|
const response = await api.get(`/raidGroup/${raidGroupId}/person/${personId}`);
|
|
|
|
if(response.status !== 200){
|
|
throw new Error("Failed to get person");
|
|
}
|
|
else if(response.data.errors){
|
|
throw new Error(response.data.errors.join(", "));
|
|
}
|
|
|
|
return response.data as Person;
|
|
},
|
|
enabled: !disabled
|
|
});
|
|
}
|
|
|
|
export function useGetPeopleByRaidGroup(raidGroupId: string, page: number, pageSize: number, searchTerm?: string){
|
|
return useQuery({
|
|
queryKey: ["people", raidGroupId, { page, pageSize, searchTerm }],
|
|
queryFn: async () => {
|
|
const searchParams = new URLSearchParams();
|
|
searchParams.append("page", page.toString());
|
|
searchParams.append("pageSize", pageSize.toString());
|
|
if(searchTerm){
|
|
searchParams.append("searchTerm", searchTerm);
|
|
}
|
|
|
|
const response = await api.get(`/raidGroup/${raidGroupId}/person?${searchParams}`);
|
|
|
|
if(response.status !== 200){
|
|
throw new Error("Failed to get people");
|
|
}
|
|
else if(response.data.errors){
|
|
throw new Error(response.data.errors.join(", "));
|
|
}
|
|
|
|
return response.data as Person[];
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useGetPeopleByRaidGroupCount(raidGroupId: string, searchTerm?: string){
|
|
const searchParams = new URLSearchParams();
|
|
if(searchTerm){
|
|
searchParams.append("searchTerm", searchTerm);
|
|
}
|
|
|
|
|
|
return useQuery({
|
|
queryKey: ["people", raidGroupId, "count", searchTerm],
|
|
queryFn: async () => {
|
|
const response = await api.get(`/raidGroup/${raidGroupId}/person/count?${searchParams}`);
|
|
|
|
if(response.status !== 200){
|
|
throw new Error("Failed to get people count");
|
|
}
|
|
else if(response.data.errors){
|
|
throw new Error(response.data.errors.join(", "));
|
|
}
|
|
|
|
return response.data.count as number;
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useCreatePerson(){
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
return useMutation({
|
|
mutationKey: ["createPerson"],
|
|
mutationFn: async ({raidGroupId, personName, discordId}:{raidGroupId: string; personName: string; discordId?: string;}) => {
|
|
const response = await api.post(`/raidGroup/${raidGroupId}/person`, {raidGroupId, personName, discordId});
|
|
|
|
if(response.status !== 200){
|
|
throw new Error("Failed to create person");
|
|
}
|
|
else if(response.data.errors){
|
|
throw new Error(response.data.errors.join(", "));
|
|
}
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["people"] });
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useUpdatePerson(){
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
return useMutation({
|
|
mutationKey: ["updatePerson"],
|
|
mutationFn: async (person: Person) => {
|
|
const response = await api.put(`/raidGroup/${person.raidGroupId}/person/${person.personId}`, person);
|
|
|
|
if(response.status !== 200){
|
|
throw new Error("Failed to update person");
|
|
}
|
|
else if(response.data.errors){
|
|
throw new Error(response.data.errors.join(", "));
|
|
}
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["people"] });
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useDeletePerson(){
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
return useMutation({
|
|
mutationKey: ["deletePerson"],
|
|
mutationFn: async ({raidGroupId, personId}:{raidGroupId: string; personId: string;}) => {
|
|
const response = await api.delete(`/raidGroup/${raidGroupId}/person/${personId}`);
|
|
|
|
if(response.status !== 200){
|
|
throw new Error("Failed to delete person");
|
|
}
|
|
else if(response.data.errors){
|
|
throw new Error(response.data.errors.join(", "));
|
|
}
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["people"] });
|
|
}
|
|
});
|
|
}
|