-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.jsx
More file actions
149 lines (137 loc) · 7.22 KB
/
index.jsx
File metadata and controls
149 lines (137 loc) · 7.22 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
/*
* Copyright (c) 2025, salesforce.com, 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 from 'react'
import PropTypes from 'prop-types'
// Chakra Components
import {Box, Fade, Flex, Stack, Text} from '@salesforce/retail-react-app/app/components/shared/ui'
// Project Components
import {HideOnDesktop, HideOnMobile} from '@salesforce/retail-react-app/app/components/responsive'
import ItemVariantProvider from '@salesforce/retail-react-app/app/components/item-variant'
import CartItemVariantImage from '@salesforce/retail-react-app/app/components/item-variant/item-image'
import CartItemVariantName from '@salesforce/retail-react-app/app/components/item-variant/item-name'
import CartItemVariantAttributes from '@salesforce/retail-react-app/app/components/item-variant/item-attributes'
import CartItemVariantPrice from '@salesforce/retail-react-app/app/components/item-variant/item-price'
import LoadingSpinner from '@salesforce/retail-react-app/app/components/loading-spinner'
import BonusProductQuantity from '@salesforce/retail-react-app/app/components/product-item/bonus-product-quantity'
import ProductQuantityPicker from '@salesforce/retail-react-app/app/components/product-item/product-quantity-picker'
// Utilities
import {noop} from '@salesforce/retail-react-app/app/utils/utils'
// Hooks
import {useCurrency, useDerivedProduct} from '@salesforce/retail-react-app/app/hooks'
/**
* Component representing a product item usually in a list with details about the product - name, variant, pricing, etc.
* @param {Object} product Product to be represented in the list item.
* @param {node} primaryAction Child component representing the most prominent action to be performed by the user.
* @param {node} secondaryActions Child component representing the other actions relevant to the product to be performed by the user.
* @param {node} deliveryActions Child component representing the delivery actions relevant to the product to be performed by the user.
* @param {func} onItemQuantityChange callback function to be invoked whenever item quantity changes.
* @param {boolean} showLoading Renders a loading spinner with overlay if set to true.
* @returns A JSX element representing product item in a list (eg: wishlist, cart, etc).
*/
const ProductItem = ({
product,
primaryAction,
secondaryActions,
deliveryActions,
onItemQuantityChange = noop,
showLoading = false,
containerStyles = {},
isRemoving = false,
pickupInStore = false
}) => {
const {stepQuantity, showInventoryMessage, inventoryMessage, quantity, setQuantity} =
useDerivedProduct(product, false, false, pickupInStore)
const {currency: activeCurrency} = useCurrency()
return (
<Box
position="relative"
data-testid={`sf-cart-item-${product.productId ? product.productId : product.id}`}
>
<ItemVariantProvider variant={product}>
{showLoading && <LoadingSpinner />}
<Stack layerStyle="cardBordered" align="flex-start" {...containerStyles}>
<Flex width="full" alignItems="flex-start" backgroundColor="white">
<CartItemVariantImage width={['88px', '136px']} mr={4} />
<Stack spacing={3} flex={1}>
<Flex align="flex-end" justify="space-between">
<Stack spacing={1}>
<CartItemVariantName />
<CartItemVariantAttributes excludeBonusLabel />
<HideOnDesktop>
<Box marginTop={2}>
<CartItemVariantPrice
align="left"
currency={activeCurrency}
/>
</Box>
</HideOnDesktop>
</Stack>
{deliveryActions && !product.bonusProductLineItem && (
<HideOnMobile>{deliveryActions}</HideOnMobile>
)}
</Flex>
{deliveryActions && !product.bonusProductLineItem && (
<HideOnDesktop>{deliveryActions}</HideOnDesktop>
)}
<Flex align="flex-end" justify="space-between">
<Stack spacing={1}>
{!(isRemoving && product.bonusProductLineItem) &&
(product.bonusProductLineItem ? (
<BonusProductQuantity product={product} />
) : (
<ProductQuantityPicker
product={product}
onItemQuantityChange={onItemQuantityChange}
stepQuantity={stepQuantity}
quantity={quantity}
setQuantity={setQuantity}
/>
))}
</Stack>
<Stack>
<HideOnMobile>
<CartItemVariantPrice currency={activeCurrency} />
</HideOnMobile>
<Box display={['none', 'block', 'block', 'block']}>
{primaryAction}
</Box>
</Stack>
</Flex>
<Box>
{product && showInventoryMessage && (
<Fade in={true}>
<Text color="orange.600" fontWeight={600}>
{inventoryMessage}
</Text>
</Fade>
)}
</Box>
{secondaryActions}
</Stack>
</Flex>
<Box display={['block', 'none', 'none', 'none']} w={'full'}>
{primaryAction}
</Box>
</Stack>
</ItemVariantProvider>
</Box>
)
}
ProductItem.propTypes = {
product: PropTypes.object,
onItemQuantityChange: PropTypes.func,
onAddItemToCart: PropTypes.func,
showLoading: PropTypes.bool,
isWishlistItem: PropTypes.bool,
primaryAction: PropTypes.node,
secondaryActions: PropTypes.node,
deliveryActions: PropTypes.node,
containerStyles: PropTypes.object,
isRemoving: PropTypes.bool,
pickupInStore: PropTypes.bool
}
export default ProductItem