-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathbundle.jsx
More file actions
188 lines (177 loc) · 9.64 KB
/
bundle.jsx
File metadata and controls
188 lines (177 loc) · 9.64 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
/*
* Copyright (c) 2023, 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, {useState, useRef} from 'react'
import PropTypes from 'prop-types'
import {Dialog, Flex, Box, VStack, useBreakpointValue} from '@chakra-ui/react'
import {keepPreviousData} from '@tanstack/react-query'
import ProductView from '../../components/product-view'
import {useProductViewModal} from '../../hooks/use-product-view-modal'
import SafePortal from '../safe-portal'
import {useProducts} from '@salesforce/commerce-sdk-react'
import ImageGallery, {Skeleton as ImageGallerySkeleton} from '../../components/image-gallery'
import {useDerivedProduct} from '../../hooks'
import {useIntl} from 'react-intl'
/**
* A Dialog that contains Product View for product bundle
*/
const BundleProductViewModal = ({product: bundle, isOpen, onClose, updateCart, ...props}) => {
const productViewModalData = useProductViewModal(bundle)
const {variationParams} = useDerivedProduct(bundle)
const childProductRefs = useRef({})
const [childProductOrderability, setChildProductOrderability] = useState({})
const [selectedChildProducts, setSelectedChildProducts] = useState([])
const [selectedBundleQuantity, setSelectedBundleQuantity] = useState(
productViewModalData?.product?.quantity
)
const trueIfMobile = useBreakpointValue({base: true, lg: false})
let childProductIds = productViewModalData.product?.bundledProductItems
?.map(({productId}) => productId)
.join(',')
const productIds = selectedChildProducts.map(({variant}) => variant.productId).join(',')
if (productIds?.length > 0 && productIds !== childProductIds) {
childProductIds = productIds
}
const {data: childProducts, isLoading} = useProducts(
{parameters: {ids: childProductIds, allImages: true}},
{
enabled: Boolean(childProductIds),
placeholderData: keepPreviousData
}
)
const intl = useIntl()
const label = intl.formatMessage(
{
defaultMessage: 'Edit modal for {productName}',
id: 'cart.product_edit_modal.modal_label'
},
{productName: productViewModalData?.product?.name}
)
return (
<Dialog.Root
open={isOpen}
onOpenChange={() => onClose()}
size="4xl"
closeOnInteractOutside={false}
>
<SafePortal>
<Dialog.Backdrop />
<Dialog.Positioner>
<Dialog.Content data-testid="product-view-modal" aria-label={label}>
<Dialog.CloseTrigger />
<Dialog.Body pb={8} bg="white" paddingBottom={6} marginTop={6}>
<Flex direction={['column', 'column', 'column', 'row']}>
{/* Due to desktop layout, we'll need to render the image gallery separately, from outside the ProductView */}
<Box
flex={1}
mr={[0, 0, 0, 6, 6]}
display={['none', 'none', 'none', 'block']}
>
{bundle ? (
<>
<ImageGallery
size="sm"
imageGroups={bundle.imageGroups}
selectedVariationAttributes={variationParams}
/>
</>
) : (
<ImageGallerySkeleton />
)}
</Box>
<VStack align="stretch" flex={1}>
{/* Parent product */}
<Box marginBottom={6}>
<ProductView
showFullLink={false}
showImageGallery={trueIfMobile}
product={productViewModalData.product}
isLoading={productViewModalData.isFetching}
updateCart={(product, quantity) =>
updateCart(product, quantity, selectedChildProducts)
}
validateOrderability={() => {
return Object.values(
childProductRefs.current
).every(({validateOrderability}) =>
validateOrderability()
)
}}
childProductOrderability={childProductOrderability}
setSelectedBundleQuantity={setSelectedBundleQuantity}
{...props}
/>
</Box>
{childProducts &&
childProducts.data.map((_product, i) => {
const product = {
..._product,
...productViewModalData.product.bundledProductItems[
i
]
}
const quantityPerBundle =
product.quantity / bundle.quantity
return (
<ProductView
key={i}
// 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[product.itemId] = {
ref,
validateOrderability:
this.validateOrderability
}
}}
showImageGallery={false}
isProductPartOfBundle={true}
showFullLink={false}
product={product}
isLoading={isLoading}
setChildProductOrderability={
setChildProductOrderability
}
childOfBundleQuantity={quantityPerBundle}
selectedBundleParentQuantity={
selectedBundleQuantity
}
onVariantSelected={(
product,
variant,
quantity
) => {
setSelectedChildProducts((prev) => {
const newArray = prev.slice(0)
newArray[i] = {
product,
variant,
quantity
}
return newArray
})
}}
/>
)
})}
</VStack>
</Flex>
</Dialog.Body>
</Dialog.Content>
</Dialog.Positioner>
</SafePortal>
</Dialog.Root>
)
}
BundleProductViewModal.propTypes = {
isOpen: PropTypes.bool.isRequired,
onOpen: PropTypes.func.isRequired,
onClose: PropTypes.func.isRequired,
product: PropTypes.object,
isLoading: PropTypes.bool,
updateCart: PropTypes.func
}
export default BundleProductViewModal