This commit is contained in:
efrilm
2025-09-18 03:04:06 +07:00
parent 3a56e56c69
commit 4640d14cb7
7 changed files with 1232 additions and 509 deletions
+52
View File
@@ -0,0 +1,52 @@
import { RewardRequest } from '@/types/services/reward'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'react-toastify'
import { api } from '../api'
export const useRewardsMutation = () => {
const queryClient = useQueryClient()
const createReward = useMutation({
mutationFn: async (newReward: RewardRequest) => {
const response = await api.post('/marketing/rewards', newReward)
return response.data
},
onSuccess: () => {
toast.success('Reward created successfully!')
queryClient.invalidateQueries({ queryKey: ['rewards'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
}
})
const updateReward = useMutation({
mutationFn: async ({ id, payload }: { id: string; payload: RewardRequest }) => {
const response = await api.put(`/marketing/rewards/${id}`, payload)
return response.data
},
onSuccess: () => {
toast.success('Reward updated successfully!')
queryClient.invalidateQueries({ queryKey: ['rewards'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Update failed')
}
})
const deleteReward = useMutation({
mutationFn: async (id: string) => {
const response = await api.delete(`/marketing/rewards/${id}`)
return response.data
},
onSuccess: () => {
toast.success('Reward deleted successfully!')
queryClient.invalidateQueries({ queryKey: ['rewards'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Delete failed')
}
})
return { createReward, updateReward, deleteReward }
}
+46
View File
@@ -0,0 +1,46 @@
import { useQuery } from '@tanstack/react-query'
import { api } from '../api'
import { Reward, Rewards } from '@/types/services/reward'
interface RewardQueryParams {
page?: number
limit?: number
search?: string
}
export function useRewards(params: RewardQueryParams = {}) {
const { page = 1, limit = 10, search = '', ...filters } = params
return useQuery<Rewards>({
queryKey: ['rewards', { 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/rewards?${queryParams.toString()}`)
return res.data.data
}
})
}
export function useRewardById(id: string) {
return useQuery<Reward>({
queryKey: ['rewards', id],
queryFn: async () => {
const res = await api.get(`/marketing/rewards/${id}`)
return res.data.data
}
})
}