|
|
@@ -10,8 +10,11 @@ import NewSortOrder from "@components/theme/filters/NewSortOrder";
|
|
|
import { SortByFields } from "@utils/constants";
|
|
|
import { isArray } from "@/utils/type-guards";
|
|
|
import { useRouter, usePathname } from "next/navigation";
|
|
|
+import { useMutation, useApolloClient } from "@apollo/client/react";
|
|
|
+import { CategoryProductsResult } from "@components/catalog/type";
|
|
|
+import {CATEGORY_PRODUCTS} from "@/graphql";
|
|
|
interface QueryState {
|
|
|
- search: string;
|
|
|
+ // search: string;
|
|
|
|
|
|
sort: string;
|
|
|
|
|
|
@@ -20,6 +23,7 @@ interface QueryState {
|
|
|
cursor?: string;
|
|
|
}
|
|
|
interface ProductListingProps {
|
|
|
+ slug:string;
|
|
|
categoryId: string;
|
|
|
|
|
|
filterAttributes: any[];
|
|
|
@@ -34,6 +38,7 @@ interface ProductListingProps {
|
|
|
}
|
|
|
|
|
|
export default function ProductListing({
|
|
|
+ slug,
|
|
|
categoryId,
|
|
|
filterAttributes,
|
|
|
initialProducts,
|
|
|
@@ -52,38 +57,63 @@ export default function ProductListing({
|
|
|
|
|
|
const [_total, setTotal] = useState(initialTotal);
|
|
|
const firstRender = useRef(true);
|
|
|
+ const apolloClient = useApolloClient();
|
|
|
+
|
|
|
const fetchProducts = async (currentQuery: QueryState) => {
|
|
|
try {
|
|
|
- const res = await fetch("/api/products", {
|
|
|
- method: "POST",
|
|
|
- headers: {
|
|
|
- "Content-Type": "application/json",
|
|
|
- },
|
|
|
- body: JSON.stringify({
|
|
|
- categoryId,
|
|
|
-
|
|
|
- search: currentQuery.search,
|
|
|
-
|
|
|
- sort: currentQuery.sort,
|
|
|
-
|
|
|
- filters: currentQuery.filters,
|
|
|
-
|
|
|
- first: 12,
|
|
|
+ const res = await apolloClient.query<CategoryProductsResult>({
|
|
|
+ query: CATEGORY_PRODUCTS,
|
|
|
+ variables:
|
|
|
+ // {
|
|
|
+
|
|
|
+ // filter: JSON.stringify({
|
|
|
+ // }),
|
|
|
+ // first: 15,
|
|
|
+ // after: null,
|
|
|
+ // }
|
|
|
+ {
|
|
|
+ slug: slug ||"ready-to-go-wig",
|
|
|
+
|
|
|
+ // sort: currentQuery.sort,
|
|
|
+
|
|
|
+ filter:JSON.stringify( currentQuery.filters),
|
|
|
+
|
|
|
+ first: 15,
|
|
|
|
|
|
after: null,
|
|
|
- }),
|
|
|
+ },
|
|
|
});
|
|
|
+ console.log("res----------------------------------:",res);
|
|
|
+
|
|
|
+ // const res = await fetch("/api/products", {
|
|
|
+ // method: "POST",
|
|
|
+ // headers: {
|
|
|
+ // "Content-Type": "application/json",
|
|
|
+ // },
|
|
|
+ // body: JSON.stringify({
|
|
|
+ // categoryId,
|
|
|
+
|
|
|
+ // sort: currentQuery.sort,
|
|
|
+
|
|
|
+ // filters: currentQuery.filters,
|
|
|
+
|
|
|
+ // first: 12,
|
|
|
+
|
|
|
+ // after: null,
|
|
|
+ // }),
|
|
|
+ // });
|
|
|
+ // search: currentQuery.search,
|
|
|
console.log("筛选排序触发调接口");
|
|
|
- const result = await res.json();
|
|
|
+ // const result = await res.json();
|
|
|
|
|
|
const newProducts =
|
|
|
- result.data?.products?.edges?.map((e: any) => e.node) || [];
|
|
|
+ res.data?.categoryProducts?.products.map((e: any) => e.node) || [];
|
|
|
|
|
|
setProducts(newProducts);
|
|
|
|
|
|
- setPageInfo(result.data?.products?.pageInfo);
|
|
|
+ setPageInfo(res.data?.categoryProducts?.pageInfo);
|
|
|
|
|
|
- setTotal(result.data?.products?.totalCount || 0);
|
|
|
+ setTotal(res.data?.categoryProducts?.totalCount || 0);
|
|
|
} catch (error) {
|
|
|
console.error("fetch products error", error);
|
|
|
}
|
|
|
@@ -95,9 +125,11 @@ export default function ProductListing({
|
|
|
|
|
|
return;
|
|
|
}
|
|
|
+ console.log("query-----------------------------22222:",query);
|
|
|
fetchProducts(query);
|
|
|
- }, [query.filters, query.sort, query.search]);
|
|
|
-
|
|
|
+
|
|
|
+ }, [query.filters, query.sort]);
|
|
|
+ //query.search
|
|
|
const router = useRouter();
|
|
|
const pathname = usePathname();
|
|
|
console.log("query-----:", query);
|
|
|
@@ -126,36 +158,56 @@ export default function ProductListing({
|
|
|
};
|
|
|
// view more 逻辑
|
|
|
const loadMore = async () => {
|
|
|
- const res = await fetch("/api/products", {
|
|
|
- method: "POST",
|
|
|
-
|
|
|
- headers: {
|
|
|
- "Content-Type": "application/json",
|
|
|
- },
|
|
|
-
|
|
|
- body: JSON.stringify({
|
|
|
- categoryId,
|
|
|
-
|
|
|
- search: query.search,
|
|
|
-
|
|
|
- sort: query.sort,
|
|
|
-
|
|
|
- filters: query.filters,
|
|
|
-
|
|
|
- first: 15,
|
|
|
-
|
|
|
- after: pageInfo.endCursor,
|
|
|
- }),
|
|
|
- });
|
|
|
+ // const res = await fetch("/api/products", {
|
|
|
+ // method: "POST",
|
|
|
+
|
|
|
+ // headers: {
|
|
|
+ // "Content-Type": "application/json",
|
|
|
+ // },
|
|
|
+
|
|
|
+ // body: JSON.stringify({
|
|
|
+ // categoryId,
|
|
|
+ // sort: query.sort,
|
|
|
+
|
|
|
+ // filters: query.filters,
|
|
|
+
|
|
|
+ // first: 15,
|
|
|
+
|
|
|
+ // after: pageInfo.endCursor,
|
|
|
+ // }),
|
|
|
+ // });
|
|
|
+ const res = await apolloClient.query<CategoryProductsResult>({
|
|
|
+ query: CATEGORY_PRODUCTS,
|
|
|
+ variables:
|
|
|
+ // {
|
|
|
+
|
|
|
+ // filter: JSON.stringify({
|
|
|
+ // }),
|
|
|
+ // first: 15,
|
|
|
+ // after: null,
|
|
|
+ // }
|
|
|
+ {
|
|
|
+ slug: slug ||"ready-to-go-wig",
|
|
|
+
|
|
|
+ // sort: query.sort,
|
|
|
+
|
|
|
+ filter:JSON.stringify(query.filters),
|
|
|
+
|
|
|
+ first: 15,
|
|
|
+
|
|
|
+ after: pageInfo.endCursor,
|
|
|
+ },
|
|
|
+ });
|
|
|
+ //search: query.search,
|
|
|
console.log("触发view more调接口");
|
|
|
- const result = await res.json();
|
|
|
+ // const result = await res.json();
|
|
|
|
|
|
const moreProducts =
|
|
|
- result.data?.products?.edges?.map((e: any) => e.node) || [];
|
|
|
+ res.data?.categoryProducts?.products.map((e: any) => e.node) || [];
|
|
|
|
|
|
setProducts((prev) => [...prev, ...moreProducts]);
|
|
|
|
|
|
- setPageInfo(result.data.products.pageInfo);
|
|
|
+ setPageInfo(res.data?.categoryProducts?.pageInfo);
|
|
|
};
|
|
|
return (
|
|
|
<>
|