Game Classes tab working
This commit is contained in:
140
src/hooks/GameClassHooks.ts
Normal file
140
src/hooks/GameClassHooks.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
import { GameClass } from "@/interface/GameClass";
|
||||
import { api } from "@/util/AxiosUtil";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
|
||||
export function useGetGameClasses(gameId: string, page: number, pageSize: number, searchTerm?: string){
|
||||
return useQuery({
|
||||
queryKey: ["gameClasses", gameId, { 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(`/gameClass/game/${gameId}?${params}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to get game classes");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
|
||||
return response.data as GameClass[];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useGetGameClassesCount(gameId: string, searchTerm?: string){
|
||||
const searchParams = new URLSearchParams();
|
||||
if(searchTerm){
|
||||
searchParams.append("searchTerm", searchTerm);
|
||||
}
|
||||
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["gameClasses", gameId, "count", searchTerm],
|
||||
queryFn: async () => {
|
||||
const response = await api.get(`/gameClass/game/${gameId}/count?${searchParams}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to get game classes count");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
|
||||
return response.data.count as number;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export function useCreateGameClass(){
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["createGameClass"],
|
||||
mutationFn: async ({gameId, gameClassName, iconFile}:{gameId: string; gameClassName: string; iconFile: File | null}) => {
|
||||
const formData = new FormData();
|
||||
if(iconFile){
|
||||
formData.append("iconFile", iconFile);
|
||||
}
|
||||
formData.append("gameClassName", gameClassName);
|
||||
formData.append("gameId", gameId);
|
||||
|
||||
const response = await api.post(
|
||||
`/gameClass/game/${gameId}`,
|
||||
formData
|
||||
);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to create game class");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["gameClasses"] });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateGameClass(){
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["updateGameClass"],
|
||||
mutationFn: async ({gameClass, iconFile}:{gameClass: GameClass; iconFile: File | null}) => {
|
||||
const formData = new FormData();
|
||||
if(iconFile){
|
||||
formData.append("iconFile", iconFile);
|
||||
}
|
||||
formData.append("gameClassName", gameClass.gameClassName);
|
||||
formData.append("gameId", gameClass.gameId);
|
||||
if(gameClass.gameClassIcon){
|
||||
formData.append("gameClassIcon", gameClass.gameClassIcon);
|
||||
}
|
||||
|
||||
const response = await api.put(`/gameClass/${gameClass.gameClassId}/game/${gameClass.gameId}`, formData);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to update game class");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["gameClasses"] });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteGameClass(){
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["deleteGameClass"],
|
||||
mutationFn: async (gameClass: GameClass) => {
|
||||
const response = await api.delete(`/gameClass/${gameClass.gameClassId}/game/${gameClass.gameId}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to delete game class");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["gameClasses"] });
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user