4
0
forked from crowetic/commerce
commerce/pages/product/[slug].tsx

48 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-10-01 20:40:40 -05:00
import { GetStaticPropsContext, InferGetStaticPropsType } from 'next'
import { useRouter } from 'next/router'
2020-10-15 18:55:39 -05:00
import getAllPages from '@lib/bigcommerce/api/operations/get-all-pages'
2020-10-03 05:10:08 -05:00
import getProduct from '@lib/bigcommerce/api/operations/get-product'
2020-10-01 20:40:40 -05:00
import { Layout } from '@components/core'
import { ProductView } from '@components/product'
2020-10-03 05:10:08 -05:00
import getAllProductPaths from '@lib/bigcommerce/api/operations/get-all-product-paths'
export async function getStaticProps({
params,
}: GetStaticPropsContext<{ slug: string }>) {
2020-10-15 18:55:39 -05:00
const { pages } = await getAllPages()
2020-10-03 04:17:41 -05:00
const { product } = await getProduct({ variables: { slug: params!.slug } })
2020-10-04 19:44:11 -05:00
if (!product) {
throw new Error(`Product with slug '${params!.slug}' not found`)
}
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
}
}
export async function getStaticPaths() {
2020-10-03 04:17:41 -05:00
const { products } = await getAllProductPaths()
2020-10-01 20:26:32 -05:00
return {
2020-10-10 14:19:05 -05:00
paths: 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-10-01 21:30:08 -05:00
export default function Slug({
2020-10-01 20:26:32 -05:00
product,
}: 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-10-01 21:30:08 -05:00
Slug.Layout = Layout