File tree 3 files changed +34
-3
lines changed
packages/template-retail-react-app
3 files changed +34
-3
lines changed Original file line number Diff line number Diff line change
1
+ ## v2.8.0-dev (Mar 03, 2023)
2
+ - Make ` mergeBasket ` conditional more robust [ #1048 ] ( https://github.com/SalesforceCommerceCloud/pwa-kit/pull/1048 )
3
+
1
4
## v2.7.0 (Mar 03, 2023)
2
5
- Add Page Designer ImageTile component [ #967 ] ( https://github.com/SalesforceCommerceCloud/pwa-kit/pull/967 )
3
6
- Add Page Designer ImageWithText component [ #991 ] ( https://github.com/SalesforceCommerceCloud/pwa-kit/pull/991 )
Original file line number Diff line number Diff line change @@ -10,6 +10,12 @@ import {useCommerceAPI, CustomerContext} from '../contexts'
10
10
11
11
const AuthTypes = Object . freeze ( { GUEST : 'guest' , REGISTERED : 'registered' } )
12
12
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
+
13
19
export default function useCustomer ( ) {
14
20
const api = useCommerceAPI ( )
15
21
const { customer, setCustomer} = useContext ( CustomerContext )
@@ -39,6 +45,16 @@ export default function useCustomer() {
39
45
return customer ?. authType === AuthTypes . GUEST
40
46
} ,
41
47
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
+
42
58
/** Returns the customer's saved addresses with the 'preferred' address in the first index */
43
59
get addresses ( ) {
44
60
// TODO: This performs array manipulation every time it is accessed; should it be
Original file line number Diff line number Diff line change 4
4
* SPDX-License-Identifier: BSD-3-Clause
5
5
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6
6
*/
7
- import { useEffect } from 'react'
7
+ import { useEffect , useRef } from 'react'
8
8
import useBasket from './useBasket'
9
9
import useCustomer from './useCustomer'
10
10
@@ -17,6 +17,7 @@ const useShopper = (opts = {}) => {
17
17
const { currency} = opts
18
18
const customer = useCustomer ( )
19
19
const basket = useBasket ( { currency} )
20
+ const prevAuthType = useRef ( )
20
21
21
22
// Create or restore the user session upon mounting
22
23
useEffect ( ( ) => {
@@ -58,11 +59,22 @@ const useShopper = (opts = {}) => {
58
59
}
59
60
} , [ customer . authType , basket . loaded ] )
60
61
61
- // Call merge basket whenever user type changes from guest to registered
62
+ // Call merge basket.
62
63
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 ) {
64
73
basket . mergeBasket ( )
65
74
}
75
+
76
+ // Update the current `authType` value.
77
+ prevAuthType . current = customer . authType
66
78
} , [ customer . authType ] )
67
79
68
80
useEffect ( ( ) => {
You can’t perform that action at this time.
0 commit comments