discount
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { api } from '../api'
|
||||
import {
|
||||
CategoryRule,
|
||||
Discount,
|
||||
DiscountOutlet,
|
||||
DiscountsResponse,
|
||||
FetchDiscountsParams,
|
||||
ProductRule
|
||||
} from '@/types/services/discount'
|
||||
|
||||
export function useDiscounts(params: FetchDiscountsParams = {}) {
|
||||
const { page = 1, limit = 10, search = '', ...filters } = params
|
||||
|
||||
return useQuery<DiscountsResponse>({
|
||||
queryKey: ['discounts', { 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(`/discounts?${queryParams.toString()}`)
|
||||
console.log(res.data.data)
|
||||
return res.data.data
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function useAllDiscounts() {
|
||||
return useQuery<Discount[]>({
|
||||
queryKey: ['discounts', 'all'],
|
||||
queryFn: async () => {
|
||||
const res = await api.get('/discounts?page=1&limit=100')
|
||||
return res.data.discounts
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function useDiscountsByCampaign(campaignId: string) {
|
||||
return useQuery<Discount[]>({
|
||||
queryKey: ['discounts', 'campaign', campaignId],
|
||||
queryFn: async () => {
|
||||
const res = await api.get(`/discounts?campaign_id=${campaignId}&limit=100`)
|
||||
return res.data.discounts
|
||||
},
|
||||
enabled: !!campaignId
|
||||
})
|
||||
}
|
||||
|
||||
export function useDiscountById(id: string) {
|
||||
return useQuery<Discount>({
|
||||
queryKey: ['discount', id],
|
||||
queryFn: async () => {
|
||||
const res = await api.get(`/discounts/${id}`)
|
||||
return res.data
|
||||
},
|
||||
enabled: !!id,
|
||||
staleTime: 5 * 60 * 1000
|
||||
})
|
||||
}
|
||||
|
||||
export function useDiscountByCode(code: string) {
|
||||
return useQuery<Discount>({
|
||||
queryKey: ['discount', 'code', code],
|
||||
queryFn: async () => {
|
||||
const res = await api.get(`/discounts/code/${code}`)
|
||||
return res.data
|
||||
},
|
||||
enabled: !!code,
|
||||
staleTime: 5 * 60 * 1000
|
||||
})
|
||||
}
|
||||
|
||||
export function useDiscountOutlets(discountId: string) {
|
||||
return useQuery<DiscountOutlet[]>({
|
||||
queryKey: ['discount', discountId, 'outlets'],
|
||||
queryFn: async () => {
|
||||
const res = await api.get(`/discounts/${discountId}/outlets`)
|
||||
return res.data
|
||||
},
|
||||
enabled: !!discountId
|
||||
})
|
||||
}
|
||||
|
||||
export function useProductRules(discountId: string) {
|
||||
return useQuery<ProductRule[]>({
|
||||
queryKey: ['discount', discountId, 'product-rules'],
|
||||
queryFn: async () => {
|
||||
const res = await api.get(`/discounts/${discountId}/products`)
|
||||
return res.data
|
||||
},
|
||||
enabled: !!discountId
|
||||
})
|
||||
}
|
||||
|
||||
export function useCategoryRules(discountId: string) {
|
||||
return useQuery<CategoryRule[]>({
|
||||
queryKey: ['discount', discountId, 'category-rules'],
|
||||
queryFn: async () => {
|
||||
const res = await api.get(`/discounts/${discountId}/categories`)
|
||||
return res.data
|
||||
},
|
||||
enabled: !!discountId
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user