155 lines
4.2 KiB
TypeScript
155 lines
4.2 KiB
TypeScript
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 useGetClassGroupsByRaidLayout(raidGroupId: string, raidLayoutId: string | undefined){
|
|
return useQuery({
|
|
queryKey: ["classGroups", "raidLayout", raidLayoutId],
|
|
queryFn: async () => {
|
|
const response = await api.get(`/raidGroup/${raidGroupId}/classGroup/raidLayout/${raidLayoutId}`);
|
|
|
|
if(response.status !== 200){
|
|
throw new Error("Failed to get class groups");
|
|
}
|
|
else if(response.data.error){
|
|
throw new Error(response.data.errors.join(", "));
|
|
}
|
|
|
|
return response.data as (ClassGroup | null)[];
|
|
},
|
|
enabled: !!raidLayoutId
|
|
})
|
|
}
|
|
|
|
|
|
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"]});
|
|
}
|
|
});
|
|
}
|