57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import { api } from '../api'
|
|
import { Vendor, Vendors } from '@/types/services/vendor'
|
|
|
|
interface VendorQueryParams {
|
|
page?: number
|
|
limit?: number
|
|
search?: string
|
|
}
|
|
|
|
export function useVendors(params: VendorQueryParams = {}) {
|
|
const { page = 1, limit = 10, search = '', ...filters } = params
|
|
|
|
return useQuery<Vendors>({
|
|
queryKey: ['vendors', { 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(`/vendors?${queryParams.toString()}`)
|
|
return res.data.data
|
|
}
|
|
})
|
|
}
|
|
|
|
export function useVendorActive() {
|
|
return useQuery<Vendor[]>({
|
|
queryKey: ['vendors/active'],
|
|
queryFn: async () => {
|
|
const res = await api.get(`/vendors/active`)
|
|
return res.data.data
|
|
}
|
|
})
|
|
}
|
|
|
|
export function useVendorById(id: string) {
|
|
return useQuery<Vendor>({
|
|
queryKey: ['vendors', id],
|
|
queryFn: async () => {
|
|
const res = await api.get(`/vendors/${id}`)
|
|
return res.data.data
|
|
}
|
|
})
|
|
}
|