4
0
forked from crowetic/commerce
commerce/pages/search.tsx

92 lines
2.8 KiB
TypeScript
Raw Normal View History

2020-10-12 10:25:36 -03:00
import { GetStaticPropsContext, InferGetStaticPropsType } from 'next'
2020-10-13 03:49:24 -05:00
import getSiteInfo from '@lib/bigcommerce/api/operations/get-site-info'
import useSearch from '@lib/bigcommerce/products/use-search'
2020-10-12 10:25:36 -03:00
import { Layout } from '@components/core'
2020-10-13 09:41:37 -03:00
import { Container, Grid } from '@components/ui'
2020-10-12 10:25:36 -03:00
import { ProductCard } from '@components/product'
2020-10-12 11:10:20 -03:00
import { useRouter } from 'next/router'
2020-10-12 10:25:36 -03:00
export async function getStaticProps({ preview }: GetStaticPropsContext) {
const { categories, brands } = await getSiteInfo()
return {
2020-10-13 07:07:35 -05:00
props: { categories, brands },
2020-10-12 10:25:36 -03:00
}
}
export default function Home({
categories,
brands,
}: InferGetStaticPropsType<typeof getStaticProps>) {
2020-10-12 11:10:20 -03:00
const router = useRouter()
2020-10-13 07:07:35 -05:00
const { q } = router.query
const { data } = useSearch({
search: typeof q === 'string' ? q : '',
})
2020-10-13 03:49:24 -05:00
2020-10-12 10:25:36 -03:00
return (
2020-10-13 09:41:37 -03:00
<Container>
<div className="grid grid-cols-12 gap-8 mt-3 mb-20">
<div className="col-span-2">
<ul className="mb-10">
<li className="py-1 text-primary font-bold tracking-wide">
All Categories
2020-10-12 10:25:36 -03:00
</li>
2020-10-13 09:41:37 -03:00
{categories.map((cat) => (
<li key={cat.path} className="py-1 text-secondary">
<a href="#">{cat.name}</a>
</li>
))}
</ul>
<ul>
<li className="py-1 text-primary font-bold tracking-wide">
All Designers
2020-10-12 10:25:36 -03:00
</li>
2020-10-13 09:41:37 -03:00
{brands.flatMap(({ node }) => (
<li key={node.path} className="py-1 text-secondary">
<a href="#">{node.name}</a>
</li>
))}
</ul>
</div>
<div className="col-span-8">
{data ? (
<>
{q && (
<div className="mb-12">
{data.found ? (
<>Showing {data.products.length} results for</>
) : (
<>There are no products that match</>
)}{' '}
"<strong>{q}</strong>"
</div>
)}
<Grid
items={data.products}
layout="normal"
wrapper={ProductCard}
/>
</>
) : (
<div>Searching...</div>
)}
</div>
<div className="col-span-2">
<ul>
<li className="py-1 text-primary font-bold tracking-wide">
Relevance
</li>
<li className="py-1 text-secondary">Latest arrivals</li>
<li className="py-1 text-secondary">Trending</li>
<li className="py-1 text-secondary">Price: Low to high</li>
<li className="py-1 text-secondary">Price: High to low</li>
</ul>
</div>
2020-10-12 10:25:36 -03:00
</div>
2020-10-13 09:41:37 -03:00
</Container>
2020-10-12 10:25:36 -03:00
)
}
Home.Layout = Layout