changed get products
This commit is contained in:
@@ -5,44 +5,53 @@ import { API_ENDPOINTS, QUERY_KEYS } from "~/constants";
|
||||
|
||||
// types
|
||||
|
||||
export type GetProductsResponse = Product[];
|
||||
export type GetProductsResponse = ApiPaginated<Product>;
|
||||
|
||||
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<number>
|
||||
) => {
|
||||
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<GetProductsResponse>(
|
||||
`${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),
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user