2020-10-25 15:14:54 -05:00
|
|
|
import {
|
|
|
|
GetStaticPathsContext,
|
|
|
|
GetStaticPropsContext,
|
|
|
|
InferGetStaticPropsType,
|
|
|
|
} from 'next'
|
2020-10-01 20:40:40 -05:00
|
|
|
import { useRouter } from 'next/router'
|
2020-10-27 02:35:40 +01:00
|
|
|
import { getConfig } from '@bigcommerce/storefront-data-hooks/dist/api'
|
|
|
|
import getAllPages from '@bigcommerce/storefront-data-hooks/dist/api/operations/get-all-pages'
|
|
|
|
import getProduct from '@bigcommerce/storefront-data-hooks/dist/api/operations/get-product'
|
2020-10-01 20:40:40 -05:00
|
|
|
import { Layout } from '@components/core'
|
|
|
|
import { ProductView } from '@components/product'
|
2020-10-27 02:35:40 +01:00
|
|
|
import getAllProductPaths from '@bigcommerce/storefront-data-hooks/dist/api/operations/get-all-product-paths'
|
2020-09-29 16:02:09 -03:00
|
|
|
|
2020-10-01 14:53:29 -05:00
|
|
|
export async function getStaticProps({
|
|
|
|
params,
|
2020-10-25 15:14:54 -05:00
|
|
|
locale,
|
2020-10-01 14:53:29 -05:00
|
|
|
}: GetStaticPropsContext<{ slug: string }>) {
|
2020-10-25 15:14:54 -05:00
|
|
|
const config = getConfig({ locale })
|
|
|
|
|
|
|
|
const { pages } = await getAllPages({ config })
|
|
|
|
const { product } = await getProduct({
|
|
|
|
variables: { slug: params!.slug },
|
|
|
|
config,
|
|
|
|
})
|
2020-10-04 19:44:11 -05:00
|
|
|
|
|
|
|
if (!product) {
|
|
|
|
throw new Error(`Product with slug '${params!.slug}' not found`)
|
|
|
|
}
|
|
|
|
|
2020-09-29 16:02:09 -03:00
|
|
|
return {
|
2020-10-15 18:55:39 -05:00
|
|
|
props: { pages, product },
|
2020-09-29 19:13:06 -03:00
|
|
|
revalidate: 200,
|
2020-10-01 20:40:40 -05:00
|
|
|
}
|
2020-09-29 16:02:09 -03:00
|
|
|
}
|
|
|
|
|
2020-10-25 15:14:54 -05:00
|
|
|
export async function getStaticPaths({ locales }: GetStaticPathsContext) {
|
2020-10-03 04:17:41 -05:00
|
|
|
const { products } = await getAllProductPaths()
|
2020-10-01 20:26:32 -05:00
|
|
|
|
2020-09-29 16:02:09 -03:00
|
|
|
return {
|
2020-10-25 15:14:54 -05:00
|
|
|
paths: locales
|
|
|
|
? locales.reduce<string[]>((arr, locale) => {
|
|
|
|
// Add a product path for every locale
|
|
|
|
products.forEach((product) => {
|
|
|
|
arr.push(`/${locale}/product${product.node.path}`)
|
|
|
|
})
|
|
|
|
return arr
|
|
|
|
}, [])
|
|
|
|
: products.map((product) => `/product${product.node.path}`),
|
2020-10-22 21:38:03 -05:00
|
|
|
// If your store has tons of products, enable fallback mode to improve build times!
|
2020-10-07 15:08:17 -05:00
|
|
|
fallback: false,
|
2020-10-01 20:40:40 -05:00
|
|
|
}
|
2020-09-29 16:02:09 -03:00
|
|
|
}
|
|
|
|
|
2020-10-01 21:30:08 -05:00
|
|
|
export default function Slug({
|
2020-10-01 20:26:32 -05:00
|
|
|
product,
|
2020-10-01 14:53:29 -05:00
|
|
|
}: InferGetStaticPropsType<typeof getStaticProps>) {
|
2020-10-01 20:40:40 -05:00
|
|
|
const router = useRouter()
|
2020-10-01 20:26:32 -05:00
|
|
|
|
2020-10-01 21:30:08 -05:00
|
|
|
return router.isFallback ? (
|
2020-10-12 10:46:17 -03:00
|
|
|
<h1>Loading...</h1> // TODO (BC) Add Skeleton Views
|
2020-10-01 21:30:08 -05:00
|
|
|
) : (
|
2020-10-12 10:46:17 -03:00
|
|
|
<ProductView product={product} />
|
2020-10-01 20:40:40 -05:00
|
|
|
)
|
2020-09-29 16:02:09 -03:00
|
|
|
}
|
2020-10-01 21:30:08 -05:00
|
|
|
|
|
|
|
Slug.Layout = Layout
|