Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make mergeBasket Conditional More Robust #1048

Merged
merged 7 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/template-retail-react-app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v2.8.0-dev (Mar 03, 2023)
- Make `mergeBasket` conditional more robust [#1048](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/1048)

## v2.7.0 (Mar 03, 2023)
- Add Page Designer ImageTile component [#967](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/967)
- Add Page Designer ImageWithText component [#991](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/991)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import {useCommerceAPI, CustomerContext} from '../contexts'

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

// This value represents the max age in milliseconds a customer can be before they are
// no longer considered a "new" customer.
// E.g. If a customers creation date is older than 2 seconds it will no longer be considered
// a new customer.
const NEW_CUSTOMER_MAX_AGE = 2 * 1000 // 2 seconds in milliseconds

export default function useCustomer() {
const api = useCommerceAPI()
const {customer, setCustomer} = useContext(CustomerContext)
Expand Down Expand Up @@ -39,6 +45,16 @@ export default function useCustomer() {
return customer?.authType === AuthTypes.GUEST
},

/**
* Returns if this customer is newly registered.
*/
get isNew() {
if (!customer || customer.authType !== 'registered') return false
const lastLoginTimeStamp = Date.parse(customer.lastLoginTime)
const creationTimeStamp = Date.parse(customer.creationDate)
return lastLoginTimeStamp - creationTimeStamp < NEW_CUSTOMER_MAX_AGE
},

/** Returns the customer's saved addresses with the 'preferred' address in the first index */
get addresses() {
// TODO: This performs array manipulation every time it is accessed; should it be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {useEffect} from 'react'
import {useEffect, useRef} from 'react'
import useBasket from './useBasket'
import useCustomer from './useCustomer'

Expand All @@ -17,6 +17,7 @@ const useShopper = (opts = {}) => {
const {currency} = opts
const customer = useCustomer()
const basket = useBasket({currency})
const prevAuthType = useRef()

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

// Call merge basket whenever user type changes from guest to registered
// Call merge basket.
useEffect(() => {
if (customer.authType === 'registered') {
// Only call merge when there are items in the guest basket and you are
// a returning customer.
const shouldMerge =
customer.authType === 'registered' &&
prevAuthType.current === 'guest' &&
!customer.isNew &&
basket.itemCount > 0

if (shouldMerge) {
basket.mergeBasket()
}

// Update the current `authType` value.
prevAuthType.current = customer.authType
}, [customer.authType])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}, [customer.authType])
}, [customer.authType, customer.isNewlyRegistered, basket.itemCount])

Are these needed? (React hooks are still confusing!)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. If we added those values it just means that the hooks didUpdate logic would be run when those addition values change, which isn't necessarily what we want to be doing. I know the hook with the guard in it is ugly, there are alternatives but they are equally unsightly.


useEffect(() => {
Expand Down