Games tab on admin page working

This commit is contained in:
2025-03-04 21:14:24 -05:00
parent 91800574e4
commit ffe51d6fbb
17 changed files with 700 additions and 27 deletions

131
src/hooks/GameHooks.ts Normal file
View File

@@ -0,0 +1,131 @@
import { Game } from "@/interface/Game";
import { api } from "@/util/AxiosUtil";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
export function useGetGames(page: number, pageSize: number, searchTerm?: string){
return useQuery({
queryKey: ["games", { page, pageSize, searchTerm }],
queryFn: async () => {
const params = new URLSearchParams();
params.append("page", page.toString());
params.append("pageSize", pageSize.toString());
if(searchTerm){
params.append("search", searchTerm);
}
const response = await api.get(`/game?${params}`);
if(response.status !== 200){
throw new Error("Failed to get games");
}
else if(response.data.errors){
throw new Error(response.data.errors.join(", "));
}
return response.data as Game[];
}
});
}
export function useGetGamesCount(){
return useQuery({
queryKey: ["games", "count"],
queryFn: async () => {
const response = await api.get("/game/count");
if(response.status !== 200){
throw new Error("Failed to get games count");
}
else if(response.data.errors){
throw new Error(response.data.errors.join(", "));
}
return response.data.count as number;
}
});
}
export function useCreateGame(){
const queryClient = useQueryClient();
return useMutation({
mutationKey: ["createGame"],
mutationFn: async ({gameName, iconFile}:{gameName: string, iconFile: File | null}) => {
const formData = new FormData();
if(iconFile){
formData.append("iconFile", iconFile);
}
formData.append("gameName", gameName);
const response = await api.post(
"/game",
formData
);
if(response.status !== 200){
throw new Error("Failed to create game");
}
else if(response.data.errors){
throw new Error(response.data.errors.join(", "));
}
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["games"] });
}
});
}
export function useUpdateGame(){
const queryClient = useQueryClient();
return useMutation({
mutationKey: ["updateGame"],
mutationFn: async ({game, iconFile}:{game: Game, iconFile: File | null}) => {
const formData = new FormData();
if(iconFile){
formData.append("iconFile", iconFile);
}
formData.append("gameName", game.gameName);
if(game.gameIcon){
formData.append("gameIcon", game.gameIcon);
}
const response = await api.put(`/game/${game.gameId}`, formData);
if(response.status !== 200){
throw new Error("Failed to update game");
}
else if(response.data.errors){
throw new Error(response.data.errors.join(", "));
}
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["games"] });
}
});
}
export function useDeleteGame(){
const queryClient = useQueryClient();
return useMutation({
mutationKey: ["deleteGame"],
mutationFn: async (gameId: string) => {
const response = await api.delete(`/game/${gameId}`);
if(response.status !== 200){
throw new Error("Failed to delete game");
}
else if(response.data.errors){
throw new Error(response.data.errors.join(", "));
}
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["games"] });
}
});
}