Skip to content
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

refactor: sort saved customer methods #427

Merged
merged 5 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -183,6 +184,7 @@ let defaultCustomerMethods = {
paymentMethodType: None,
defaultPaymentMethodSet: false,
requiresCvv: true,
lastUsedAt: "",
}
let defaultLayout = {
defaultCollapsed: false,
Expand Down Expand Up @@ -875,6 +877,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 @@ -897,6 +900,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) => {
PritishBudhiraja marked this conversation as resolved.
Show resolved Hide resolved
if a == b {
0.
} else if a > b {
-1.
} else {
1.
}
}

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