-
Notifications
You must be signed in to change notification settings - Fork 142
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
Changes from 3 commits
97b589e
56b61c3
d2c11a0
57958a8
d6c475f
f6e73f4
0b1e37a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -10,6 +10,8 @@ import {useCommerceAPI, CustomerContext} from '../contexts' | |||||||||
|
||||||||||
const AuthTypes = Object.freeze({GUEST: 'guest', REGISTERED: 'registered'}) | ||||||||||
|
||||||||||
const REGISTRATION_GRACE_PERIOD = 2 * 1000 // 2 seconds in milliseconds | ||||||||||
|
||||||||||
export default function useCustomer() { | ||||||||||
const api = useCommerceAPI() | ||||||||||
const {customer, setCustomer} = useContext(CustomerContext) | ||||||||||
|
@@ -39,6 +41,16 @@ export default function useCustomer() { | |||||||||
return customer?.authType === AuthTypes.GUEST | ||||||||||
}, | ||||||||||
|
||||||||||
/** | ||||||||||
* Returns if this customer is newly registered. | ||||||||||
*/ | ||||||||||
get isNewlyRegistered() { | ||||||||||
if (!customer || customer.authType !== 'registered') return false | ||||||||||
const lastLoginTimeDate = new Date(Date.parse(customer.lastLoginTime)) | ||||||||||
const creationDate = new Date(Date.parse(customer.creationDate)) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Don't convert from number to Date if all you're doing is calculating a number. 😉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True. I'll fix this outside of this suggestion as my OCD is telling me to rename those variables as they would no longer be Date but rather TimeStamps. |
||||||||||
return lastLoginTimeDate - creationDate < REGISTRATION_GRACE_PERIOD | ||||||||||
}, | ||||||||||
|
||||||||||
/** Returns the customer's saved addresses with the 'preferred' address in the first index */ | ||||||||||
get addresses() { | ||||||||||
if (!customer?.addresses) { | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -58,9 +58,16 @@ 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' && | ||||||
!customer.isNewlyRegistered && | ||||||
basket.itemCount > 0 | ||||||
|
||||||
if (shouldMerge) { | ||||||
basket.mergeBasket() | ||||||
} | ||||||
}, [customer.authType]) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Are these needed? (React hooks are still confusing!) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. If we added those values it just means that the hooks |
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Grace period" is confusing - is this just a workaround for "just registered and logged in"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this is a work around for determining if a user has just registered. Unfortunately there isn't a property on the customer object that tells you that directly. There is a host of data type values, but they unfortunately cant be used to derive whether or not a user is new.
I was struggling with that name, I'm open to suggestions on other names, or solutions.