added filter to category page

This commit is contained in:
Jelaletdin12
2025-12-20 03:34:46 +05:00
parent 73cd90207c
commit 903d6e1f4f
13 changed files with 1041 additions and 784 deletions

View File

@@ -5,17 +5,24 @@ export const categoriesApi = baseApi.injectEndpoints({
getCategories: builder.query({
query: (type = "tree") => `/categories?type=${type}`,
}),
getCategoryProducts: builder.query({
query: ({ categoryId, page = 1, limit }) => {
let url = `categories/${categoryId}/products?page=${page}`;
if (limit) url += `&limit=${limit}`;
return url;
query: ({ categoryId, page = 1, limit, brands, min_price, max_price }) => {
const params = new URLSearchParams();
params.append('page', page);
if (limit) params.append('limit', limit);
if (brands) params.append('brands', brands);
if (min_price) params.append('min_price', min_price);
if (max_price) params.append('max_price', max_price);
return `categories/${categoryId}/products?${params.toString()}`;
},
transformResponse: (response) => ({
data: response.data || [],
pagination: response.pagination || {},
}),
}),
getAllCategoryProducts: builder.query({
async queryFn(category, queryApi, extraOptions, baseQuery) {
const fetchProducts = async (categoryId) => {
@@ -36,7 +43,7 @@ export const categoriesApi = baseApi.injectEndpoints({
getAllCategoryProductsPaginated: builder.query({
async queryFn(
{ category, page = 1, limit = 6 },
{ category, page = 1, limit = 6, brands, min_price, max_price },
queryApi,
extraOptions,
baseQuery
@@ -51,14 +58,20 @@ export const categoriesApi = baseApi.injectEndpoints({
const perCategoryLimit = Math.ceil(limit / categoryIds.length);
for (const categoryId of categoryIds) {
const params = new URLSearchParams();
params.append('page', currentPage);
params.append('limit', perCategoryLimit);
if (brands) params.append('brands', brands);
if (min_price) params.append('min_price', min_price);
if (max_price) params.append('max_price', max_price);
const result = await baseQuery(
`categories/${categoryId}/products?page=${currentPage}&limit=${perCategoryLimit}`
`categories/${categoryId}/products?${params.toString()}`
);
if (result.data && result.data.data) {
allPageProducts = [...allPageProducts, ...result.data.data];
hasMoreByCategory[categoryId] =
!!result.data.pagination.next_page_url;
hasMoreByCategory[categoryId] = !!result.data.pagination.next_page_url;
}
}
@@ -90,9 +103,11 @@ export const categoriesApi = baseApi.injectEndpoints({
}
},
}),
getProductById: builder.query({
query: (productId) => `/products/${productId}`,
}),
getRelatedProducts: builder.query({
query: (productId) => `/products/${productId}/related`,
}),
@@ -107,4 +122,4 @@ export const {
useLazyGetAllCategoryProductsPaginatedQuery,
useGetProductByIdQuery,
useGetRelatedProductsQuery,
} = categoriesApi;
} = categoriesApi;