40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|||
|
|
import { Categories } from '../../types/services/category'
|
||
|
|
import { api } from '../api'
|
||
|
|
|
||
|
|
interface CategoriesQueryParams {
|
||
|
|
page?: number
|
||
|
|
limit?: number
|
||
|
|
search?: string
|
||
|
|
}
|
||
|
|
|
||
|
|
export const useCategoriesQuery = {
|
||
|
|
getCategories: (params: CategoriesQueryParams = {}) => {
|
||
|
|
const { page = 1, limit = 10, search = '', ...filters } = params
|
||
|
|
|
||
|
|
return useQuery<Categories>({
|
||
|
|
queryKey: ['categories', { 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)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Add other filters
|
||
|
|
Object.entries(filters).forEach(([key, value]) => {
|
||
|
|
if (value !== undefined && value !== null && value !== '') {
|
||
|
|
queryParams.append(key, value.toString())
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
const res = await api.get(`/categories?${queryParams.toString()}`)
|
||
|
|
return res.data.data
|
||
|
|
},
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|