Skip to content

Commit

Permalink
fix: added ordering for saved payment methods (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArushKapoorJuspay authored Mar 12, 2024
1 parent 4b5654f commit a7e9f6d
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions src/PaymentElement.res
Original file line number Diff line number Diff line change
Expand Up @@ -49,39 +49,48 @@ let make = (
setLoadSavedCards: (PaymentType.savedCardsLoadState => PaymentType.savedCardsLoadState) => unit,
) = React.useState(_ => PaymentType.LoadingSavedCards)

React.useEffect1(() => {
switch customerPaymentMethods {
| LoadingSavedCards => ()
| LoadedSavedCards(savedCards, isGuestCustomer) => {
setSavedMethods(_ => savedCards)
React.useEffect2(() => {
switch (displaySavedPaymentMethodsCheckbox, customerPaymentMethods) {
| (false, _) => {
setShowFields(._ => true)
setLoadSavedCards(_ => LoadedSavedCards([], true))
}
| (_, LoadingSavedCards) => ()
| (_, LoadedSavedCards(savedPaymentMethods, isGuestCustomer)) => {
let defaultPaymentMethod =
savedPaymentMethods->Js.Array2.find(savedCard => savedCard.defaultPaymentMethodSet)

let savedCardsWithoutDefaultPaymentMethod =
savedPaymentMethods->Js.Array2.filter(savedCard => {
!savedCard.defaultPaymentMethodSet
})

let finalSavedPaymentMethods = switch defaultPaymentMethod {
| Some(defaultPaymentMethod) =>
[defaultPaymentMethod]->Js.Array2.concat(savedCardsWithoutDefaultPaymentMethod)
| None => savedCardsWithoutDefaultPaymentMethod
}

setSavedMethods(_ => finalSavedPaymentMethods)
setLoadSavedCards(_ =>
savedCards->Js.Array2.length == 0
finalSavedPaymentMethods->Js.Array2.length == 0
? NoResult(isGuestCustomer)
: LoadedSavedCards(savedCards, isGuestCustomer)
: LoadedSavedCards(finalSavedPaymentMethods, isGuestCustomer)
)
setShowFields(.prev => savedCards->Js.Array2.length == 0 || prev)
setShowFields(.prev => finalSavedPaymentMethods->Js.Array2.length == 0 || prev)
}
| NoResult(isGuestCustomer) => {
| (_, NoResult(isGuestCustomer)) => {
setLoadSavedCards(_ => NoResult(isGuestCustomer))
setShowFields(._ => true)
}
}

None
}, [customerPaymentMethods])

React.useEffect1(() => {
if displaySavedPaymentMethodsCheckbox {
setShowFields(._ => true)
setLoadSavedCards(_ => LoadedSavedCards([], true))
}
None
}, [displaySavedPaymentMethodsCheckbox])
}, (customerPaymentMethods, displaySavedPaymentMethodsCheckbox))

React.useEffect1(() => {
let defaultPaymentMethod =
savedMethods
->Js.Array2.find(savedMethod => savedMethod.defaultPaymentMethodSet)
savedMethods->Js.Array2.find(savedMethod => savedMethod.defaultPaymentMethodSet)

let isSavedMethodsEmpty = savedMethods->Js.Array2.length === 0

Expand Down

0 comments on commit a7e9f6d

Please sign in to comment.