2020-10-13 18:52:02 -05:00
|
|
|
import { useEffect, useState } from 'react'
|
2020-10-12 10:25:36 -03:00
|
|
|
import { GetStaticPropsContext, InferGetStaticPropsType } from 'next'
|
2020-10-13 18:52:02 -05:00
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import Link from 'next/link'
|
2020-10-13 19:31:00 -05:00
|
|
|
import cn from 'classnames'
|
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'
|
|
|
|
|
|
|
|
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 22:20:23 -05:00
|
|
|
const { asPath } = router
|
|
|
|
const { q, sort } = router.query
|
|
|
|
const query = filterQuery({ q, sort })
|
2020-10-13 22:43:11 -05:00
|
|
|
const pathname = asPath.split('?')[0]
|
|
|
|
|
2020-10-13 22:20:23 -05:00
|
|
|
const { category, brand } = useSearchMeta(asPath)
|
2020-10-13 20:16:43 -05:00
|
|
|
const activeCategory = categories.find(
|
|
|
|
(cat) => getSlug(cat.path) === category
|
|
|
|
)
|
|
|
|
const activeBrand = brands.find(
|
|
|
|
(b) => getSlug(b.node.path) === `brands/${brand}`
|
|
|
|
)?.node
|
2020-10-13 22:43:11 -05:00
|
|
|
|
2020-10-13 07:07:35 -05:00
|
|
|
const { data } = useSearch({
|
|
|
|
search: typeof q === 'string' ? q : '',
|
2020-10-13 20:16:43 -05:00
|
|
|
categoryId: activeCategory?.entityId,
|
|
|
|
brandId: activeBrand?.entityId,
|
2020-10-13 07:07:35 -05:00
|
|
|
})
|
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">
|
2020-10-13 22:20:23 -05:00
|
|
|
<Link href={{ pathname: getCategoryPath('', brand), query }}>
|
2020-10-13 20:43:56 -05:00
|
|
|
<a>All Categories</a>
|
|
|
|
</Link>
|
2020-10-12 10:25:36 -03:00
|
|
|
</li>
|
2020-10-13 09:41:37 -03:00
|
|
|
{categories.map((cat) => (
|
2020-10-13 19:31:00 -05:00
|
|
|
<li
|
|
|
|
key={cat.path}
|
|
|
|
className={cn('py-1 text-default', {
|
2020-10-13 20:16:43 -05:00
|
|
|
underline: activeCategory?.entityId === cat.entityId,
|
2020-10-13 19:31:00 -05:00
|
|
|
})}
|
|
|
|
>
|
2020-10-13 22:20:23 -05:00
|
|
|
<Link
|
|
|
|
href={{
|
|
|
|
pathname: getCategoryPath(getSlug(cat.path), brand),
|
|
|
|
query,
|
|
|
|
}}
|
|
|
|
>
|
2020-10-13 18:52:02 -05:00
|
|
|
<a>{cat.name}</a>
|
|
|
|
</Link>
|
2020-10-13 09:41:37 -03:00
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
<ul>
|
|
|
|
<li className="py-1 text-primary font-bold tracking-wide">
|
2020-10-13 22:20:23 -05:00
|
|
|
<Link href={{ pathname: getDesignerPath('', category), query }}>
|
2020-10-13 20:43:56 -05:00
|
|
|
<a>All Designers</a>
|
|
|
|
</Link>
|
2020-10-12 10:25:36 -03:00
|
|
|
</li>
|
2020-10-13 09:41:37 -03:00
|
|
|
{brands.flatMap(({ node }) => (
|
2020-10-13 19:31:00 -05:00
|
|
|
<li
|
|
|
|
key={node.path}
|
|
|
|
className={cn('py-1 text-default', {
|
2020-10-13 20:16:43 -05:00
|
|
|
underline: activeBrand?.entityId === node.entityId,
|
2020-10-13 19:31:00 -05:00
|
|
|
})}
|
|
|
|
>
|
2020-10-13 22:20:23 -05:00
|
|
|
<Link
|
|
|
|
href={{
|
|
|
|
pathname: getDesignerPath(getSlug(node.path), category),
|
|
|
|
query,
|
|
|
|
}}
|
|
|
|
>
|
2020-10-13 18:52:02 -05:00
|
|
|
<a>{node.name}</a>
|
|
|
|
</Link>
|
2020-10-13 09:41:37 -03:00
|
|
|
</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>
|
2020-10-13 22:20:23 -05:00
|
|
|
<li className="py-1 text-primary font-bold tracking-wide">Sort</li>
|
|
|
|
<li className="py-1 text-default">
|
|
|
|
<Link
|
|
|
|
href={{
|
2020-10-13 22:43:11 -05:00
|
|
|
pathname,
|
2020-10-13 22:20:23 -05:00
|
|
|
query: filterQuery({ q }),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<a>Relevance</a>
|
|
|
|
</Link>
|
2020-10-13 09:41:37 -03:00
|
|
|
</li>
|
2020-10-13 22:43:11 -05:00
|
|
|
<li className="py-1 text-default">
|
|
|
|
<Link
|
|
|
|
href={{
|
|
|
|
pathname,
|
|
|
|
query: filterQuery({ q, sort: 'latest-desc' }),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<a>Latest arrivals</a>
|
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
<li className="py-1 text-default">
|
|
|
|
<Link
|
|
|
|
href={{
|
|
|
|
pathname,
|
|
|
|
query: filterQuery({ q, sort: 'trending-desc' }),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<a>Trending</a>
|
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
<li className="py-1 text-default">
|
|
|
|
<Link
|
|
|
|
href={{
|
|
|
|
pathname,
|
|
|
|
query: filterQuery({ q, sort: 'price-asc' }),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<a>Price: Low to high</a>
|
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
<li className="py-1 text-default">
|
|
|
|
<Link
|
|
|
|
href={{
|
|
|
|
pathname,
|
|
|
|
query: filterQuery({ q, sort: 'price-desc' }),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<a>Price: High to low</a>
|
|
|
|
</Link>
|
|
|
|
</li>
|
2020-10-13 09:41:37 -03:00
|
|
|
</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
|
2020-10-13 18:52:02 -05:00
|
|
|
|
|
|
|
function useSearchMeta(asPath: string) {
|
|
|
|
const [category, setCategory] = useState<string | undefined>()
|
|
|
|
const [brand, setBrand] = useState<string | undefined>()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const parts = asPath.split('/')
|
|
|
|
|
|
|
|
let c = parts[2]
|
|
|
|
let b = parts[3]
|
|
|
|
|
|
|
|
if (c === 'designers') {
|
|
|
|
c = parts[4]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c !== category) setCategory(c)
|
|
|
|
if (b !== brand) setBrand(b)
|
|
|
|
}, [asPath])
|
|
|
|
|
|
|
|
return { category, brand }
|
|
|
|
}
|
|
|
|
|
2020-10-13 22:20:23 -05:00
|
|
|
// Removes empty query parameters from the query object
|
|
|
|
const filterQuery = (query: any) =>
|
|
|
|
Object.keys(query).reduce<any>((obj, key) => {
|
|
|
|
if (query[key]?.length) {
|
|
|
|
obj[key] = query[key]
|
|
|
|
}
|
|
|
|
return obj
|
|
|
|
}, {})
|
|
|
|
|
2020-10-13 19:31:00 -05:00
|
|
|
// Remove trailing and leading slash
|
|
|
|
const getSlug = (path: string) => path.replace(/^\/|\/$/g, '')
|
2020-10-13 18:52:02 -05:00
|
|
|
|
2020-10-13 20:43:56 -05:00
|
|
|
const getCategoryPath = (slug: string, brand?: string) =>
|
|
|
|
`/search${brand ? `/designers/${brand}` : ''}${slug ? `/${slug}` : ''}`
|
2020-10-13 18:52:02 -05:00
|
|
|
|
2020-10-13 19:31:00 -05:00
|
|
|
const getDesignerPath = (slug: string, category?: string) => {
|
|
|
|
const designer = slug.replace(/^brands/, 'designers')
|
2020-10-13 20:43:56 -05:00
|
|
|
|
|
|
|
return `/search${designer ? `/${designer}` : ''}${
|
|
|
|
category ? `/${category}` : ''
|
|
|
|
}`
|
2020-10-13 18:52:02 -05:00
|
|
|
}
|