mirror of
https://github.com/vercel/commerce.git
synced 2025-04-28 05:47:50 +00:00
44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import Grid from "components/grid";
|
|
import ProductGridItems from "components/layout/product-grid-items";
|
|
import { getProducts } from "lib/store/products";
|
|
import { Metadata } from "next";
|
|
|
|
export const metadata: Metadata = {
|
|
title: "Search",
|
|
description: "Search for products in the store.",
|
|
};
|
|
|
|
const defaultSort = {
|
|
sortKey: "RELEVANCE",
|
|
reverse: false,
|
|
};
|
|
|
|
export default async function SearchPage(props: {
|
|
searchParams?: Promise<{ [key: string]: string | string[] | undefined }>;
|
|
}) {
|
|
const searchParams = await props.searchParams;
|
|
const { sort, q: searchValue } = searchParams as { [key: string]: string };
|
|
const { sortKey, reverse } = defaultSort;
|
|
|
|
const products = await getProducts({ sortKey, reverse, query: searchValue });
|
|
const resultsText = products.length > 1 ? "results" : "result";
|
|
|
|
return (
|
|
<>
|
|
{searchValue ? (
|
|
<p className="mb-4">
|
|
{products.length === 0
|
|
? "There are no products that match "
|
|
: `Showing ${products.length} ${resultsText} for `}
|
|
<span className="font-bold">"{searchValue}"</span>
|
|
</p>
|
|
) : null}
|
|
{products.length > 0 ? (
|
|
<Grid className="grid-cols-1 sm:grid-cols-2 lg:grid-cols-3">
|
|
<ProductGridItems products={products} />
|
|
</Grid>
|
|
) : null}
|
|
</>
|
|
);
|
|
}
|