2023-04-20 11:27:18 -05:00
|
|
|
import Grid from 'components/grid';
|
|
|
|
import ProductGridItems from 'components/layout/product-grid-items';
|
2023-04-17 23:00:47 -04:00
|
|
|
import { defaultSort, sorting } from 'lib/constants';
|
|
|
|
import { getProducts } from 'lib/shopify';
|
|
|
|
|
|
|
|
export const metadata = {
|
|
|
|
title: 'Search',
|
|
|
|
description: 'Search for products in the store.'
|
|
|
|
};
|
|
|
|
|
2024-10-15 22:07:55 -05:00
|
|
|
export default async function SearchPage(props: {
|
|
|
|
searchParams?: Promise<{ [key: string]: string | string[] | undefined }>;
|
2023-04-17 23:00:47 -04:00
|
|
|
}) {
|
2024-10-15 22:07:55 -05:00
|
|
|
const searchParams = await props.searchParams;
|
2023-04-17 23:00:47 -04:00
|
|
|
const { sort, q: searchValue } = searchParams as { [key: string]: string };
|
|
|
|
const { sortKey, reverse } = sorting.find((item) => item.slug === sort) || defaultSort;
|
|
|
|
|
|
|
|
const products = await getProducts({ sortKey, reverse, query: searchValue });
|
|
|
|
const resultsText = products.length > 1 ? 'results' : 'result';
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-04-20 11:27:18 -05:00
|
|
|
{searchValue ? (
|
2023-08-02 09:04:44 -05:00
|
|
|
<p className="mb-4">
|
2023-04-20 11:27:18 -05:00
|
|
|
{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 ? (
|
2023-07-24 21:40:29 -05:00
|
|
|
<Grid className="grid-cols-1 sm:grid-cols-2 lg:grid-cols-3">
|
2023-04-20 11:27:18 -05:00
|
|
|
<ProductGridItems products={products} />
|
|
|
|
</Grid>
|
|
|
|
) : null}
|
2023-04-17 23:00:47 -04:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|