People tab working
This commit is contained in:
88
src/hooks/PersonCharacterHooks.ts
Normal file
88
src/hooks/PersonCharacterHooks.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
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 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"] });
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user