From 7b095d264f3aa6e23afdd3d975e0b44ac1307635 Mon Sep 17 00:00:00 2001 From: "aritro.ghosh" <aritro.ghosh@juspay.in> Date: Fri, 8 Nov 2024 17:32:52 +0530 Subject: [PATCH] chore: add optimised code using switch --- src/CardUtils.res | 10 ++++++++++ src/Payment.res | 28 ++++++++++------------------ 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/CardUtils.res b/src/CardUtils.res index e917851b..9a2453df 100644 --- a/src/CardUtils.res +++ b/src/CardUtils.res @@ -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) + } +} diff --git a/src/Payment.res b/src/Payment.res index 9bd39436..250dfe3a 100644 --- a/src/Payment.res +++ b/src/Payment.res @@ -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])