diff --git a/frontend/composables/api/products/useGetProducts.ts b/frontend/composables/api/products/useGetProducts.ts index 7828df4..adfa9a1 100644 --- a/frontend/composables/api/products/useGetProducts.ts +++ b/frontend/composables/api/products/useGetProducts.ts @@ -5,44 +5,53 @@ import { API_ENDPOINTS, QUERY_KEYS } from "~/constants"; // types -export type GetProductsResponse = Product[]; +export type GetProductsResponse = ApiPaginated; export type GetProductsFilters = { - search: string | undefined; - sort: string | undefined; - categories: string[] | undefined; - price_range: number[] | undefined; - has_discount: boolean | undefined; - in_stock: boolean | undefined; + search?: string | undefined; + sort?: string | undefined; + category?: string | undefined; + price_gte: number; + price_lte: number; + has_discount?: boolean | undefined; + in_stock?: boolean | undefined; + page: number; }; // composable -const useGetProducts = ( - filters: GetProductsFilters, - page: ComputedRef -) => { +const useGetProducts = (params?: GetProductsFilters) => { // state const { $axios: axios } = useNuxtApp(); + const { + search, + sort, + in_stock, + has_discount, + category, + price_gte, + price_lte, + page, + } = toRefs(params as GetProductsFilters); + // methods - const handleGetProducts = async ({ - filters, - page, - }: { - filters: GetProductsFilters; - page: number; - }) => { + const handleGetProducts = async (params?: GetProductsFilters) => { const { data } = await axios.get( `${API_ENDPOINTS.products.get_all}`, { params: { - ...filters, - page, - offest: page * 10 - 10, - limit: 10, + sort: params?.sort, + in_stock: params?.in_stock, + search: params?.search, + has_discount: params?.has_discount, + category: params?.category, + price_gte: params?.price_gte, + price_lte: params?.price_lte, + offest: params?.page! * 9 - 9, + limit: 9, }, } ); @@ -52,8 +61,18 @@ const useGetProducts = ( return useQuery({ staleTime: 60 * 1000, - queryKey: [QUERY_KEYS.products, filters, page], - queryFn: () => handleGetProducts({ filters, page: page.value }), + queryKey: [ + QUERY_KEYS.products, + search, + sort, + in_stock, + has_discount, + category, + price_gte, + price_lte, + page, + ], + queryFn: () => handleGetProducts(params), }); };