-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.jsx
More file actions
639 lines (586 loc) · 26.9 KB
/
index.jsx
File metadata and controls
639 lines (586 loc) · 26.9 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*
* Copyright (c) 2022, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React, {Fragment, useCallback, useEffect, useState} from 'react'
import PropTypes from 'prop-types'
import {Helmet} from 'react-helmet'
import {FormattedMessage, useIntl} from 'react-intl'
import {
normalizeSetBundleProduct,
getUpdateBundleChildArray
} from '@salesforce/retail-react-app/app/utils/product-utils'
// Components
import {Box, Button, Stack} from '@salesforce/retail-react-app/app/components/shared/ui'
import {
useProduct,
useProducts,
useCategory,
useShopperCustomersMutation,
useShopperBasketsMutation,
useCustomerId,
useShopperBasketsMutationHelper
} from '@salesforce/commerce-sdk-react'
// Hooks
import {useCurrentBasket} from '@salesforce/retail-react-app/app/hooks/use-current-basket'
import {useVariant} from '@salesforce/retail-react-app/app/hooks'
import useNavigation from '@salesforce/retail-react-app/app/hooks/use-navigation'
import useEinstein from '@salesforce/retail-react-app/app/hooks/use-einstein'
import useActiveData from '@salesforce/retail-react-app/app/hooks/use-active-data'
import {useServerContext} from '@salesforce/pwa-kit-react-sdk/ssr/universal/hooks'
// Project Components
import RecommendedProducts from '@salesforce/retail-react-app/app/components/recommended-products'
import ProductView from '@salesforce/retail-react-app/app/components/product-view'
import InformationAccordion from '@salesforce/retail-react-app/app/pages/product-detail/partials/information-accordion'
import {HTTPNotFound, HTTPError} from '@salesforce/pwa-kit-react-sdk/ssr/universal/errors'
import logger from '@salesforce/retail-react-app/app/utils/logger-instance'
// constant
import {
API_ERROR_MESSAGE,
EINSTEIN_RECOMMENDERS,
MAX_CACHE_AGE,
TOAST_ACTION_VIEW_WISHLIST,
TOAST_MESSAGE_ADDED_TO_WISHLIST,
TOAST_MESSAGE_ALREADY_IN_WISHLIST,
STALE_WHILE_REVALIDATE
} from '@salesforce/retail-react-app/app/constants'
import {rebuildPathWithParams} from '@salesforce/retail-react-app/app/utils/url'
import {useHistory, useLocation, useParams} from 'react-router-dom'
import {useToast} from '@salesforce/retail-react-app/app/hooks/use-toast'
import {useWishList} from '@salesforce/retail-react-app/app/hooks/use-wish-list'
const ProductDetail = () => {
const {formatMessage} = useIntl()
const history = useHistory()
const location = useLocation()
const einstein = useEinstein()
const activeData = useActiveData()
const toast = useToast()
const navigate = useNavigation()
const customerId = useCustomerId()
/****************************** Basket *********************************/
const {isLoading: isBasketLoading} = useCurrentBasket()
const {addItemToNewOrExistingBasket} = useShopperBasketsMutationHelper()
const updateItemsInBasketMutation = useShopperBasketsMutation('updateItemsInBasket')
const {res} = useServerContext()
if (res) {
res.set(
'Cache-Control',
`s-maxage=${MAX_CACHE_AGE}, stale-while-revalidate=${STALE_WHILE_REVALIDATE}`
)
}
/*************************** Product Detail and Category ********************/
const {productId} = useParams()
const urlParams = new URLSearchParams(location.search)
const {
data: product,
isLoading: isProductLoading,
isError: isProductError,
error: productError
} = useProduct(
{
parameters: {
id: urlParams.get('pid') || productId,
perPricebook: true,
expand: [
'availability',
'promotions',
'options',
'images',
'prices',
'variations',
'set_products',
'bundled_products',
'page_meta_tags'
],
allImages: true
}
},
{
// When shoppers select a different variant (and the app fetches the new data),
// the old data is still rendered (and not the skeletons).
keepPreviousData: true
}
)
// Note: Since category needs id from product detail, it can't be server side rendered atm
// until we can do dependent query on server
const {
data: category,
isError: isCategoryError,
error: categoryError
} = useCategory({
parameters: {
id: product?.primaryCategoryId,
levels: 1
}
})
/****************************** Sets and Bundles *********************************/
const [childProductSelection, setChildProductSelection] = useState({})
const [childProductOrderability, setChildProductOrderability] = useState({})
const [selectedBundleQuantity, setSelectedBundleQuantity] = useState(1)
const childProductRefs = React.useRef({})
const isProductASet = product?.type.set
const isProductABundle = product?.type.bundle
let bundleChildVariantIds = ''
if (isProductABundle)
bundleChildVariantIds = Object.keys(childProductSelection)
?.map((key) => childProductSelection[key].variant.productId)
.join(',')
const {data: bundleChildrenData} = useProducts(
{
parameters: {
ids: bundleChildVariantIds,
allImages: false,
expand: ['availability', 'variations'],
select: '(data.(id,inventory,master))'
}
},
{
enabled: bundleChildVariantIds?.length > 0,
keepPreviousData: true
}
)
if (isProductABundle && bundleChildrenData) {
// Loop through the bundle children and update the inventory for variant selection
product.bundledProducts.forEach(({product: childProduct}, index) => {
const matchingChildProduct = bundleChildrenData.data.find(
(bundleChild) => bundleChild.master.masterId === childProduct.id
)
if (matchingChildProduct) {
product.bundledProducts[index].product = {
...childProduct,
inventory: matchingChildProduct.inventory
}
}
})
}
const comboProduct = isProductASet || isProductABundle ? normalizeSetBundleProduct(product) : {}
/**************** Error Handling ****************/
if (isProductError) {
const errorStatus = productError?.response?.status
switch (errorStatus) {
case 404:
throw new HTTPNotFound('Product Not Found.')
default:
throw new HTTPError(errorStatus, `HTTP Error ${errorStatus} occurred.`)
}
}
if (isCategoryError) {
const errorStatus = categoryError?.response?.status
switch (errorStatus) {
case 404:
throw new HTTPNotFound('Category Not Found.')
default:
throw new HTTPError(errorStatus, `HTTP Error ${errorStatus} occurred.`)
}
}
const [primaryCategory, setPrimaryCategory] = useState(category)
const variant = useVariant(product)
// This page uses the `primaryCategoryId` to retrieve the category data. This attribute
// is only available on `master` products. Since a variation will be loaded once all the
// attributes are selected (to get the correct inventory values), the category information
// is overridden. This will allow us to keep the initial category around until a different
// master product is loaded.
useEffect(() => {
if (category) {
setPrimaryCategory(category)
}
}, [category])
/**************** Product Variant ****************/
useEffect(() => {
if (!variant) {
return
}
// update the variation attributes parameter on
// the url accordingly as the variant changes
const updatedUrl = rebuildPathWithParams(`${location.pathname}${location.search}`, {
pid: variant?.productId
})
history.replace(updatedUrl)
}, [variant])
/**************** Wishlist ****************/
const {data: wishlist, isLoading: isWishlistLoading} = useWishList()
const createCustomerProductListItem = useShopperCustomersMutation(
'createCustomerProductListItem'
)
const handleAddToWishlist = (product, variant, quantity) => {
const isItemInWishlist = wishlist?.customerProductListItems?.find(
(i) => i.productId === variant?.productId || i.productId === product?.id
)
if (!isItemInWishlist) {
createCustomerProductListItem.mutate(
{
parameters: {
listId: wishlist.id,
customerId
},
body: {
// NOTE: API does not respect quantity, it always adds 1
quantity,
productId: variant?.productId || product?.id,
public: false,
priority: 1,
type: 'product'
}
},
{
onSuccess: () => {
toast({
title: formatMessage(TOAST_MESSAGE_ADDED_TO_WISHLIST, {quantity: 1}),
status: 'success',
action: (
// it would be better if we could use <Button as={Link}>
// but unfortunately the Link component is not compatible
// with Chakra Toast, since the ToastManager is rendered via portal
// and the toast doesn't have access to intl provider, which is a
// requirement of the Link component.
<Button
variant="link"
onClick={() => navigate('/account/wishlist')}
>
{formatMessage(TOAST_ACTION_VIEW_WISHLIST)}
</Button>
)
})
},
onError: () => {
showError()
}
}
)
} else {
toast({
title: formatMessage(TOAST_MESSAGE_ALREADY_IN_WISHLIST),
status: 'info',
action: (
<Button variant="link" onClick={() => navigate('/account/wishlist')}>
{formatMessage(TOAST_ACTION_VIEW_WISHLIST)}
</Button>
)
})
}
}
/**************** Add To Cart ****************/
const showToast = useToast()
const showError = () => {
showToast({
title: formatMessage(API_ERROR_MESSAGE),
status: 'error'
})
}
const handleAddToCart = async (productSelectionValues) => {
try {
const productItems = productSelectionValues.map(({variant, quantity}) => ({
productId: variant.productId,
price: variant.price,
quantity
}))
await addItemToNewOrExistingBasket(productItems)
einstein.sendAddToCart(productItems)
// If the items were successfully added, set the return value to be used
// by the add to cart modal.
return productSelectionValues
} catch (error) {
console.log('error', error)
showError(error)
}
}
/**************** Product Set/Bundles Handlers ****************/
const handleChildProductValidation = useCallback(() => {
// Run validation for all child products. This will ensure the error
// messages are shown.
Object.values(childProductRefs.current).forEach(({validateOrderability}) => {
validateOrderability({scrollErrorIntoView: false})
})
// Using ot state for which child products are selected, scroll to the first
// one that isn't selected.
const selectedProductIds = Object.keys(childProductSelection)
const firstUnselectedProduct = comboProduct.childProducts.find(
({product: childProduct}) => !selectedProductIds.includes(childProduct.id)
)?.product
if (firstUnselectedProduct) {
// Get the reference to the product view and scroll to it.
const {ref} = childProductRefs.current[firstUnselectedProduct.id]
if (ref.scrollIntoView) {
ref.scrollIntoView({
behavior: 'smooth',
block: 'end'
})
}
return false
}
return true
}, [product, childProductSelection])
/**************** Product Set Handlers ****************/
const handleProductSetAddToCart = () => {
// Get all the selected products, and pass them to the addToCart handler which
// accepts an array.
const productSelectionValues = Object.values(childProductSelection)
return handleAddToCart(productSelectionValues)
}
/**************** Product Bundle Handlers ****************/
// Top level bundle does not have variants
const handleProductBundleAddToCart = async (variant, selectedQuantity) => {
try {
const childProductSelections = Object.values(childProductSelection)
const productItems = [
{
productId: product.id,
price: product.price,
quantity: selectedQuantity,
// The add item endpoint in the shopper baskets API does not respect variant selections
// for bundle children, so we have to make a follow up call to update the basket
// with the chosen variant selections
bundledProductItems: childProductSelections.map((child) => {
return {
productId: child.variant.productId,
quantity: child.quantity
}
})
}
]
const res = await addItemToNewOrExistingBasket(productItems)
const bundleChildMasterIds = childProductSelections.map((child) => {
return child.product.id
})
// since the returned data includes all products in basket
// here we compare list of productIds in bundleProductItems of each productItem to filter out the
// current bundle that was last added into cart
const currentBundle = res.productItems.find((productItem) => {
if (!productItem.bundledProductItems?.length) return
const bundleChildIds = productItem.bundledProductItems?.map((item) => {
// seek out the bundle child that still uses masterId as product id
return item.productId
})
return bundleChildIds.every((id) => bundleChildMasterIds.includes(id))
})
const itemsToBeUpdated = getUpdateBundleChildArray(
currentBundle,
childProductSelections
)
if (itemsToBeUpdated.length) {
// make a follow up call to update child variant selection for product bundle
// since add item endpoint doesn't currently consider product bundle child variants
await updateItemsInBasketMutation.mutateAsync({
method: 'PATCH',
parameters: {
basketId: res.basketId
},
body: itemsToBeUpdated
})
}
einstein.sendAddToCart(productItems)
return childProductSelections
} catch (error) {
showError(error)
}
}
/**************** Einstein ****************/
useEffect(() => {
if (product && product.type.set) {
einstein.sendViewProduct(product)
const childrenProducts = product.setProducts
childrenProducts.map((child) => {
try {
einstein.sendViewProduct(child)
} catch (err) {
logger.error('Einstein sendViewProduct error', {
namespace: 'ProductDetail.useEffect',
additionalProperties: {error: err, child}
})
}
activeData.sendViewProduct(category, child, 'detail')
})
} else if (product) {
try {
einstein.sendViewProduct(product)
} catch (err) {
logger.error('Einstein sendViewProduct error', {
namespace: 'ProductDetail.useEffect',
additionalProperties: {error: err, product}
})
}
activeData.sendViewProduct(category, product, 'detail')
}
}, [product])
return (
<Box
className="sf-product-detail-page"
layerStyle="page"
data-testid="product-details-page"
>
<Helmet>
<title>{product?.pageTitle}</title>
{product?.pageMetaTags ? (
// description is one of the page meta tags returned for products
// and this is the same as product?.pageDescription
product.pageMetaTags?.map((pageMetaTag) => {
const name = pageMetaTag.id
const content = pageMetaTag.value
return <meta name={name} content={content} key={name} />
})
) : (
<meta name="description" content={product?.pageDescription} />
)}
</Helmet>
<Stack spacing={16}>
{isProductASet || isProductABundle ? (
<Fragment>
<ProductView
product={product}
category={primaryCategory?.parentCategoryTree || []}
addToCart={
isProductASet
? handleProductSetAddToCart
: handleProductBundleAddToCart
}
addToWishlist={handleAddToWishlist}
isProductLoading={isProductLoading}
isBasketLoading={isBasketLoading}
isWishlistLoading={isWishlistLoading}
validateOrderability={handleChildProductValidation}
childProductOrderability={childProductOrderability}
setSelectedBundleQuantity={setSelectedBundleQuantity}
/>
<hr />
{/* TODO: consider `childProduct.belongsToSet` */}
{
// Render the child products
comboProduct.childProducts.map(
({product: childProduct, quantity: childQuantity}) => (
<Box key={childProduct.id} data-testid="child-product">
<ProductView
// Do not use an arrow function as we are manipulating the functions scope.
ref={function (ref) {
// Assign the "set" scope of the ref, this is how we access the internal
// validation.
childProductRefs.current[childProduct.id] = {
ref,
validateOrderability: this.validateOrderability
}
}}
product={childProduct}
isProductPartOfSet={isProductASet}
isProductPartOfBundle={isProductABundle}
childOfBundleQuantity={childQuantity}
selectedBundleParentQuantity={selectedBundleQuantity}
addToCart={
isProductASet
? (variant, quantity) =>
handleAddToCart([
{
product: childProduct,
variant,
quantity
}
])
: null
}
addToWishlist={
isProductASet ? handleAddToWishlist : null
}
onVariantSelected={(product, variant, quantity) => {
if (quantity) {
setChildProductSelection((previousState) => ({
...previousState,
[product.id]: {
product,
variant,
quantity: isProductABundle
? childQuantity
: quantity
}
}))
} else {
const selections = {...childProductSelection}
delete selections[product.id]
setChildProductSelection(selections)
}
}}
isProductLoading={isProductLoading}
isBasketLoading={isBasketLoading}
isWishlistLoading={isWishlistLoading}
setChildProductOrderability={
setChildProductOrderability
}
/>
<InformationAccordion product={childProduct} />
<Box display={['none', 'none', 'none', 'block']}>
<hr />
</Box>
</Box>
)
)
}
</Fragment>
) : (
<Fragment>
<ProductView
product={product}
category={primaryCategory?.parentCategoryTree || []}
addToCart={(variant, quantity) =>
handleAddToCart([{product, variant, quantity}])
}
addToWishlist={handleAddToWishlist}
isProductLoading={isProductLoading}
isBasketLoading={isBasketLoading}
isWishlistLoading={isWishlistLoading}
/>
<InformationAccordion product={product} />
</Fragment>
)}
{/* Product Recommendations */}
<Stack spacing={16}>
{!isProductASet && (
<RecommendedProducts
title={
<FormattedMessage
defaultMessage="Complete the Set"
id="product_detail.recommended_products.title.complete_set"
/>
}
recommender={EINSTEIN_RECOMMENDERS.PDP_COMPLETE_SET}
products={[product]}
mx={{base: -4, md: -8, lg: 0}}
shouldFetch={() => product?.id}
/>
)}
<RecommendedProducts
title={
<FormattedMessage
defaultMessage="You might also like"
id="product_detail.recommended_products.title.might_also_like"
/>
}
recommender={EINSTEIN_RECOMMENDERS.PDP_MIGHT_ALSO_LIKE}
products={[product]}
mx={{base: -4, md: -8, lg: 0}}
shouldFetch={() => product?.id}
/>
<RecommendedProducts
// The Recently Viewed recommender doesn't use `products`, so instead we
// provide a key to update the recommendations on navigation.
key={location.key}
title={
<FormattedMessage
defaultMessage="Recently Viewed"
id="product_detail.recommended_products.title.recently_viewed"
/>
}
recommender={EINSTEIN_RECOMMENDERS.PDP_RECENTLY_VIEWED}
mx={{base: -4, md: -8, lg: 0}}
/>
</Stack>
</Stack>
</Box>
)
}
ProductDetail.getTemplateName = () => 'product-detail'
ProductDetail.propTypes = {
/**
* The current react router match object. (Provided internally)
*/
match: PropTypes.object
}
export default ProductDetail