-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path[[...hash]].tsx
124 lines (111 loc) · 3.31 KB
/
[[...hash]].tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import type {
GetStaticPathsContext,
GetStaticPropsContext,
InferGetStaticPropsType,
} from 'next'
import commerce from '@lib/api/commerce'
import { Layout } from '@components/common'
import { ProductView } from '@components/product'
import { parsePersonalizedURL } from '@builder.io/personalization-utils/next'
import { useIsPreviewing, BuilderComponent, builder } from '@builder.io/react'
import builderConfig from '../../../config/builder'
import { useRouter } from 'next/router'
import { useEffect } from 'react'
import Cookies from 'js-cookie'
import { addAsyncProps } from '../../../lib/add-async-props'
builder.init(builderConfig.apiKey)
export async function getStaticProps({
params,
locale,
locales,
preview,
}: GetStaticPropsContext<{ slug: string; hash?: string[] }>) {
const config = { locale, locales }
const pagesPromise = commerce.getAllPages({ config, preview })
const siteInfoPromise = commerce.getSiteInfo({ config, preview })
const productPromise = commerce.getProduct({
variables: { slug: params!.slug },
config,
preview,
})
const allProductsPromise = commerce.getAllProducts({
variables: { first: 4 },
config,
preview,
})
const { pages } = await pagesPromise
const { categories } = await siteInfoPromise
const { product } = await productPromise
const { products: relatedProducts } = await allProductsPromise
const { attributes } = parsePersonalizedURL(params?.hash || [])
const builderSection =
(await builder
.get('product-editorial', {
userAttributes: {
...attributes,
product: params?.slug,
},
})
.promise()) || null
if (!product) {
throw new Error(`Product with slug '${params!.slug}' not found`)
}
if (builderSection) {
await addAsyncProps(builderSection)
}
return {
props: {
pages,
product,
relatedProducts,
categories,
builderSection,
},
revalidate: 1,
}
}
export async function getStaticPaths({ locales }: GetStaticPathsContext) {
const { products } = await commerce.getAllProductPaths()
return {
paths: locales
? locales.reduce<string[]>((arr, locale) => {
// Add a product path for every locale
products.forEach((product: any) => {
arr.push(`/${locale}/product${product.path}`)
})
return arr
}, [])
: products.map((product: any) => `/product${product.path}`),
fallback: 'blocking',
}
}
export default function ProductPage({
product,
relatedProducts,
builderSection,
}: InferGetStaticPropsType<typeof getStaticProps>) {
const router = useRouter()
const isPreviewingInBuilder = useIsPreviewing()
useEffect(() => {
if (router.query.slug?.includes('jacket')) {
Cookies.set(
'personalization.audience',
JSON.stringify(['jacket-shopper'])
)
}
if (router.query.slug?.includes('shirt')) {
Cookies.set('personalization.audience', JSON.stringify(['shirt-shopper']))
}
})
return router.isFallback && !isPreviewingInBuilder ? (
<h1>Loading...</h1>
) : (
<>
<ProductView product={product} relatedProducts={relatedProducts} />
{(builderSection || isPreviewingInBuilder) && (
<BuilderComponent model="product-editorial" content={builderSection} />
)}
</>
)
}
ProductPage.Layout = Layout