Skip to content

Commit ad4ce2d

Browse files
authored
Merge branch 'feature/chakra-ui-upgrade-v3' into cleanup/chakra-ui-v3-part-1
2 parents b09716f + 4b74eb2 commit ad4ce2d

File tree

9 files changed

+26
-60
lines changed

9 files changed

+26
-60
lines changed

packages/extension-chakra-storefront/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ module.exports = {
6262
'<rootDir>/src/pages/checkout/partials/login-state.test.js',
6363
'<rootDir>/src/pages/account/orders.test.js',
6464
'<rootDir>/src/utils/responsive-image.test.js',
65+
'<rootDir>/src/hooks/use-errors.test.js',
6566
'<rootDir>/src/hooks/use-toast.test.js',
6667
'<rootDir>/src/pages/product-detail/metadata.test.js',
6768
'<rootDir>/src/pages/product-list/metadata.test.js',

packages/extension-chakra-storefront/src/components/product-view/index.jsx

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ import withRegistration from '../../components/with-registration'
2323
import {Skeleton as ImageGallerySkeleton} from '../../components/image-gallery'
2424
import {HideOnDesktop, HideOnMobile} from '../../components/responsive'
2525
import QuantityPicker from '../../components/quantity-picker'
26-
import useToast from '../../hooks/use-toast'
27-
import {API_ERROR_MESSAGE} from '../../constants'
26+
import {useErrorHandler} from '../../hooks/use-errors'
2827
import DisplayPrice from '../../components/display-price'
2928
import Swatch from '../../components/swatch-group/swatch'
3029
import SwatchGroup from '../../components/swatch-group'
@@ -114,7 +113,7 @@ const ProductView = forwardRef(
114113
ref
115114
) => {
116115
const {currency: activeCurrency} = useCurrency()
117-
const toast = useToast()
116+
const showError = useErrorHandler()
118117
const intl = useIntl()
119118
const location = useLocation()
120119
const {
@@ -194,10 +193,10 @@ const ProductView = forwardRef(
194193
const {scrollErrorIntoView = true} = opts
195194
// Validate that all attributes are selected before proceeding.
196195
const hasValidSelection = validateOrderability(variant, quantity, stockLevel)
197-
const showError = !isProductASet && !isProductABundle && !hasValidSelection
198-
const scrollToError = showError && scrollErrorIntoView
196+
const hasError = !isProductASet && !isProductABundle && !hasValidSelection
197+
const scrollToError = hasError && scrollErrorIntoView
199198

200-
toggleShowOptionsMessage(showError)
199+
toggleShowOptionsMessage(hasError)
201200

202201
if (scrollToError) {
203202
errorContainerRef.current.scrollIntoView({
@@ -242,13 +241,6 @@ const ProductView = forwardRef(
242241
})
243242
}
244243

245-
const showError = () => {
246-
toast({
247-
title: intl.formatMessage(API_ERROR_MESSAGE),
248-
type: 'error'
249-
})
250-
}
251-
252244
const handleCartItem = async () => {
253245
const hasValidSelection = validateAndShowError()
254246

packages/extension-chakra-storefront/src/pages/cart/utils/cart-utils.js renamed to packages/extension-chakra-storefront/src/hooks/use-errors.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77
import {useIntl} from 'react-intl'
8-
import useToast from '../../../hooks/use-toast'
9-
import {API_ERROR_MESSAGE} from '../../../constants'
8+
import useToast from './use-toast'
9+
import {API_ERROR_MESSAGE} from '../constants'
1010

1111
/**
12-
* Custom hook for cart error handling
12+
* Custom hook for error handling
1313
* @returns {Function} showError function
1414
*/
15-
export const useCartErrorHandler = () => {
15+
export const useErrorHandler = () => {
1616
const {formatMessage} = useIntl()
1717
const toast = useToast()
1818

packages/extension-chakra-storefront/src/pages/cart/utils/cart-utils.test.js renamed to packages/extension-chakra-storefront/src/hooks/use-errors.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
import React from 'react'
88
import {renderHook} from '@testing-library/react'
99
import {IntlProvider} from 'react-intl'
10-
import {useCartErrorHandler} from './cart-utils'
10+
import {useErrorHandler} from './use-errors'
1111
import PropTypes from 'prop-types'
1212

1313
// Mock the toast hook
1414
const mockToast = jest.fn()
15-
jest.mock('../../../hooks/use-toast', () => ({
15+
jest.mock('./use-toast', () => ({
1616
__esModule: true,
1717
default: () => mockToast
1818
}))
1919

2020
// Mock the constants
21-
jest.mock('../../../constants', () => ({
21+
jest.mock('../constants', () => ({
2222
API_ERROR_MESSAGE: {
2323
id: 'api.error.message',
2424
defaultMessage: 'Something went wrong. Please try again.'
@@ -37,21 +37,21 @@ const TestWrapper = ({children}) => {
3737
)
3838
}
3939

40-
describe('useCartErrorHandler', () => {
40+
describe('useErrorHandler', () => {
4141
beforeEach(() => {
4242
mockToast.mockClear()
4343
})
4444

4545
it('returns a showError function', () => {
46-
const {result} = renderHook(() => useCartErrorHandler(), {
46+
const {result} = renderHook(() => useErrorHandler(), {
4747
wrapper: TestWrapper
4848
})
4949

5050
expect(typeof result.current).toBe('function')
5151
})
5252

5353
it('calls toast with correct parameters when showError is invoked', () => {
54-
const {result} = renderHook(() => useCartErrorHandler(), {
54+
const {result} = renderHook(() => useErrorHandler(), {
5555
wrapper: TestWrapper
5656
})
5757

packages/extension-chakra-storefront/src/pages/account/addresses.jsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import PageActionPlaceHolder from '../../components/page-action-placeholder'
3434
import {useCurrentCustomer} from '../../hooks/use-current-customer'
3535
import {useShopperCustomersMutation} from '@salesforce/commerce-sdk-react'
3636
import {nanoid} from 'nanoid'
37-
import {API_ERROR_MESSAGE} from '../../constants'
37+
import {useErrorHandler} from '../../hooks/use-errors'
3838

3939
const DEFAULT_SKELETON_COUNT = 3
4040

@@ -147,6 +147,7 @@ const AccountAddresses = () => {
147147
const [selectedAddressId, setSelectedAddressId] = useState(false)
148148
const toast = useToast()
149149
const form = useForm()
150+
const showError = useErrorHandler()
150151

151152
const headingRef = useRef()
152153
useEffect(() => {
@@ -165,12 +166,6 @@ const AccountAddresses = () => {
165166
}, [addresses])
166167

167168
const hasAddresses = addresses?.length > 0
168-
const showError = () => {
169-
toast({
170-
title: formatMessage(API_ERROR_MESSAGE),
171-
type: 'error'
172-
})
173-
}
174169
const submitForm = async (address) => {
175170
try {
176171
let data

packages/extension-chakra-storefront/src/pages/cart/index.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {useCartOperations} from '../../hooks/cart/use-cart-operations'
1414
import {useCartWishlist} from '../../hooks/cart/use-cart-wishlist'
1515
import {useCartGiftItems} from '../../hooks/cart/use-cart-gift-items'
1616
import {useCartDefaultShipping} from '../../hooks/cart/use-cart-default-shipping'
17-
import {useCartErrorHandler} from './utils/cart-utils'
17+
import {useErrorHandler} from '../../hooks/use-errors'
1818

1919
// Cart Components
2020
import CartTitle from './partials/cart-title'
@@ -31,7 +31,7 @@ const Cart = () => {
3131
const {isRegistered} = customer || {}
3232

3333
// Error handling
34-
const showError = useCartErrorHandler()
34+
const showError = useErrorHandler()
3535

3636
// Product data and processing
3737
const {isProductsPending, productsByItemId} = useCartProducts(basket)

packages/extension-chakra-storefront/src/pages/checkout/partials/payment.jsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import PropTypes from 'prop-types'
99
import {defineMessage, FormattedMessage, useIntl} from 'react-intl'
1010
import {Box, Button, Checkbox, Container, Heading, Stack, Text, Separator} from '@chakra-ui/react'
1111
import {useForm} from 'react-hook-form'
12-
import useToast from '../../../hooks/use-toast'
1312
import {useShopperBasketsMutation} from '@salesforce/commerce-sdk-react'
1413
import {useCurrentBasket} from '../../../hooks/use-current-basket'
1514
import {useCheckout} from '../../../pages/checkout/util/checkout-context'
@@ -23,7 +22,7 @@ import PaymentForm from '../../../pages/checkout/partials/payment-form'
2322
import ShippingAddressSelection from '../../../pages/checkout/partials/shipping-address-selection'
2423
import AddressDisplay from '../../../components/address-display'
2524
import {PromoCode, usePromoCode} from '../../../components/promo-code'
26-
import {API_ERROR_MESSAGE} from '../../../constants'
25+
import {useErrorHandler} from '../../../hooks/use-errors'
2726

2827
const Payment = () => {
2928
const {formatMessage} = useIntl()
@@ -42,13 +41,7 @@ const Payment = () => {
4241
const {mutateAsync: removePaymentInstrumentFromBasket} = useShopperBasketsMutation(
4342
'removePaymentInstrumentFromBasket'
4443
)
45-
const toast = useToast()
46-
const showError = () => {
47-
toast({
48-
title: formatMessage(API_ERROR_MESSAGE),
49-
type: 'error'
50-
})
51-
}
44+
const showError = useErrorHandler()
5245

5346
const {step, STEPS, goToStep, goToNextStep} = useCheckout()
5447

packages/extension-chakra-storefront/src/pages/product-detail/use-product-detail-data.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
import React, {useCallback, useEffect, useState} from 'react'
9-
import {useIntl} from 'react-intl'
109
import {keepPreviousData} from '@tanstack/react-query'
1110
import {HTTPNotFound, HTTPError} from '@salesforce/pwa-kit-react-sdk/ssr/universal/errors'
1211

@@ -21,20 +20,18 @@ import {useHistory, useLocation, useParams} from 'react-router-dom'
2120

2221
import {useCurrentBasket, useVariant} from '../../hooks'
2322
import useEinstein from '../../hooks/use-einstein'
24-
import useToast from '../../hooks/use-toast'
2523
import {useProductDetailWishlist} from './use-product-detail-wishlist'
2624

2725
import {normalizeSetBundleProduct, getUpdateBundleChildArray} from '../../utils/product-utils'
26+
import {useErrorHandler} from '../../hooks/use-errors'
2827

29-
import {API_ERROR_MESSAGE} from '../../constants'
3028
import {rebuildPathWithParams} from '../../utils/url'
3129

3230
export const useProductDetailData = () => {
33-
const {formatMessage} = useIntl()
3431
const history = useHistory()
3532
const location = useLocation()
3633
const einstein = useEinstein()
37-
const toast = useToast()
34+
const showError = useErrorHandler()
3835
const {handleAddToWishlist, isWishlistLoading} = useProductDetailWishlist()
3936

4037
/****************************** Basket *********************************/
@@ -183,12 +180,6 @@ export const useProductDetailData = () => {
183180
}, [variant])
184181

185182
/**************** Add To Cart ****************/
186-
const showError = () => {
187-
toast({
188-
title: formatMessage(API_ERROR_MESSAGE),
189-
type: 'error'
190-
})
191-
}
192183

193184
const handleAddToCart = async (productSelectionValues) => {
194185
try {

packages/extension-chakra-storefront/src/pages/product-detail/use-product-detail-wishlist.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {useShopperCustomersMutation, useCustomerId} from '@salesforce/commerce-s
1212
import useNavigation from '../../hooks/use-navigation'
1313
import useToast from '../../hooks/use-toast'
1414
import {useWishList} from '../../hooks/use-wish-list'
15+
import {useErrorHandler} from '../../hooks/use-errors'
1516
import {
16-
API_ERROR_MESSAGE,
1717
TOAST_ACTION_VIEW_WISHLIST,
1818
TOAST_MESSAGE_ADDED_TO_WISHLIST,
1919
TOAST_MESSAGE_ALREADY_IN_WISHLIST
@@ -28,19 +28,13 @@ export const useProductDetailWishlist = () => {
2828
const navigate = useNavigation()
2929
const customerId = useCustomerId()
3030
const toast = useToast()
31+
const showError = useErrorHandler()
3132

3233
const {data: wishlist, isLoading: isWishlistLoading} = useWishList()
3334
const createCustomerProductListItem = useShopperCustomersMutation(
3435
'createCustomerProductListItem'
3536
)
3637

37-
const showError = () => {
38-
toast({
39-
title: formatMessage(API_ERROR_MESSAGE),
40-
type: 'error'
41-
})
42-
}
43-
4438
const handleAddToWishlist = (product, variant, quantity) => {
4539
const isItemInWishlist = wishlist?.customerProductListItems?.find(
4640
(i) => i.productId === variant?.productId || i.productId === product?.id

0 commit comments

Comments
 (0)