Commit 9a49c7cc by Arjun Jhukal

updated the game delete and updated the invalidate logic

parent 58c945b4
"use client"; "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 { GameItem } from '@/types/game';
import Image from 'next/image'; import Image from 'next/image';
import Link from 'next/link'; import Link from 'next/link';
...@@ -38,6 +41,8 @@ function GameSkeleton() { ...@@ -38,6 +41,8 @@ function GameSkeleton() {
} }
export default function AdminGameList() { export default function AdminGameList() {
const { data, isLoading } = useGetAllGamesQuery(); const { data, isLoading } = useGetAllGamesQuery();
const [deleteGame, { isLoading: deleting }] = useDeleteGameByIdMutation();
const dispatch = useAppDispatch();
return ( return (
<div className="admin__games grid md:grid-cols-2 lg:grid-cols-3 2xl:grid-cols-4 gap-6"> <div className="admin__games grid md:grid-cols-2 lg:grid-cols-3 2xl:grid-cols-4 gap-6">
{isLoading && {isLoading &&
...@@ -50,9 +55,37 @@ export default function AdminGameList() { ...@@ -50,9 +55,37 @@ export default function AdminGameList() {
className="admin__game__card bg-[#F4F6FC] p-4 lg:p-6 rounded-[16px]" className="admin__game__card bg-[#F4F6FC] p-4 lg:p-6 rounded-[16px]"
> >
{/* Game Name */} {/* Game Name */}
<div className="flex justify-between items-start">
<h2 className="text-16 leading-[120%] font-bold mb-4"> <h2 className="text-16 leading-[120%] font-bold mb-4">
{game.name} {game.name}
</h2> </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 */} {/* Type & Provider */}
<ul className="flex justify-between items-center mb-6"> <ul className="flex justify-between items-center mb-6">
......
import { createApi } from "@reduxjs/toolkit/query/react"; import { createApi } from "@reduxjs/toolkit/query/react";
import { baseQuery } from "./baseQuery"; import { baseQuery } from "./baseQuery";
import { GameProps, GameResponseProps, SingleGameResponse } from "@/types/game"; import { GameItem, GameProps, GameResponseProps, SingleGameResponse } from "@/types/game";
import { GlobalResponse } from "@/types/config"; import { GlobalResponse } from "@/types/config";
export const gameApi = createApi({ export const gameApi = createApi({
reducerPath: "gameApi", reducerPath: "gameApi",
baseQuery: baseQuery, baseQuery: baseQuery,
tagTypes: ["games"], tagTypes: ["Games"],
endpoints: (builder) => ({ endpoints: (builder) => ({
// ➕ Add a new game
addGame: builder.mutation<GlobalResponse, FormData>({ addGame: builder.mutation<GlobalResponse, FormData>({
query: (body) => ({ query: (body) => ({
url: "/api/admin/add-game", url: "/api/admin/add-game",
method: "POST", method: "POST",
body: body body,
}), }),
invalidatesTags: ["games"] invalidatesTags: [{ type: "Games", id: "LIST" }],
}), }),
// 📋 Fetch all games
getAllGames: builder.query<GameResponseProps, void>({ getAllGames: builder.query<GameResponseProps, void>({
query: () => ({ query: () => ({
url: '/api/admin/games', url: "/api/admin/games",
method: 'GET', 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 }>({ getGameById: builder.query<SingleGameResponse, { id: string | number }>({
query: ({ id }) => ({ query: ({ id }) => ({
url: `/api/admin/game/${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 }) => ({ query: ({ id, body }) => ({
url: `/api/admin/game/${id}`, url: `/api/admin/game/${id}`,
method: "POST", 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 }>({ deleteGameById: builder.mutation<GlobalResponse, { id: string | number }>({
query: ({ id }) => ({ query: ({ id }) => ({
url: `/api/admin/game/${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; export const {
\ No newline at end of file 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