Skip to content

Commit

Permalink
chore: add optimised code using switch
Browse files Browse the repository at this point in the history
  • Loading branch information
aritro2002 committed Nov 8, 2024
1 parent cf23ead commit 7b095d2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
10 changes: 10 additions & 0 deletions src/CardUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -675,3 +675,13 @@ let getEligibleCoBadgedCardSchemes = (~matchedCardSchemes, ~enabledCardSchemes)
let getCardBrandFromStates = (cardBrand, cardScheme, showFields) => {
!showFields ? cardScheme : cardBrand
}

let getCardBrandInvalidError = (
~cardNumber,
~localeString: OrcaPaymentPage.LocaleStringTypes.localeStrings,
) => {
switch cardNumber->getCardBrand {
| "" => localeString.enterValidCardNumberErrorText
| cardBrandValue => localeString.cardBrandConfiguredErrorText(cardBrandValue)
}
}
28 changes: 10 additions & 18 deletions src/Payment.res
Original file line number Diff line number Diff line change
Expand Up @@ -328,25 +328,17 @@ let make = (~paymentMode, ~integrateError, ~logger) => {
}, (cardNumber, cvcNumber, cardExpiry, isCVCValid, isExpiryValid, isCardValid))

React.useEffect(() => {
let cardError = if isCardValid == None || cardNumber->String.length == 0 {
""
} else {
let isCardSupportedValue = isCardSupported->Option.getOr(true)
let isCardValidValue = isCardValid->Option.getOr(false)

if isCardSupportedValue && isCardValidValue {
""
} else if isCardSupportedValue {
localeString.inValidCardErrorText
} else {
let cardBrand = cardNumber->CardUtils.getCardBrand
if cardBrand == "" {
localeString.enterValidCardNumberErrorText
} else {
localeString.cardBrandConfiguredErrorText(cardBrand)
}
}
let cardError = switch (
isCardSupported->Option.getOr(true),
isCardValid->Option.getOr(true),
cardNumber->String.length == 0,
) {
| (_, _, true) => ""
| (true, true, _) => ""
| (true, _, _) => localeString.inValidCardErrorText
| (_, _, _) => CardUtils.getCardBrandInvalidError(~cardNumber, ~localeString)
}
let cardError = isCardValid->Option.isSome ? cardError : ""
setCardError(_ => cardError)
None
}, [isCardValid, isCardSupported])
Expand Down

0 comments on commit 7b095d2

Please sign in to comment.