@@ -21,7 +21,11 @@ import {
2121 useAuthHelper ,
2222 AuthHelpers ,
2323 useShopperBasketsMutation ,
24- useShopperOrdersMutation
24+ useShopperOrdersMutation ,
25+ useShopperCustomersMutation ,
26+ ShopperCustomersMutations ,
27+ ShopperBasketsMutations ,
28+ ShopperOrdersMutations
2529} from '@salesforce/commerce-sdk-react'
2630import { useToast } from '@salesforce/retail-react-app/app/hooks/use-toast'
2731import useNavigation from '@salesforce/retail-react-app/app/hooks/use-navigation'
@@ -43,6 +47,7 @@ import {
4347 getMaskCreditCardNumber
4448} from '@salesforce/retail-react-app/app/utils/cc-utils'
4549import { generatePassword } from '@salesforce/retail-react-app/app/utils/password-utils'
50+ import { nanoid } from 'nanoid'
4651
4752const CheckoutOneClick = ( ) => {
4853 const { formatMessage} = useIntl ( )
@@ -71,13 +76,16 @@ const CheckoutOneClick = () => {
7176 const appliedPayment = basket ?. paymentInstruments && basket ?. paymentInstruments [ 0 ]
7277
7378 const { mutateAsync : addPaymentInstrumentToBasket } = useShopperBasketsMutation (
74- 'addPaymentInstrumentToBasket'
79+ ShopperBasketsMutations . AddPaymentInstrumentToBasket
7580 )
7681 const { mutateAsync : updateBillingAddressForBasket } = useShopperBasketsMutation (
77- 'updateBillingAddressForBasket'
82+ ShopperBasketsMutations . UpdateBillingAddressForBasket
7883 )
79- const { mutateAsync : createOrder } = useShopperOrdersMutation ( 'createOrder' )
84+ const { mutateAsync : createOrder } = useShopperOrdersMutation ( ShopperOrdersMutations . CreateOrder )
8085 const { mutateAsync : register } = useAuthHelper ( AuthHelpers . Register )
86+ const { mutateAsync : createCustomerAddress } = useShopperCustomersMutation (
87+ ShopperCustomersMutations . CreateCustomerAddress
88+ )
8189
8290 const showError = ( message ) => {
8391 showToast ( {
@@ -141,6 +149,17 @@ const CheckoutOneClick = () => {
141149 }
142150
143151 const submitOrder = async ( ) => {
152+ const saveShippingAddress = async ( customerId , address ) => {
153+ try {
154+ await createCustomerAddress ( {
155+ body : address ,
156+ parameters : { customerId : customerId }
157+ } )
158+ } catch ( error ) {
159+ // Fail silently
160+ }
161+ }
162+
144163 const registerUser = async ( data ) => {
145164 try {
146165 const body = {
@@ -153,7 +172,10 @@ const CheckoutOneClick = () => {
153172 } ,
154173 password : generatePassword ( )
155174 }
156- await register ( body )
175+ const customer = await register ( body )
176+
177+ // Save the shipping address from this order, should not block account creation
178+ await saveShippingAddress ( customer . customerId , data . address )
157179
158180 showToast ( {
159181 variant : 'subtle' ,
@@ -197,11 +219,16 @@ const CheckoutOneClick = () => {
197219 } )
198220
199221 if ( enableUserRegistration ) {
222+ // Remove the id property from the address
223+ const { id, ...address } = order . shipments [ 0 ] . shippingAddress
224+ address . addressId = nanoid ( )
225+
200226 await registerUser ( {
201227 firstName : order . billingAddress . firstName ,
202228 lastName : order . billingAddress . lastName ,
203229 email : order . customerInfo . email ,
204- phoneHome : order . billingAddress . phone
230+ phoneHome : order . billingAddress . phone ,
231+ address : address
205232 } )
206233 }
207234
0 commit comments