70 lines
2.0 KiB
JavaScript
70 lines
2.0 KiB
JavaScript
import { baseApi } from "./baseApi";
|
|
|
|
export const brandsApi = baseApi.injectEndpoints({
|
|
endpoints: (builder) => ({
|
|
getBrands: builder.query({
|
|
query: (params = {}) => {
|
|
const queryParams = new URLSearchParams();
|
|
if (params.type) {
|
|
queryParams.append("type", params.type);
|
|
}
|
|
if (params.page) {
|
|
queryParams.append("page", params.page);
|
|
}
|
|
if (params.limit) {
|
|
queryParams.append("limit", params.limit);
|
|
}
|
|
|
|
const queryString = queryParams.toString();
|
|
return `/brands${queryString ? `?${queryString}` : ""}`;
|
|
},
|
|
transformResponse: (response) => response.data || response,
|
|
}),
|
|
|
|
getBrandDetails: builder.query({
|
|
query: (brandId) => `/brands/${brandId}`,
|
|
transformResponse: (response) => response.data || response,
|
|
}),
|
|
|
|
getBrandProducts: builder.query({
|
|
query: (params) => {
|
|
if (typeof params === 'string' || typeof params === 'number') {
|
|
return `/brands/${params}/products`;
|
|
}
|
|
|
|
const { id, page = 1, limit, sorting, min_price, max_price, brands } = params;
|
|
let url = `/brands/${id}/products?page=${page}`;
|
|
|
|
if (limit) {
|
|
url += `&limit=${limit}`;
|
|
}
|
|
if (sorting) {
|
|
url += `&sorting=${encodeURIComponent(sorting)}`;
|
|
}
|
|
if (min_price) {
|
|
url += `&min_price=${encodeURIComponent(min_price)}`;
|
|
}
|
|
if (max_price) {
|
|
url += `&max_price=${encodeURIComponent(max_price)}`;
|
|
}
|
|
if (brands) {
|
|
url += `&brands=${encodeURIComponent(brands)}`;
|
|
}
|
|
|
|
return url;
|
|
},
|
|
transformResponse: (response) => ({
|
|
data: response.data || response,
|
|
pagination: response.pagination || {},
|
|
}),
|
|
}),
|
|
}),
|
|
});
|
|
|
|
export const {
|
|
useGetBrandsQuery,
|
|
useLazyGetBrandsQuery,
|
|
useGetBrandDetailsQuery,
|
|
useGetBrandProductsQuery,
|
|
useLazyGetBrandProductsQuery,
|
|
} = brandsApi; |