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

@@ -11,7 +11,6 @@ export const brandsApi = baseApi.injectEndpoints({
if (params.page) {
queryParams.append("page", params.page);
}
if (params.limit) {
queryParams.append("limit", params.limit);
}
@@ -29,12 +28,10 @@ export const brandsApi = baseApi.injectEndpoints({
getBrandProducts: builder.query({
query: (params) => {
// Handle both string ID and object with pagination params
if (typeof params === 'string' || typeof params === 'number') {
return `/brands/${params}/products`;
}
// Handle object with pagination
const { id, page = 1, limit } = params;
let url = `/brands/${id}/products?page=${page}`;
@@ -44,9 +41,10 @@ export const brandsApi = baseApi.injectEndpoints({
return url;
},
transformResponse: (response) => {
return response.data || response;
},
transformResponse: (response) => ({
data: response.data || response,
pagination: response.pagination || {},
}),
}),
}),
});

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;

View File

@@ -5,12 +5,13 @@ export const collectionsApi = baseApi.injectEndpoints({
getCollections: builder.query({
query: () => `/collections`,
}),
getCollectionById: builder.query({
query: (collectionId) => `/collections/${collectionId}`,
}),
getCollectionProducts: builder.query({
query: (collectionId) => `/collections/${collectionId}/products`,
// Ürünleri dönüştürerek boş kontrol edilebilir
transformResponse: (response) => {
return {
data: response.data || [],
@@ -18,7 +19,7 @@ export const collectionsApi = baseApi.injectEndpoints({
};
},
}),
// Yeni endpoint: Koleksiyonun ürün içerip içermediğini kontrol eder
checkCollectionHasProducts: builder.query({
query: (collectionId) => `/collections/${collectionId}/products?limit=1`,
transformResponse: (response) => {
@@ -27,10 +28,18 @@ export const collectionsApi = baseApi.injectEndpoints({
};
},
}),
// Sayfalı koleksiyon ürünleri için endpoint
getCollectionProductsPaginated: builder.query({
query: ({ collectionId, page = 1, limit = 6 }) =>
`/collections/${collectionId}/products?page=${page}${limit ? `&limit=${limit}` : ''}`,
query: ({ collectionId, page = 1, limit = 6, 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 `/collections/${collectionId}/products?${params.toString()}`;
},
transformResponse: (response) => ({
data: response.data || [],
pagination: response.pagination || {},

77
src/app/api/filtersApi.js Normal file
View File

@@ -0,0 +1,77 @@
import { baseApi } from "./baseApi"
export const filtersApi = baseApi.injectEndpoints({
endpoints: (builder) => ({
getFilters: builder.query({
query: (params) => {
const queryParams = new URLSearchParams()
if (params?.category_id) {
queryParams.append("category_id", String(params.category_id))
}
if (params?.collection_id) {
queryParams.append("collection_id", String(params.collection_id))
}
if (params?.brand_id) {
queryParams.append("brand_id", String(params.brand_id))
}
return `/filters?${queryParams.toString()}`
},
transformResponse: (response) => {
return {
categories: response.data?.categories || [],
brands: response.data?.brands || [],
}
},
keepUnusedDataFor: 300,
serializeQueryArgs: ({ queryArgs }) => {
if (!queryArgs) return 'no-params';
const parts = [];
if (queryArgs.category_id) {
parts.push(`cat:${queryArgs.category_id}`);
}
if (queryArgs.collection_id) {
parts.push(`col:${queryArgs.collection_id}`);
}
if (queryArgs.brand_id) {
parts.push(`brd:${queryArgs.brand_id}`);
}
return parts.length > 0 ? parts.join('|') : 'no-params';
},
merge: (currentCache, newItems) => {
return newItems;
},
refetchOnMountOrArgChange: 30, // Refetch if older than 30 seconds
refetchOnFocus: false,
refetchOnReconnect: false,
providesTags: (result, error, arg) => {
if (!arg) return [{ type: "Filters", id: "no-params" }];
const tags = [{ type: "Filters", id: "LIST" }];
if (arg.category_id) {
tags.push({ type: "Filters", id: `cat-${arg.category_id}` });
}
if (arg.collection_id) {
tags.push({ type: "Filters", id: `col-${arg.collection_id}` });
}
if (arg.brand_id) {
tags.push({ type: "Filters", id: `brd-${arg.brand_id}` });
}
return tags;
},
}),
}),
overrideExisting: true,
})
export const { useGetFiltersQuery, useLazyGetFiltersQuery } = filtersApi

View File

@@ -13,6 +13,7 @@ import { mediaApi } from "./api/bannersApi";
import { reviewsApi } from "./api/reviewApi";
import { profileApi } from "./api/myProfileApi";
import { contactApi } from "./api/contactUs";
import { filtersApi } from "./api/filtersApi";
const store = configureStore({
reducer: {
@@ -30,6 +31,7 @@ const store = configureStore({
[reviewsApi.reducerPath]: reviewsApi.reducer,
[profileApi.reducerPath]: profileApi.reducer,
[contactApi.reducerPath]: contactApi.reducer,
[filtersApi.reducerPath]: filtersApi.reducer,
},
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat(
@@ -46,6 +48,7 @@ const store = configureStore({
mediaApi.middleware,
profileApi.middleware,
contactApi.middleware,
filtersApi.middleware
),
});