Skip to content

Commit

Permalink
refactor: replace if statements with switch in cardError code (#771)
Browse files Browse the repository at this point in the history
Co-authored-by: Pritish Budhiraja <[email protected]>
  • Loading branch information
aritro2002 and PritishBudhiraja authored Nov 12, 2024
1 parent 6b53ca1 commit e505d0a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 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)
}
}
21 changes: 10 additions & 11 deletions src/Payment.res
Original file line number Diff line number Diff line change
Expand Up @@ -328,18 +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 if isCardSupported->Option.getOr(true) && isCardValid->Option.getOr(true) {
""
} else if isCardSupported->Option.getOr(true) {
localeString.inValidCardErrorText
} else {
switch cardNumber->CardUtils.getCardBrand {
| "" => localeString.inValidCardErrorText
| cardBrandValue => localeString.cardBrandConfiguredErrorText(cardBrandValue)
}
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 e505d0a

Please sign in to comment.