Admin page raid groups tab working
This commit is contained in:
139
src/hooks/RaidGroupHooks.ts
Normal file
139
src/hooks/RaidGroupHooks.ts
Normal file
@@ -0,0 +1,139 @@
|
||||
import { RaidGroup } from "@/interface/RaidGroup";
|
||||
import { api } from "@/util/AxiosUtil";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
|
||||
export function useGetRaidGroups(page: number, pageSize: number, searchTerm?: string){
|
||||
return useQuery({
|
||||
queryKey: ["raidGroups", { 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?${params}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to get raid groups");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
|
||||
return response.data as RaidGroup[];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useGetRaidGroupsCount(searchTerm?: string){
|
||||
const searchParams = new URLSearchParams();
|
||||
if(searchTerm){
|
||||
searchParams.append("searchTerm", searchTerm);
|
||||
}
|
||||
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["raidGroups", "count", searchTerm],
|
||||
queryFn: async () => {
|
||||
const response = await api.get(`/raidGroup/count?${searchParams}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to get raid groups count");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
|
||||
return response.data.count as number;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateRaidGroup(){
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["createRaidGroup"],
|
||||
mutationFn: async ({raidGroupName, gameId, iconFile}:{raidGroupName: string; gameId: string; iconFile: File | null;}) => {
|
||||
const formData = new FormData();
|
||||
if(iconFile){
|
||||
formData.append("iconFile", iconFile);
|
||||
}
|
||||
formData.append("raidGroupName", raidGroupName);
|
||||
formData.append("gameId", gameId);
|
||||
|
||||
const response = await api.post(
|
||||
"/raidGroup",
|
||||
formData
|
||||
);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to create raid group");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["raidGroups"] });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateRaidGroup(){
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["updateRaidGroup"],
|
||||
mutationFn: async ({raidGroup, iconFile}:{raidGroup: RaidGroup; iconFile: File | null;}) => {
|
||||
const formData = new FormData();
|
||||
if(iconFile){
|
||||
formData.append("iconFile", iconFile);
|
||||
}
|
||||
formData.append("raidGroupName", raidGroup.raidGroupName);
|
||||
formData.append("gameId", raidGroup.gameId);
|
||||
if(raidGroup.raidGroupIcon){
|
||||
formData.append("raidGroupIcon", raidGroup.raidGroupIcon);
|
||||
}
|
||||
|
||||
const response = await api.put(`/raidGroup/${raidGroup.raidGroupId}`, formData);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to update raid group");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["raidGroups"] });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteRaidGroup(){
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["deleteRaidGroup"],
|
||||
mutationFn: async (raidGroupId: string) => {
|
||||
const response = await api.delete(`/raidGroup/${raidGroupId}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to delete raid group");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["raidGroups"] });
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user