Files
pos-dashboard-v2/src/services/queries/categories.ts
T

37 lines
1.0 KiB
TypeScript
Raw Normal View History

2025-08-05 14:34:36 +07:00
import { useQuery } from '@tanstack/react-query'
import { Categories } from '../../types/services/category'
import { api } from '../api'
interface CategoriesQueryParams {
page?: number
limit?: number
search?: string
}
2025-08-06 14:41:23 +07:00
export function useCategories(params: CategoriesQueryParams = {}) {
const { page = 1, limit = 10, search = '', ...filters } = params
2025-08-05 14:34:36 +07:00
2025-08-06 14:41:23 +07:00
return useQuery<Categories>({
queryKey: ['categories', { page, limit, search, ...filters }],
queryFn: async () => {
const queryParams = new URLSearchParams()
2025-08-05 14:34:36 +07:00
2025-08-06 14:41:23 +07:00
queryParams.append('page', page.toString())
queryParams.append('limit', limit.toString())
2025-08-05 14:34:36 +07:00
2025-08-06 14:41:23 +07:00
if (search) {
queryParams.append('search', search)
}
Object.entries(filters).forEach(([key, value]) => {
if (value !== undefined && value !== null && value !== '') {
queryParams.append(key, value.toString())
2025-08-05 14:34:36 +07:00
}
2025-08-06 14:41:23 +07:00
})
2025-08-05 14:34:36 +07:00
2025-08-06 14:41:23 +07:00
const res = await api.get(`/categories?${queryParams.toString()}`)
return res.data.data
}
})
2025-08-05 14:34:36 +07:00
}