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"] });
|
||||
}
|
||||
});
|
||||
}
|
||||
138
src/hooks/PersonHooks.ts
Normal file
138
src/hooks/PersonHooks.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
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"] });
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user