-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Checkout: Convert useCreateExistingCards to TypeScript #50723
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8065c0a
Move useCreatePaymentMethods to a directory
sirbrillig 7521e51
Update all imports to use-create-payment-methods
sirbrillig ce586a2
Move useMemoCompare hook to own file
sirbrillig c02d5d1
Update PaymentMethod type to make nodes optional
sirbrillig fbb73b8
Move useCreateExistingCards to own file
sirbrillig 6111512
Fix payment-methods imports
sirbrillig 4abfa38
Only pass string to activePayButtonText
sirbrillig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...checkout/composite-checkout/hooks/use-create-payment-methods/use-create-existing-cards.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useMemo } from 'react'; | ||
import { createExistingCardMethod } from '@automattic/composite-checkout'; | ||
import type { PaymentMethod } from '@automattic/composite-checkout'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useMemoCompare from '../use-memo-compare'; | ||
import type { StoredCard } from '../../types/stored-cards'; | ||
|
||
export default function useCreateExistingCards( { | ||
storedCards, | ||
activePayButtonText = undefined, | ||
}: { | ||
storedCards: StoredCard[]; | ||
activePayButtonText?: string; | ||
} ): PaymentMethod[] { | ||
// Memoize the cards by comparing their stored_details_id values, in case the | ||
// objects themselves are recreated on each render. | ||
const memoizedStoredCards: StoredCard[] | undefined = useMemoCompare( | ||
storedCards, | ||
( prev: undefined | StoredCard[], next: undefined | StoredCard[] ) => { | ||
const prevIds = prev?.map( ( card ) => card.stored_details_id ) ?? []; | ||
const nextIds = next?.map( ( card ) => card.stored_details_id ) ?? []; | ||
return ( | ||
prevIds.length === nextIds.length && | ||
prevIds.every( ( id, index ) => id === nextIds[ index ] ) | ||
); | ||
} | ||
); | ||
|
||
const existingCardMethods = useMemo( () => { | ||
return ( | ||
memoizedStoredCards?.map( ( storedDetails ) => | ||
createExistingCardMethod( { | ||
id: `existingCard-${ storedDetails.stored_details_id }`, | ||
cardholderName: storedDetails.name, | ||
cardExpiry: storedDetails.expiry, | ||
brand: storedDetails.card_type, | ||
last4: storedDetails.card, | ||
storedDetailsId: storedDetails.stored_details_id, | ||
paymentMethodToken: storedDetails.mp_ref, | ||
paymentPartnerProcessorId: storedDetails.payment_partner, | ||
activePayButtonText, | ||
} ) | ||
) ?? [] | ||
); | ||
}, [ memoizedStoredCards, activePayButtonText ] ); | ||
return existingCardMethods; | ||
} |
30 changes: 30 additions & 0 deletions
30
client/my-sites/checkout/composite-checkout/hooks/use-memo-compare.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useRef, useEffect } from 'react'; | ||
|
||
// See https://usehooks.com/useMemoCompare/ | ||
export default function useMemoCompare< A, B >( | ||
next: B, | ||
compare: ( previous: A | B | undefined, next: B ) => boolean | ||
): A | B | undefined { | ||
// Ref for storing previous value | ||
const previousRef = useRef< undefined | A | B >(); | ||
const previous = previousRef.current; | ||
|
||
// Pass previous and next value to compare function | ||
// to determine whether to consider them equal. | ||
const isEqual = compare( previous, next ); | ||
|
||
// If not equal update previousRef to next value. | ||
// We only update if not equal so that this hook continues to return | ||
// the same old value if compare keeps returning true. | ||
useEffect( () => { | ||
if ( ! isEqual ) { | ||
previousRef.current = next; | ||
} | ||
} ); | ||
|
||
// Finally, if equal then return the previous value | ||
return isEqual ? previous : next; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This seems general purpose enough to warrant being put in a less specific place, but I'm not sure where that would be.
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.
Yeah, same. There's a few things in this directory and
lib
that fall into a general utility category but I figure we can always move them later.