Skip to content

Commit 9fa4e45

Browse files
committed
W-20975340: Addressing comments
1 parent 15d8ca8 commit 9fa4e45

File tree

5 files changed

+227
-288
lines changed

5 files changed

+227
-288
lines changed

packages/template-retail-react-app/app/hooks/use-current-customer.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ import {useCustomer, useCustomerId, useCustomerType} from '@salesforce/commerce-
99

1010
/**
1111
* A hook that returns the current customer.
12-
*
12+
* @param {Array<string>} [expand] - Optional array of fields to expand in the customer query
1313
*/
14-
export const useCurrentCustomer = () => {
14+
export const useCurrentCustomer = (expand = undefined) => {
1515
const customerId = useCustomerId()
1616
const {isRegistered, isGuest, customerType} = useCustomerType()
17-
const query = useCustomer({parameters: {customerId}}, {enabled: !!customerId && isRegistered})
17+
const parameters = {
18+
customerId,
19+
...(expand && {expand})
20+
}
21+
const query = useCustomer({parameters}, {enabled: !!customerId && isRegistered})
1822
const value = {
1923
...query,
2024
data: {

packages/template-retail-react-app/app/pages/checkout/partials/sf-payments-sheet.jsx

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,9 @@ import {
2020
Divider
2121
} from '@salesforce/retail-react-app/app/components/shared/ui'
2222
import {useForm} from 'react-hook-form'
23-
import {
24-
useShopperBasketsMutation,
25-
useCustomer,
26-
useCustomerId,
27-
useCustomerType
28-
} from '@salesforce/commerce-sdk-react'
23+
import {useShopperBasketsMutation} from '@salesforce/commerce-sdk-react'
2924
import {useCurrentBasket} from '@salesforce/retail-react-app/app/hooks/use-current-basket'
25+
import {useCurrentCustomer} from '@salesforce/retail-react-app/app/hooks/use-current-customer'
3026
import {useCurrency} from '@salesforce/retail-react-app/app/hooks/use-currency'
3127
import {useCheckout} from '@salesforce/retail-react-app/app/pages/checkout/util/checkout-context'
3228
import {usePaymentConfiguration} from '@salesforce/commerce-sdk-react'
@@ -64,25 +60,7 @@ const SFPaymentsSheet = forwardRef((props, ref) => {
6460
const navigate = useNavigation()
6561

6662
const {data: basket} = useCurrentBasket()
67-
const customerId = useCustomerId()
68-
const {isRegistered} = useCustomerType()
69-
const {data: customerData} = useCustomer(
70-
{
71-
parameters: {
72-
customerId,
73-
expand: ['paymentmethodreferences']
74-
}
75-
},
76-
{enabled: !!customerId && isRegistered}
77-
)
78-
// Add customerId and isRegistered to customer data for consistency with useCurrentCustomer
79-
const customer = customerData
80-
? {
81-
...customerData,
82-
customerId,
83-
isRegistered
84-
}
85-
: null
63+
const {data: customer} = useCurrentCustomer(['paymentmethodreferences'])
8664

8765
const isPickupOnly =
8866
basket?.shipments?.length > 0 &&
@@ -505,20 +483,16 @@ const SFPaymentsSheet = forwardRef((props, ref) => {
505483
}))
506484

507485
const savedPaymentMethods = useMemo(
508-
() =>
509-
transformPaymentMethodReferences(
510-
customer?.paymentMethodReferences,
511-
paymentConfig?.paymentMethodSetAccounts
512-
),
513-
[customer?.paymentMethodReferences, paymentConfig?.paymentMethodSetAccounts]
486+
() => transformPaymentMethodReferences(customer, paymentConfig),
487+
[customer, paymentConfig]
514488
)
515489

516490
useEffect(() => {
517491
if (sfp && metadata && containerElementRef.current && paymentConfig) {
518492
const paymentMethodSetAccounts = (paymentConfig.paymentMethodSetAccounts || []).map(
519493
(account) => ({
520494
...account,
521-
gatewayId: account.gatewayId || account.accountId
495+
gatewayId: account.accountId
522496
})
523497
)
524498

packages/template-retail-react-app/app/pages/checkout/partials/sf-payments-sheet.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,7 @@ describe('SFPaymentsSheet', () => {
10131013
const ref = React.createRef()
10141014
setupConfirmPaymentMocks()
10151015

1016+
// eslint-disable-next-line @typescript-eslint/no-var-requires
10161017
const useShopperConfigurationModule = require('@salesforce/retail-react-app/app/hooks/use-shopper-configuration')
10171018
const originalMock = useShopperConfigurationModule.useShopperConfiguration
10181019

@@ -1154,6 +1155,7 @@ describe('SFPaymentsSheet', () => {
11541155
]
11551156

11561157
jest.spyOn(
1158+
// eslint-disable-next-line @typescript-eslint/no-var-requires
11571159
require('@salesforce/commerce-sdk-react'),
11581160
'usePaymentConfiguration'
11591161
).mockReturnValue({

packages/template-retail-react-app/app/utils/sf-payments-utils.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,14 @@ export const createPaymentInstrumentBody = ({
276276

277277
/**
278278
* Transforms payment method references from API format to SF Payments SDK format.
279-
* @param {Array} paymentMethodReferences - Array of payment method references
280-
* @param {Array} paymentMethodSetAccounts - Array of payment method set accounts
279+
* @param {Object} customer - Customer object with paymentMethodReferences property
280+
* @param {Object} paymentConfig - Payment configuration object with paymentMethodSetAccounts property
281281
* @returns {Array} Transformed payment method references for SF Payments SDK
282282
*/
283-
export const transformPaymentMethodReferences = (
284-
paymentMethodReferences,
285-
paymentMethodSetAccounts = []
286-
) => {
283+
export const transformPaymentMethodReferences = (customer, paymentConfig) => {
284+
const paymentMethodReferences = customer?.paymentMethodReferences
285+
const paymentMethodSetAccounts = paymentConfig?.paymentMethodSetAccounts || []
286+
287287
if (!Array.isArray(paymentMethodReferences) || !Array.isArray(paymentMethodSetAccounts)) {
288288
return []
289289
}
@@ -316,7 +316,7 @@ export const transformPaymentMethodReferences = (
316316
return null
317317
}
318318

319-
const gatewayId = matchingAccount.gatewayId || matchingAccount.accountId
319+
const gatewayId = matchingAccount.accountId
320320

321321
if (!gatewayId || typeof gatewayId !== 'string') {
322322
return null

0 commit comments

Comments
 (0)