Game
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { GameRequest } from '@/types/services/game'
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { toast } from 'react-toastify'
|
||||
import { api } from '../api'
|
||||
|
||||
export const useGamesMutation = () => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const createGame = useMutation({
|
||||
mutationFn: async (newGame: GameRequest) => {
|
||||
const response = await api.post('/marketing/games', newGame)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success('Game created successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['games'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
|
||||
}
|
||||
})
|
||||
|
||||
const updateGame = useMutation({
|
||||
mutationFn: async ({ id, payload }: { id: string; payload: GameRequest }) => {
|
||||
const response = await api.put(`/marketing/games/${id}`, payload)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success('Game updated successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['games'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Update failed')
|
||||
}
|
||||
})
|
||||
|
||||
const deleteGame = useMutation({
|
||||
mutationFn: async (id: string) => {
|
||||
const response = await api.delete(`/marketing/games/${id}`)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success('Game deleted successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['games'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Delete failed')
|
||||
}
|
||||
})
|
||||
|
||||
return { createGame, updateGame, deleteGame }
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { api } from '../api'
|
||||
import { Game, Games } from '@/types/services/game'
|
||||
|
||||
interface GameQueryParams {
|
||||
page?: number
|
||||
limit?: number
|
||||
search?: string
|
||||
}
|
||||
|
||||
export function useGames(params: GameQueryParams = {}) {
|
||||
const { page = 1, limit = 10, search = '', ...filters } = params
|
||||
|
||||
return useQuery<Games>({
|
||||
queryKey: ['games', { page, limit, search, ...filters }],
|
||||
queryFn: async () => {
|
||||
const queryParams = new URLSearchParams()
|
||||
|
||||
queryParams.append('page', page.toString())
|
||||
queryParams.append('limit', limit.toString())
|
||||
|
||||
if (search) {
|
||||
queryParams.append('search', search)
|
||||
}
|
||||
|
||||
Object.entries(filters).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null && value !== '') {
|
||||
queryParams.append(key, value.toString())
|
||||
}
|
||||
})
|
||||
|
||||
const res = await api.get(`/marketing/games?${queryParams.toString()}`)
|
||||
return res.data.data
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function useGameById(id: string) {
|
||||
return useQuery<Game>({
|
||||
queryKey: ['games', id],
|
||||
queryFn: async () => {
|
||||
const res = await api.get(`/marketing/games/${id}`)
|
||||
return res.data.data
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user