Skip to content

Commit cf008cb

Browse files
authored
Make mergeBasket Conditional More Robust (#1048)
* Update merge logic * Update CHANGELOG.md * Lint * PR feedback * Rename isNewlyRegisters to isNew for simplicity * Lint
1 parent c72bb52 commit cf008cb

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

packages/template-retail-react-app/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v2.8.0-dev (Mar 03, 2023)
2+
- Make `mergeBasket` conditional more robust [#1048](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/1048)
3+
14
## v2.7.0 (Mar 03, 2023)
25
- Add Page Designer ImageTile component [#967](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/967)
36
- Add Page Designer ImageWithText component [#991](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/991)

packages/template-retail-react-app/app/commerce-api/hooks/useCustomer.js

+16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import {useCommerceAPI, CustomerContext} from '../contexts'
1010

1111
const AuthTypes = Object.freeze({GUEST: 'guest', REGISTERED: 'registered'})
1212

13+
// This value represents the max age in milliseconds a customer can be before they are
14+
// no longer considered a "new" customer.
15+
// E.g. If a customers creation date is older than 2 seconds it will no longer be considered
16+
// a new customer.
17+
const NEW_CUSTOMER_MAX_AGE = 2 * 1000 // 2 seconds in milliseconds
18+
1319
export default function useCustomer() {
1420
const api = useCommerceAPI()
1521
const {customer, setCustomer} = useContext(CustomerContext)
@@ -39,6 +45,16 @@ export default function useCustomer() {
3945
return customer?.authType === AuthTypes.GUEST
4046
},
4147

48+
/**
49+
* Returns if this customer is newly registered.
50+
*/
51+
get isNew() {
52+
if (!customer || customer.authType !== 'registered') return false
53+
const lastLoginTimeStamp = Date.parse(customer.lastLoginTime)
54+
const creationTimeStamp = Date.parse(customer.creationDate)
55+
return lastLoginTimeStamp - creationTimeStamp < NEW_CUSTOMER_MAX_AGE
56+
},
57+
4258
/** Returns the customer's saved addresses with the 'preferred' address in the first index */
4359
get addresses() {
4460
// TODO: This performs array manipulation every time it is accessed; should it be

packages/template-retail-react-app/app/commerce-api/hooks/useShopper.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: BSD-3-Clause
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
7-
import {useEffect} from 'react'
7+
import {useEffect, useRef} from 'react'
88
import useBasket from './useBasket'
99
import useCustomer from './useCustomer'
1010

@@ -17,6 +17,7 @@ const useShopper = (opts = {}) => {
1717
const {currency} = opts
1818
const customer = useCustomer()
1919
const basket = useBasket({currency})
20+
const prevAuthType = useRef()
2021

2122
// Create or restore the user session upon mounting
2223
useEffect(() => {
@@ -58,11 +59,22 @@ const useShopper = (opts = {}) => {
5859
}
5960
}, [customer.authType, basket.loaded])
6061

61-
// Call merge basket whenever user type changes from guest to registered
62+
// Call merge basket.
6263
useEffect(() => {
63-
if (customer.authType === 'registered') {
64+
// Only call merge when there are items in the guest basket and you are
65+
// a returning customer.
66+
const shouldMerge =
67+
customer.authType === 'registered' &&
68+
prevAuthType.current === 'guest' &&
69+
!customer.isNew &&
70+
basket.itemCount > 0
71+
72+
if (shouldMerge) {
6473
basket.mergeBasket()
6574
}
75+
76+
// Update the current `authType` value.
77+
prevAuthType.current = customer.authType
6678
}, [customer.authType])
6779

6880
useEffect(() => {

0 commit comments

Comments
 (0)