mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 23:46:58 +00:00
Update page.tsx
Refactor component to conditionally render different JSX and simplify repetitive code.
This commit is contained in:
parent
3a18f9a098
commit
e0cdd867f4
@ -19,23 +19,30 @@ export default async function SearchPage({
|
||||
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';
|
||||
const totalProducts = products.length;
|
||||
const isProductEmpty = totalProduct === 0;
|
||||
const resultsText = totalProducts > 1 ? 'results' : 'result';
|
||||
|
||||
if(searchValue && isProductEmpty){
|
||||
return (
|
||||
<p className="mb-4">
|
||||
There are no products that match
|
||||
</p>
|
||||
)
|
||||
}
|
||||
|
||||
if(searchValue && !isProductEmpty){
|
||||
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>
|
||||
{`Showing ${totalProducts} ${resultsText} for `}
|
||||
</p>
|
||||
) : null}
|
||||
{products.length > 0 ? (
|
||||
<Grid className="grid-cols-1 sm:grid-cols-2 lg:grid-cols-3">
|
||||
<ProductGridItems products={products} />
|
||||
</Grid>
|
||||
) : null}
|
||||
</>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user