Skip to content

Commit

Permalink
fix: default selected pm issue
Browse files Browse the repository at this point in the history
  • Loading branch information
PritishBudhiraja committed Jun 14, 2024
1 parent 28865dc commit 90bb65d
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/PaymentElement.res
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,18 @@ let make = (~cardProps, ~expiryProps, ~cvcProps, ~paymentType: CardThemeType.mod
!savedCard.defaultPaymentMethodSet
})

let finalSavedPaymentMethods = switch defaultPaymentMethod {
| Some(defaultPaymentMethod) =>
[defaultPaymentMethod]->Array.concat(savedCardsWithoutDefaultPaymentMethod)
| None => savedCardsWithoutDefaultPaymentMethod
let finalSavedPaymentMethods = if optionAtomValue.displayDefaultSavedPaymentIcon {
switch defaultPaymentMethod {
| Some(defaultPaymentMethod) =>
[defaultPaymentMethod]->Array.concat(savedCardsWithoutDefaultPaymentMethod)
| None => savedCardsWithoutDefaultPaymentMethod
}
} else {
savedPaymentMethods->Array.toSorted((a, b) =>
compareLogic(Date.fromString(a.lastUsedAt), Date.fromString(b.lastUsedAt))
)
}

finalSavedPaymentMethods->Array.sort((a, b) =>
compareLogic(Date.fromString(a.lastUsedAt), Date.fromString(b.lastUsedAt))
)

setSavedMethods(_ => finalSavedPaymentMethods)
setLoadSavedCards(_ =>
finalSavedPaymentMethods->Array.length == 0
Expand All @@ -88,13 +90,14 @@ let make = (~cardProps, ~expiryProps, ~cvcProps, ~paymentType: CardThemeType.mod
}, (customerPaymentMethods, displaySavedPaymentMethods))

React.useEffect(() => {
let defaultPaymentMethod =
savedMethods->Array.find(savedMethod => savedMethod.defaultPaymentMethodSet)
let defaultSelectedPaymentMethod = optionAtomValue.displayDefaultSavedPaymentIcon
? savedMethods->Array.find(savedMethod => savedMethod.defaultPaymentMethodSet)
: savedMethods->Array.get(0)

let isSavedMethodsEmpty = savedMethods->Array.length === 0

let tokenObj = switch (isSavedMethodsEmpty, defaultPaymentMethod) {
| (false, Some(defaultPaymentMethod)) => Some(defaultPaymentMethod)
let tokenObj = switch (isSavedMethodsEmpty, defaultSelectedPaymentMethod) {
| (false, Some(defaultSelectedPaymentMethod)) => Some(defaultSelectedPaymentMethod)
| (false, None) => Some(savedMethods->Array.get(0)->Option.getOr(defaultCustomerMethods))
| _ => None
}
Expand Down

0 comments on commit 90bb65d

Please sign in to comment.