141 lines
4.2 KiB
TypeScript
141 lines
4.2 KiB
TypeScript
import { PersonCharacter } from "@/interface/PersonCharacter";
|
|
import { api } from "@/util/AxiosUtil";
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
|
|
|
|
export function useGetPersonCharactersByPersonId(personId: string, raidGroupId: string){
|
|
return useQuery({
|
|
queryKey: ["personCharacters", personId],
|
|
queryFn: async () => {
|
|
const response = await api.get(`/raidGroup/${raidGroupId}/person/${personId}/character`);
|
|
|
|
if(response.status !== 200){
|
|
throw new Error("Failed to get person characters");
|
|
}
|
|
else if(response.data.errors){
|
|
throw new Error(response.data.errors.join(", "));
|
|
}
|
|
|
|
return response.data as PersonCharacter[];
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useGetPersonCharactersByPersonIdSearch(personId: string, raidGroupId: string, page: number, pageSize: number, searchTerm?: string){
|
|
const params = new URLSearchParams();
|
|
params.append("page", page.toString());
|
|
params.append("pageSize", pageSize.toString());
|
|
if(searchTerm){
|
|
params.append("searchTerm", searchTerm);
|
|
}
|
|
|
|
|
|
return useQuery({
|
|
queryKey: ["personCharacters", personId, { page, pageSize, searchTerm }],
|
|
queryFn: async () => {
|
|
const response = await api.get(`/raidGroup/${raidGroupId}/person/${personId}/character/page?${params}`);
|
|
|
|
if(response.status !== 200){
|
|
throw new Error("Failed to get person characters");
|
|
}
|
|
else if(response.data.errors){
|
|
throw new Error(response.data.errors.join(", "));
|
|
}
|
|
|
|
console.log("person characters");
|
|
console.log(response.data);
|
|
|
|
return response.data as PersonCharacter[];
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useGetPersonCharactersCountByPersonIdSearch(personId: string, raidGroupId: string, searchTerm?: string){
|
|
const params = new URLSearchParams();
|
|
if(searchTerm){
|
|
params.append("searchTerm", searchTerm);
|
|
}
|
|
|
|
return useQuery({
|
|
queryKey: ["personCharactersCount", personId, { searchTerm }],
|
|
queryFn: async () => {
|
|
const response = await api.get(`/raidGroup/${raidGroupId}/person/${personId}/character/count?${params}`);
|
|
|
|
if(response.status !== 200){
|
|
throw new Error("Failed to get person characters count");
|
|
}
|
|
else if(response.data.errors){
|
|
throw new Error(response.data.errors.join(", "));
|
|
}
|
|
|
|
return response.data.count as number;
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useCreatePersonCharacter(raidGroupId: string, personId: string){
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
return useMutation({
|
|
mutationKey: ["createPersonCharacter"],
|
|
mutationFn: async (personCharacter: PersonCharacter) => {
|
|
const response = await api.post(`/raidGroup/${raidGroupId}/person/${personId}/character`, personCharacter);
|
|
|
|
if(response.status !== 200){
|
|
throw new Error("Failed to create person character");
|
|
}
|
|
else if(response.data.errors){
|
|
throw new Error(response.data.errors.join(", "));
|
|
}
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["personCharacters"] });
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useUpdatePersonCharacter(raidGroupId: string, personId: string){
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
return useMutation({
|
|
mutationKey: ["updatePersonCharacter"],
|
|
mutationFn: async (personCharacter: PersonCharacter) => {
|
|
const response = await api.put(`/raidGroup/${raidGroupId}/person/${personId}/character/${personCharacter.personCharacterId}`, personCharacter);
|
|
|
|
if(response.status !== 200){
|
|
throw new Error("Failed to update person character");
|
|
}
|
|
else if(response.data.errors){
|
|
throw new Error(response.data.errors.join(", "));
|
|
}
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["personCharacters"] });
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useDeletePersonCharacter(raidGroupId: string, personId: string){
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
return useMutation({
|
|
mutationKey: ["deletePersonCharacter"],
|
|
mutationFn: async (personCharacterId: string) => {
|
|
const response = await api.delete(`/raidGroup/${raidGroupId}/person/${personId}/character/${personCharacterId}`);
|
|
|
|
if(response.status !== 200){
|
|
throw new Error("Failed to delete person character");
|
|
}
|
|
else if(response.data.errors){
|
|
throw new Error(response.data.errors.join(", "));
|
|
}
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["personCharacters"] });
|
|
}
|
|
});
|
|
}
|