Game Prize

This commit is contained in:
efrilm
2025-09-18 01:07:13 +07:00
parent 757d9f524a
commit 7252c05569
11 changed files with 1181 additions and 2 deletions
+52
View File
@@ -0,0 +1,52 @@
import { GamePrizeRequest } from '@/types/services/gamePrize'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'react-toastify'
import { api } from '../api'
export const useGamePrizesMutation = () => {
const queryClient = useQueryClient()
const createGamePrize = useMutation({
mutationFn: async (newGamePrize: GamePrizeRequest) => {
const response = await api.post('/marketing/game-prizes', newGamePrize)
return response.data
},
onSuccess: () => {
toast.success('GamePrize created successfully!')
queryClient.invalidateQueries({ queryKey: ['gamePrizes'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
}
})
const updateGamePrize = useMutation({
mutationFn: async ({ id, payload }: { id: string; payload: GamePrizeRequest }) => {
const response = await api.put(`/marketing/game-prizes/${id}`, payload)
return response.data
},
onSuccess: () => {
toast.success('GamePrize updated successfully!')
queryClient.invalidateQueries({ queryKey: ['gamePrizes'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Update failed')
}
})
const deleteGamePrize = useMutation({
mutationFn: async (id: string) => {
const response = await api.delete(`/marketing/game-prizes/${id}`)
return response.data
},
onSuccess: () => {
toast.success('GamePrize deleted successfully!')
queryClient.invalidateQueries({ queryKey: ['gamePrizes'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Delete failed')
}
})
return { createGamePrize, updateGamePrize, deleteGamePrize }
}
+46
View File
@@ -0,0 +1,46 @@
import { useQuery } from '@tanstack/react-query'
import { api } from '../api'
import { GamePrize, GamePrizes } from '@/types/services/gamePrize'
interface GamePrizeQueryParams {
page?: number
limit?: number
search?: string
}
export function useGamePrizes(params: GamePrizeQueryParams = {}) {
const { page = 1, limit = 10, search = '', ...filters } = params
return useQuery<GamePrizes>({
queryKey: ['gamePrizes', { 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/game-prizes?${queryParams.toString()}`)
return res.data.data
}
})
}
export function useGamePrizeById(id: string) {
return useQuery<GamePrize>({
queryKey: ['gamePrizes', id],
queryFn: async () => {
const res = await api.get(`/marketing/game-prizes/${id}`)
return res.data.data
}
})
}