Commit 9a49c7cc by Arjun Jhukal

updated the game delete and updated the invalidate logic

parent 58c945b4
"use client";
import { useGetAllGamesQuery } from '@/services/gameApi'
import ActionGroup from '@/components/molecules/Action';
import { useAppDispatch } from '@/hooks/hook';
import { useDeleteGameByIdMutation, useGetAllGamesQuery } from '@/services/gameApi'
import { showToast, ToastVariant } from '@/slice/toastSlice';
import { GameItem } from '@/types/game';
import Image from 'next/image';
import Link from 'next/link';
......@@ -38,6 +41,8 @@ function GameSkeleton() {
}
export default function AdminGameList() {
const { data, isLoading } = useGetAllGamesQuery();
const [deleteGame, { isLoading: deleting }] = useDeleteGameByIdMutation();
const dispatch = useAppDispatch();
return (
<div className="admin__games grid md:grid-cols-2 lg:grid-cols-3 2xl:grid-cols-4 gap-6">
{isLoading &&
......@@ -50,9 +55,37 @@ export default function AdminGameList() {
className="admin__game__card bg-[#F4F6FC] p-4 lg:p-6 rounded-[16px]"
>
{/* Game Name */}
<div className="flex justify-between items-start">
<h2 className="text-16 leading-[120%] font-bold mb-4">
{game.name}
</h2>
<ActionGroup
onDelete={async () => {
try {
const response = await deleteGame({ id: game.id }).unwrap();
dispatch(
showToast(
{
variant: ToastVariant.SUCCESS,
message: response?.message || "Game Deleted Successfully"
}
)
)
}
catch (e) {
dispatch(
showToast(
{
variant: ToastVariant.ERROR,
message: "Unable to Delete Game"
}
)
)
}
}}
/>
</div>
{/* Type & Provider */}
<ul className="flex justify-between items-center mb-6">
......
import { createApi } from "@reduxjs/toolkit/query/react";
import { baseQuery } from "./baseQuery";
import { GameProps, GameResponseProps, SingleGameResponse } from "@/types/game";
import { GameItem, GameProps, GameResponseProps, SingleGameResponse } from "@/types/game";
import { GlobalResponse } from "@/types/config";
export const gameApi = createApi({
reducerPath: "gameApi",
baseQuery: baseQuery,
tagTypes: ["games"],
tagTypes: ["Games"],
endpoints: (builder) => ({
// ➕ Add a new game
addGame: builder.mutation<GlobalResponse, FormData>({
query: (body) => ({
url: "/api/admin/add-game",
method: "POST",
body: body
body,
}),
invalidatesTags: ["games"]
invalidatesTags: [{ type: "Games", id: "LIST" }],
}),
// 📋 Fetch all games
getAllGames: builder.query<GameResponseProps, void>({
query: () => ({
url: '/api/admin/games',
method: 'GET',
url: "/api/admin/games",
method: "GET",
}),
providesTags: ['games']
providesTags: (result) =>
result?.data?.data
? [
{ type: "Games", id: "LIST" },
...result.data.data.map((game: GameItem) => ({
type: "Games" as const,
id: game.id,
})),
]
: [{ type: "Games", id: "LIST" }],
}),
// 🎮 Get single game by ID
getGameById: builder.query<SingleGameResponse, { id: string | number }>({
query: ({ id }) => ({
url: `/api/admin/game/${id}`,
method: 'GET',
method: "GET",
}),
providesTags: ['games']
providesTags: (result, error, { id }) => [{ type: "Games", id }],
}),
updateGameById: builder.mutation<SingleGameResponse, { id: string | number, body: FormData }>({
// ✏️ Update game by ID
updateGameById: builder.mutation<
SingleGameResponse,
{ id: string | number; body: FormData }
>({
query: ({ id, body }) => ({
url: `/api/admin/game/${id}`,
method: "POST",
body: body
body,
}),
invalidatesTags: ["games"]
invalidatesTags: (result, error, { id }) => [
{ type: "Games", id },
{ type: "Games", id: "LIST" },
],
}),
// ❌ Delete game by ID
deleteGameById: builder.mutation<GlobalResponse, { id: string | number }>({
query: ({ id }) => ({
url: `/api/admin/game/${id}`,
method: "DELETE"
method: "DELETE",
}),
invalidatesTags: (result, error, { id }) => [
{ type: "Games", id },
{ type: "Games", id: "LIST" },
],
}),
}),
invalidatesTags: ["games"]
})
})
})
});
export const { useAddGameMutation, useGetAllGamesQuery, useGetGameByIdQuery, useUpdateGameByIdMutation, useDeleteGameByIdMutation } = gameApi;
\ No newline at end of file
export const {
useAddGameMutation,
useGetAllGamesQuery,
useGetGameByIdQuery,
useUpdateGameByIdMutation,
useDeleteGameByIdMutation,
} = gameApi;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment