This commit is contained in:
efrilm
2025-09-17 20:54:54 +07:00
parent 4655411b24
commit 8ed2786bc2
9 changed files with 534 additions and 258 deletions
+52
View File
@@ -0,0 +1,52 @@
import { TierRequest } from '@/types/services/tier'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'react-toastify'
import { api } from '../api'
export const useTiersMutation = () => {
const queryClient = useQueryClient()
const createTier = useMutation({
mutationFn: async (newTier: TierRequest) => {
const response = await api.post('/marketing/tiers', newTier)
return response.data
},
onSuccess: () => {
toast.success('Tier created successfully!')
queryClient.invalidateQueries({ queryKey: ['tiers'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
}
})
const updateTier = useMutation({
mutationFn: async ({ id, payload }: { id: string; payload: TierRequest }) => {
const response = await api.put(`/marketing/tiers/${id}`, payload)
return response.data
},
onSuccess: () => {
toast.success('Tier updated successfully!')
queryClient.invalidateQueries({ queryKey: ['tiers'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Update failed')
}
})
const deleteTier = useMutation({
mutationFn: async (id: string) => {
const response = await api.delete(`/marketing/tiers/${id}`)
return response.data
},
onSuccess: () => {
toast.success('Tier deleted successfully!')
queryClient.invalidateQueries({ queryKey: ['tiers'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Delete failed')
}
})
return { createTier, updateTier, deleteTier }
}
+46
View File
@@ -0,0 +1,46 @@
import { useQuery } from '@tanstack/react-query'
import { api } from '../api'
import { Tier, Tiers } from '@/types/services/tier'
interface TierQueryParams {
page?: number
limit?: number
search?: string
}
export function useTiers(params: TierQueryParams = {}) {
const { page = 1, limit = 10, search = '', ...filters } = params
return useQuery<Tiers>({
queryKey: ['tiers', { 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/tiers?${queryParams.toString()}`)
return res.data.data
}
})
}
export function useTierById(id: string) {
return useQuery<Tier>({
queryKey: ['tiers', id],
queryFn: async () => {
const res = await api.get(`/marketing/tiers/${id}`)
return res.data.data
}
})
}