feat list product and category

This commit is contained in:
ferdiansyah783
2025-08-05 14:34:36 +07:00
parent fffa2ead5c
commit 0fde122ac4
8 changed files with 388 additions and 382 deletions
+41
View File
@@ -0,0 +1,41 @@
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
},
// Cache for 5 minutes
staleTime: 5 * 60 * 1000
})
}
}
+34 -5
View File
@@ -1,15 +1,44 @@
import { useQuery } from '@tanstack/react-query'
import { Products } from '../../types/services/products'
import { Products } from '../../types/services/product'
import { api } from '../api'
interface ProductsQueryParams {
page?: number
limit?: number
search?: string
// Add other filter parameters as needed
category_id?: string
is_active?: boolean
}
export const useProductsQuery = {
getProducts: () => {
getProducts: (params: ProductsQueryParams = {}) => {
const { page = 1, limit = 10, search = '', ...filters } = params
return useQuery<Products>({
queryKey: ['products'],
queryKey: ['products', { page, limit, search, ...filters }],
queryFn: async () => {
const res = await api.get('/products')
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(`/products?${queryParams.toString()}`)
return res.data.data
}
},
// Cache for 5 minutes
staleTime: 5 * 60 * 1000
})
}
}