Skip to content

Commit

Permalink
refactor: sort saved customer methods (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
PritishBudhiraja authored Jun 13, 2024
1 parent 4745fa2 commit d923d7f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/PaymentElement.res
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ let make = (~cardProps, ~expiryProps, ~cvcProps, ~paymentType: CardThemeType.mod
| None => savedCardsWithoutDefaultPaymentMethod
}

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

setSavedMethods(_ => finalSavedPaymentMethods)
setLoadSavedCards(_ =>
finalSavedPaymentMethods->Array.length == 0
Expand Down
4 changes: 4 additions & 0 deletions src/Types/PaymentType.res
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ type customerMethods = {
paymentMethodType: option<string>,
defaultPaymentMethodSet: bool,
requiresCvv: bool,
lastUsedAt: string,
}
type savedCardsLoadState =
LoadingSavedCards | LoadedSavedCards(array<customerMethods>, bool) | NoResult(bool)
Expand Down Expand Up @@ -184,6 +185,7 @@ let defaultCustomerMethods = {
paymentMethodType: None,
defaultPaymentMethodSet: false,
requiresCvv: true,
lastUsedAt: "",
}
let defaultLayout = {
defaultCollapsed: false,
Expand Down Expand Up @@ -877,6 +879,7 @@ let createCustomerObjArr = dict => {
paymentMethodType: getPaymentMethodType(dict),
defaultPaymentMethodSet: getBool(dict, "default_payment_method_set", false),
requiresCvv: getBool(dict, "requires_cvv", true),
lastUsedAt: getString(dict, "last_used_at", ""),
}
})
LoadedSavedCards(customerPaymentMethods, isGuestCustomer)
Expand All @@ -899,6 +902,7 @@ let getCustomerMethods = (dict, str) => {
paymentMethodType: getPaymentMethodType(dict),
defaultPaymentMethodSet: getBool(dict, "default_payment_method_set", false),
requiresCvv: getBool(dict, "requires_cvv", true),
lastUsedAt: getString(dict, "last_used_at", ""),
}
})
LoadedSavedCards(customerPaymentMethods, false)
Expand Down
10 changes: 10 additions & 0 deletions src/Utilities/Utils.res
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,16 @@ let getStateNameFromStateCodeAndCountry = (list: JSON.t, stateCode: string, coun

let removeHyphen = str => str->String.replaceRegExp(%re("/-/g"), "")

let compareLogic = (a, b) => {
if a == b {
0.
} else if a > b {
-1.
} else {
1.
}
}

let currencyNetworksDict =
[
("BTC", ["bitcoin", "bnb_smart_chain"]),
Expand Down

0 comments on commit d923d7f

Please sign in to comment.