|
@@ -1,31 +1,24 @@
|
|
|
import dynamicImport from "next/dynamic";
|
|
import dynamicImport from "next/dynamic";
|
|
|
import Grid from "@/components/theme/ui/grid/Grid";
|
|
import Grid from "@/components/theme/ui/grid/Grid";
|
|
|
-// import NotFound from "@/components/theme/search/not-found";
|
|
|
|
|
import { isArray } from "@/utils/type-guards";
|
|
import { isArray } from "@/utils/type-guards";
|
|
|
import { serverGraphqlFetch } from "@utils/bagisto/index";
|
|
import { serverGraphqlFetch } from "@utils/bagisto/index";
|
|
|
import {
|
|
import {
|
|
|
SEARCH_PRODUCTS,
|
|
SEARCH_PRODUCTS,
|
|
|
} from "@/graphql";
|
|
} from "@/graphql";
|
|
|
-
|
|
|
|
|
-import { generateMetadataForPage, buildProductFilters } from "@/utils/helper";
|
|
|
|
|
import SortOrder from "@/components/theme/filters/SortOrder";
|
|
import SortOrder from "@/components/theme/filters/SortOrder";
|
|
|
import { SortByFields } from "@/utils/constants";
|
|
import { SortByFields } from "@/utils/constants";
|
|
|
-// import MobileFilter from "@/components/theme/filters/MobileFilter";
|
|
|
|
|
-// import FilterList from "@/components/theme/filters/FilterList";
|
|
|
|
|
import type {
|
|
import type {
|
|
|
SearchProductsResponse,
|
|
SearchProductsResponse,
|
|
|
SearchProductsVariables,
|
|
SearchProductsVariables,
|
|
|
} from "@/components/catalog/type";
|
|
} from "@/components/catalog/type";
|
|
|
import { MobileSearchBar } from "@components/layout/navbar/MobileSearch";
|
|
import { MobileSearchBar } from "@components/layout/navbar/MobileSearch";
|
|
|
import HotSearch from "./_components/HotSearch";
|
|
import HotSearch from "./_components/HotSearch";
|
|
|
-const Pagination = dynamicImport(
|
|
|
|
|
- () => import("@/components/catalog/Pagination"),
|
|
|
|
|
-);
|
|
|
|
|
-// const ProductGridItems = dynamicImport(
|
|
|
|
|
-// () => import("@/components/catalog/product/ProductGridItems"),
|
|
|
|
|
-// );
|
|
|
|
|
import NewProductGridItems from "@/components/catalog/product/NewProductGridItems";
|
|
import NewProductGridItems from "@/components/catalog/product/NewProductGridItems";
|
|
|
-export const dynamicParams = true;
|
|
|
|
|
|
|
+const SearchPagination = dynamicImport(
|
|
|
|
|
+ () => import("./_components/SearchPagination"),
|
|
|
|
|
+);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
|
|
|
// SEO+分享
|
|
// SEO+分享
|
|
|
export async function generateMetadata({
|
|
export async function generateMetadata({
|
|
@@ -34,15 +27,14 @@ export async function generateMetadata({
|
|
|
searchParams?: Promise<{ q: string }>;
|
|
searchParams?: Promise<{ q: string }>;
|
|
|
}) {
|
|
}) {
|
|
|
const params = await searchParams;
|
|
const params = await searchParams;
|
|
|
- const searchQuery = params?.q as string | undefined;
|
|
|
|
|
|
|
+ const searchQuery = params?.q;
|
|
|
|
|
|
|
|
- return generateMetadataForPage("search", {
|
|
|
|
|
|
|
+ return {
|
|
|
title: searchQuery ? `Search: ${searchQuery}` : "Search Products",
|
|
title: searchQuery ? `Search: ${searchQuery}` : "Search Products",
|
|
|
description: searchQuery
|
|
description: searchQuery
|
|
|
? `Search results for "${searchQuery}"`
|
|
? `Search results for "${searchQuery}"`
|
|
|
: "Search for products in our store",
|
|
: "Search for products in our store",
|
|
|
- image: "/search-og.jpg",
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ };
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export default async function SearchPage({
|
|
export default async function SearchPage({
|
|
@@ -52,71 +44,20 @@ export default async function SearchPage({
|
|
|
q: string;
|
|
q: string;
|
|
|
page: string;
|
|
page: string;
|
|
|
sort: string;
|
|
sort: string;
|
|
|
- cursor: string;
|
|
|
|
|
- before: string;
|
|
|
|
|
}>;
|
|
}>;
|
|
|
}) {
|
|
}) {
|
|
|
const params = await searchParams;
|
|
const params = await searchParams;
|
|
|
const {
|
|
const {
|
|
|
q: searchValue,
|
|
q: searchValue,
|
|
|
page,
|
|
page,
|
|
|
- cursor,
|
|
|
|
|
- before,
|
|
|
|
|
} = (params || {});
|
|
} = (params || {});
|
|
|
const itemsPerPage = 8;
|
|
const itemsPerPage = 8;
|
|
|
- const currentPage = page ? parseInt(page) - 1 : 0;
|
|
|
|
|
|
|
+ const currentPage = page ? parseInt(page): 1;
|
|
|
const sortValue = params?.sort || "name-asc";
|
|
const sortValue = params?.sort || "name-asc";
|
|
|
- const selectedSort =
|
|
|
|
|
- SortByFields.find((s) => s.key === sortValue) || SortByFields[0];
|
|
|
|
|
- const afterCursor: string | undefined = cursor;
|
|
|
|
|
- const beforeCursor: string | undefined = before;
|
|
|
|
|
-
|
|
|
|
|
- const { filterInput, isFilterApplied } = buildProductFilters(params || {});
|
|
|
|
|
-
|
|
|
|
|
- let dataPromise;
|
|
|
|
|
- if (isFilterApplied) {
|
|
|
|
|
- dataPromise = await serverGraphqlFetch<
|
|
|
|
|
- SearchProductsResponse,
|
|
|
|
|
- SearchProductsVariables
|
|
|
|
|
- >({
|
|
|
|
|
- query: SEARCH_PRODUCTS,
|
|
|
|
|
- variables: {
|
|
|
|
|
- suggest: true,
|
|
|
|
|
- query: searchValue || '',
|
|
|
|
|
- filter: filterInput,
|
|
|
|
|
- first: 8,
|
|
|
|
|
- ...(beforeCursor
|
|
|
|
|
- ? { last: itemsPerPage, before: beforeCursor }
|
|
|
|
|
- : { first: itemsPerPage, after: afterCursor }),
|
|
|
|
|
- sortKey: selectedSort.sortKey,
|
|
|
|
|
- reverse: selectedSort.reverse,
|
|
|
|
|
- },
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ const selectedSort = SortByFields.find((s) => s.key === sortValue) || SortByFields[0];
|
|
|
|
|
|
|
|
- } else {
|
|
|
|
|
- dataPromise = (async () => {
|
|
|
|
|
- let currentAfterCursor: string | undefined = afterCursor;
|
|
|
|
|
- // console.log("currentAfterCursoraaaaa00:",currentAfterCursor);
|
|
|
|
|
- if (currentPage > 0 && !afterCursor) {
|
|
|
|
|
- // 兜底处理 如果用户直接通过url访问且没有在url上带cursor= 会调接口获取cursor值
|
|
|
|
|
- // 正常走底部分页按钮不会触发该接口调用;
|
|
|
|
|
- const { data: cursorData } = await serverGraphqlFetch<
|
|
|
|
|
- SearchProductsResponse,
|
|
|
|
|
- SearchProductsVariables
|
|
|
|
|
- >({
|
|
|
|
|
- query: SEARCH_PRODUCTS,
|
|
|
|
|
- variables: {
|
|
|
|
|
- suggest: true,
|
|
|
|
|
- query: searchValue || '',
|
|
|
|
|
- first: currentPage * itemsPerPage,
|
|
|
|
|
- sortKey: selectedSort.sortKey,
|
|
|
|
|
- reverse: selectedSort.reverse,
|
|
|
|
|
- },
|
|
|
|
|
- });
|
|
|
|
|
|
|
|
|
|
- currentAfterCursor = cursorData?.search?.pageInfo?.endCursor;
|
|
|
|
|
- // console.log("currentAfterCursoraaaaa:",currentAfterCursor);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const dataPromise = (async () => {
|
|
|
return serverGraphqlFetch<
|
|
return serverGraphqlFetch<
|
|
|
SearchProductsResponse,
|
|
SearchProductsResponse,
|
|
|
SearchProductsVariables
|
|
SearchProductsVariables
|
|
@@ -125,28 +66,23 @@ export default async function SearchPage({
|
|
|
variables: {
|
|
variables: {
|
|
|
suggest: true,
|
|
suggest: true,
|
|
|
query: searchValue || '',
|
|
query: searchValue || '',
|
|
|
- ...(beforeCursor
|
|
|
|
|
- ? { last: itemsPerPage, before: beforeCursor }
|
|
|
|
|
- : { first: itemsPerPage, after: currentAfterCursor }),
|
|
|
|
|
|
|
+ first: itemsPerPage,
|
|
|
|
|
+ page: currentPage,
|
|
|
sortKey: selectedSort.sortKey,
|
|
sortKey: selectedSort.sortKey,
|
|
|
reverse: selectedSort.reverse,
|
|
reverse: selectedSort.reverse,
|
|
|
},
|
|
},
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- })();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ })();
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
|
|
|
- // const [{ data }, filterAttributes] = await Promise.all([
|
|
|
|
|
- // dataPromise,
|
|
|
|
|
- // getFilterAttributes(),
|
|
|
|
|
- // ]);
|
|
|
|
|
const { data } = await dataPromise;
|
|
const { data } = await dataPromise;
|
|
|
- console.log("sssssssssssssssssssssssssss:", data);
|
|
|
|
|
|
|
|
|
|
const products = data?.search?.edges?.map((e) => e.node) || [];
|
|
const products = data?.search?.edges?.map((e) => e.node) || [];
|
|
|
- const pageInfo = data?.search?.pageInfo;
|
|
|
|
|
|
|
+// const pageInfo = data?.search?.pageInfo;
|
|
|
const totalCount = data?.search?.totalCount || 0;
|
|
const totalCount = data?.search?.totalCount || 0;
|
|
|
- console.log("productssssssssssssssssssss---------------------",products);
|
|
|
|
|
|
|
+
|
|
|
|
|
|
|
|
return (
|
|
return (
|
|
|
<>
|
|
<>
|
|
@@ -154,7 +90,6 @@ export default async function SearchPage({
|
|
|
<HotSearch />
|
|
<HotSearch />
|
|
|
{isArray(products) ? (
|
|
{isArray(products) ? (
|
|
|
<div className="flex items-center justify-between gap-4 pt-0 pb-6 md:hidden mx-auto w-full max-w-screen-2xl px-4 xss:px-7.5">
|
|
<div className="flex items-center justify-between gap-4 pt-0 pb-6 md:hidden mx-auto w-full max-w-screen-2xl px-4 xss:px-7.5">
|
|
|
- {/* <MobileFilter filterAttributes={filterAttributes} /> */}
|
|
|
|
|
<div>{totalCount||0} items</div>
|
|
<div>{totalCount||0} items</div>
|
|
|
<SortOrder sortOrders={SortByFields} title="Sort by" />
|
|
<SortOrder sortOrders={SortByFields} title="Sort by" />
|
|
|
</div>
|
|
</div>
|
|
@@ -173,35 +108,15 @@ export default async function SearchPage({
|
|
|
</Grid>
|
|
</Grid>
|
|
|
) : null}
|
|
) : null}
|
|
|
|
|
|
|
|
- {!isFilterApplied && isArray(products) && totalCount > itemsPerPage && (
|
|
|
|
|
- <nav
|
|
|
|
|
- aria-label="Collection pagination"
|
|
|
|
|
- className="my-10 block items-center sm:flex"
|
|
|
|
|
- >
|
|
|
|
|
- <Pagination
|
|
|
|
|
- itemsPerPage={itemsPerPage}
|
|
|
|
|
- itemsTotal={totalCount || 0}
|
|
|
|
|
- currentPage={currentPage}
|
|
|
|
|
- nextCursor={pageInfo?.endCursor}
|
|
|
|
|
- prevCursor={pageInfo?.startCursor}
|
|
|
|
|
- />
|
|
|
|
|
- </nav>
|
|
|
|
|
- )}
|
|
|
|
|
-
|
|
|
|
|
- {isFilterApplied && isArray(products) && pageInfo?.hasNextPage && (
|
|
|
|
|
- <nav
|
|
|
|
|
- aria-label="Filtered pagination"
|
|
|
|
|
- className="my-10 block items-center sm:flex"
|
|
|
|
|
- >
|
|
|
|
|
- <Pagination
|
|
|
|
|
- itemsPerPage={itemsPerPage}
|
|
|
|
|
- itemsTotal={totalCount || 0}
|
|
|
|
|
- currentPage={currentPage}
|
|
|
|
|
- nextCursor={pageInfo?.endCursor}
|
|
|
|
|
- prevCursor={pageInfo?.startCursor}
|
|
|
|
|
- />
|
|
|
|
|
- </nav>
|
|
|
|
|
- )}
|
|
|
|
|
|
|
+ {isArray(products) && totalCount > itemsPerPage && (
|
|
|
|
|
+ <nav className="mt-4 mb-8">
|
|
|
|
|
+ <SearchPagination
|
|
|
|
|
+ itemsPerPage={itemsPerPage}
|
|
|
|
|
+ itemsTotal={totalCount || 0}
|
|
|
|
|
+ currentPage={currentPage}
|
|
|
|
|
+ />
|
|
|
|
|
+ </nav>
|
|
|
|
|
+ )}
|
|
|
</>
|
|
</>
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|