|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +import React, {useMemo, useCallback} from 'react' |
| 9 | +import PropTypes from 'prop-types' |
| 10 | +import { |
| 11 | + Modal, |
| 12 | + ModalOverlay, |
| 13 | + ModalContent, |
| 14 | + ModalBody, |
| 15 | + ModalCloseButton, |
| 16 | + Button, |
| 17 | + Box, |
| 18 | + Text |
| 19 | +} from '@salesforce/retail-react-app/app/components/shared/ui' |
| 20 | +import ProductView from '@salesforce/retail-react-app/app/components/product-view' |
| 21 | +import {useProductViewModal} from '@salesforce/retail-react-app/app/hooks/use-product-view-modal' |
| 22 | +import {useIntl} from 'react-intl' |
| 23 | +import {useShopperBasketsMutationHelper} from '@salesforce/commerce-sdk-react' |
| 24 | +import {useCurrentBasket} from '@salesforce/retail-react-app/app/hooks/use-current-basket' |
| 25 | +import {findAvailableBonusDiscountLineItemId} from '@salesforce/retail-react-app/app/utils/bonus-product-utils' |
| 26 | +import useNavigation from '@salesforce/retail-react-app/app/hooks/use-navigation' |
| 27 | +import {useLocation} from 'react-router-dom' |
| 28 | +import {productViewModalTheme} from '@salesforce/retail-react-app/app/theme/components/project/product-view-modal' |
| 29 | + |
| 30 | +/** |
| 31 | + * A Modal that contains Bonus Product View |
| 32 | + */ |
| 33 | +const BonusProductViewModal = ({ |
| 34 | + product, |
| 35 | + isOpen, |
| 36 | + onClose, |
| 37 | + bonusDiscountLineItemId, |
| 38 | + promotionId, |
| 39 | + ...props |
| 40 | +}) => { |
| 41 | + // Ensure a safe product shape for the modal hook |
| 42 | + const safeProduct = useMemo(() => { |
| 43 | + if (!product) return {productId: undefined, variants: [], variationAttributes: []} |
| 44 | + const id = product.productId || product.id |
| 45 | + return { |
| 46 | + productId: id, |
| 47 | + id, |
| 48 | + variants: product.variants || [], |
| 49 | + variationAttributes: product.variationAttributes || [], |
| 50 | + imageGroups: product.imageGroups || [], |
| 51 | + type: product.type || {set: false, bundle: false}, |
| 52 | + price: product.price, |
| 53 | + name: product.name || product.productName |
| 54 | + } |
| 55 | + }, [product]) |
| 56 | + |
| 57 | + const productViewModalData = useProductViewModal(safeProduct) |
| 58 | + const {addItemToNewOrExistingBasket} = useShopperBasketsMutationHelper() |
| 59 | + const {data: basket} = useCurrentBasket() |
| 60 | + const navigate = useNavigation() |
| 61 | + const location = useLocation() |
| 62 | + |
| 63 | + const intl = useIntl() |
| 64 | + const {formatMessage} = intl |
| 65 | + |
| 66 | + const messages = useMemo( |
| 67 | + () => ({ |
| 68 | + modalLabel: formatMessage( |
| 69 | + { |
| 70 | + id: 'bonus_product_view_modal.modal_label', |
| 71 | + defaultMessage: 'Bonus product selection modal for {productName}' |
| 72 | + }, |
| 73 | + {productName: productViewModalData?.product?.name} |
| 74 | + ), |
| 75 | + viewCart: formatMessage({ |
| 76 | + id: 'bonus_product_view_modal.button.view_cart', |
| 77 | + defaultMessage: 'View Cart' |
| 78 | + }) |
| 79 | + }), |
| 80 | + [intl] |
| 81 | + ) |
| 82 | + |
| 83 | + // Determine context for navigation behavior |
| 84 | + const isFromAddToCartModal = location.pathname !== '/cart' |
| 85 | + |
| 86 | + // Custom addToCart handler for bonus products that includes bonusDiscountLineItemId |
| 87 | + const handleAddToCart = useCallback( |
| 88 | + async (variant, quantity) => { |
| 89 | + try { |
| 90 | + // Default quantity to 1 if not provided or invalid |
| 91 | + const finalQuantity = quantity && quantity > 0 ? quantity : 1 |
| 92 | + |
| 93 | + // Find the first available bonus discount line item with capacity |
| 94 | + const availableBonusDiscountLineItemId = findAvailableBonusDiscountLineItemId( |
| 95 | + basket, |
| 96 | + promotionId, |
| 97 | + finalQuantity, |
| 98 | + bonusDiscountLineItemId // fallback to originally passed id |
| 99 | + ) |
| 100 | + |
| 101 | + if (!availableBonusDiscountLineItemId) { |
| 102 | + console.warn('No available bonus discount line item found') |
| 103 | + return null |
| 104 | + } |
| 105 | + |
| 106 | + const productItems = [ |
| 107 | + { |
| 108 | + productId: variant?.productId || product?.productId || product?.id, |
| 109 | + price: variant?.price || product?.price, |
| 110 | + quantity: parseInt(finalQuantity, 10), |
| 111 | + bonusDiscountLineItemId: availableBonusDiscountLineItemId |
| 112 | + } |
| 113 | + ] |
| 114 | + |
| 115 | + const result = await addItemToNewOrExistingBasket(productItems) |
| 116 | + |
| 117 | + // Navigate to cart page after successful add to cart |
| 118 | + if (result) { |
| 119 | + // Close modal immediately and navigate with proper delay |
| 120 | + onClose() |
| 121 | + // Always use a delay to ensure modal closes cleanly |
| 122 | + setTimeout(() => { |
| 123 | + navigate('/cart', 'push') |
| 124 | + }, 200) |
| 125 | + } |
| 126 | + |
| 127 | + return result |
| 128 | + } catch (error) { |
| 129 | + console.error('Error adding bonus product to cart:', error) |
| 130 | + return null |
| 131 | + } |
| 132 | + }, |
| 133 | + [ |
| 134 | + addItemToNewOrExistingBasket, |
| 135 | + product, |
| 136 | + bonusDiscountLineItemId, |
| 137 | + promotionId, |
| 138 | + basket, |
| 139 | + onClose, |
| 140 | + navigate, |
| 141 | + isFromAddToCartModal |
| 142 | + ] |
| 143 | + ) |
| 144 | + |
| 145 | + // Custom buttons for the ProductView |
| 146 | + const handleViewCart = useCallback(() => { |
| 147 | + // Close modal immediately and navigate with proper delay |
| 148 | + onClose() |
| 149 | + // Always use a delay to ensure modal closes cleanly |
| 150 | + setTimeout(() => { |
| 151 | + navigate('/cart', 'push') |
| 152 | + }, 200) |
| 153 | + }, [onClose, navigate]) |
| 154 | + |
| 155 | + const customButtons = useMemo( |
| 156 | + () => [ |
| 157 | + <Button key="view-cart" variant="outline" onClick={handleViewCart}> |
| 158 | + {messages.viewCart} |
| 159 | + </Button> |
| 160 | + ], |
| 161 | + [messages.viewCart, handleViewCart] |
| 162 | + ) |
| 163 | + |
| 164 | + // Aggressively clean product data to prevent SwatchGroup errors while preserving essential fields |
| 165 | + const productToRender = useMemo(() => { |
| 166 | + const baseProduct = productViewModalData.product || safeProduct |
| 167 | + return { |
| 168 | + ...baseProduct, |
| 169 | + variationAttributes: [], // Force empty array |
| 170 | + variants: [], // Also remove variants to be safe |
| 171 | + variationParams: {}, |
| 172 | + selectedVariationAttributes: {}, |
| 173 | + type: {...baseProduct.type, variant: false, master: false}, |
| 174 | + // Ensure proper inventory and quantity defaults for bonus products |
| 175 | + inventory: { |
| 176 | + ...baseProduct.inventory, |
| 177 | + orderable: true, |
| 178 | + stockLevel: 999 // High stock level for bonus products |
| 179 | + }, |
| 180 | + minOrderQuantity: 1, |
| 181 | + stepQuantity: 1, |
| 182 | + // Ensure the product is orderable |
| 183 | + orderable: true |
| 184 | + } |
| 185 | + }, [productViewModalData.product, safeProduct]) |
| 186 | + |
| 187 | + return ( |
| 188 | + <Modal |
| 189 | + isOpen={isOpen} |
| 190 | + onClose={onClose} |
| 191 | + size={productViewModalTheme.modal.size} |
| 192 | + closeOnOverlayClick={true} |
| 193 | + closeOnEsc={true} |
| 194 | + motionPreset="slideInBottom" |
| 195 | + preserveScrollBarGap={true} |
| 196 | + > |
| 197 | + <ModalOverlay bg="blackAlpha.600" /> |
| 198 | + <ModalContent |
| 199 | + data-testid="bonus-product-view-modal" |
| 200 | + aria-label={messages.modalLabel} |
| 201 | + margin="0" |
| 202 | + borderRadius={{base: 'none', md: 'base'}} |
| 203 | + bg="white" |
| 204 | + maxHeight="85vh" |
| 205 | + overflowY="auto" |
| 206 | + boxShadow="xl" |
| 207 | + > |
| 208 | + <ModalBody bg="white" p={6} pb={8} mt={6}> |
| 209 | + {productViewModalData.isFetching && !productViewModalData.product ? ( |
| 210 | + <Box p={8} textAlign="center"> |
| 211 | + <Text>Loading product details...</Text> |
| 212 | + </Box> |
| 213 | + ) : ( |
| 214 | + <ProductView |
| 215 | + showFullLink={false} |
| 216 | + imageSize="sm" |
| 217 | + showImageGallery={true} |
| 218 | + product={productToRender} |
| 219 | + isLoading={false} |
| 220 | + addToCart={handleAddToCart} |
| 221 | + isProductLoading={false} |
| 222 | + customButtons={customButtons} |
| 223 | + promotionId={promotionId} |
| 224 | + {...props} |
| 225 | + /> |
| 226 | + )} |
| 227 | + </ModalBody> |
| 228 | + <ModalCloseButton size="sm" /> |
| 229 | + </ModalContent> |
| 230 | + </Modal> |
| 231 | + ) |
| 232 | +} |
| 233 | + |
| 234 | +BonusProductViewModal.propTypes = { |
| 235 | + isOpen: PropTypes.bool.isRequired, |
| 236 | + onOpen: PropTypes.func, |
| 237 | + onClose: PropTypes.func.isRequired, |
| 238 | + product: PropTypes.object, |
| 239 | + isLoading: PropTypes.bool, |
| 240 | + bonusDiscountLineItemId: PropTypes.string, // The 'id' from bonusDiscountLineItems |
| 241 | + promotionId: PropTypes.string // The promotion ID to filter promotions in PromoCallout |
| 242 | +} |
| 243 | + |
| 244 | +export default BonusProductViewModal |
0 commit comments