Class Groups tab working
This commit is contained in:
135
src/hooks/ClassGroupHooks.ts
Normal file
135
src/hooks/ClassGroupHooks.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
import { ClassGroup } from "@/interface/ClassGroup";
|
||||
import { api } from "@/util/AxiosUtil";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
|
||||
export function useGetClassGroups(raidGroupId: string, page: number, pageSize: number, searchTerm?: string){
|
||||
return useQuery({
|
||||
queryKey: ["classGroups", raidGroupId, { page, pageSize, searchTerm }],
|
||||
queryFn: async () => {
|
||||
const params = new URLSearchParams();
|
||||
params.append("page", page.toString());
|
||||
params.append("pageSize", pageSize.toString());
|
||||
if(searchTerm){
|
||||
params.append("searchTerm", searchTerm);
|
||||
}
|
||||
|
||||
const response = await api.get(`/raidGroup/${raidGroupId}/classGroup?${params}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to get class groups");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
|
||||
return response.data as ClassGroup[];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useGetClassGroupsCount(raidGroupId: string, searchTerm?: string){
|
||||
const searchParams = new URLSearchParams();
|
||||
if(searchTerm){
|
||||
searchParams.append("searchTerm", searchTerm);
|
||||
}
|
||||
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["classGroups", "count", searchTerm],
|
||||
queryFn: async () => {
|
||||
const response = await api.get(`/raidGroup/${raidGroupId}/classGroup/count?${searchParams}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to get class groups count");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
|
||||
return response.data.count as number;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export function useCreateClassGroup(raidGroupId: string){
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["createClassGroup"],
|
||||
mutationFn: async ({classGroupName, gameClassIds}:{classGroupName: string; gameClassIds: string[];}) => {
|
||||
const response = await api.post(`/raidGroup/${raidGroupId}/classGroup`,
|
||||
{
|
||||
classGroup: {
|
||||
classGroupName: classGroupName,
|
||||
raidGroupId: raidGroupId
|
||||
},
|
||||
gameClassIds
|
||||
}
|
||||
);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to create class group");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({queryKey: ["classGroups"]});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateClassGroup(raidGroupId: string){
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["updateClassGroup"],
|
||||
mutationFn: async ({classGroup, gameClassIds}:{classGroup: ClassGroup; gameClassIds: string[];}) => {
|
||||
console.log("Hit");
|
||||
const response = await api.put(`/raidGroup/${raidGroupId}/classGroup/${classGroup.classGroupId}`,
|
||||
{
|
||||
classGroup,
|
||||
gameClassIds
|
||||
}
|
||||
);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to update class group");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({queryKey: ["gameClasses", "classGroups"]});
|
||||
queryClient.invalidateQueries({queryKey: ["classGroups"]});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteClassGroup(raidGroupId: string, classGroupId: string){
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["deleteClassGroup", classGroupId, raidGroupId],
|
||||
mutationFn: async () => {
|
||||
const response = await api.delete(`/raidGroup/${raidGroupId}/classGroup/${classGroupId}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to delete class group");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({queryKey: ["classGroups"]});
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user