Skip to content

Commit

Permalink
refactor: card scheme filtering logic update
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanskar2001 committed Sep 4, 2024
1 parent d607d9e commit 9dd637b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
29 changes: 23 additions & 6 deletions src/CardSchemeComponent.res
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module CoBadgeCardSchemeDropDown = {
@react.component
let make = (~cardNumber, ~setCardBrand) => {
let make = (~eligibleCardSchemes, ~setCardBrand) => {
<select
className="w-4"
onChange={ev => {
Expand All @@ -9,8 +9,7 @@ module CoBadgeCardSchemeDropDown = {
setCardBrand(_ => value)
}}>
<option disabled=true> {"Select a card brand"->React.string} </option>
{cardNumber
->CardUtils.getCoBadgesCardSchemes
{eligibleCardSchemes
->Array.mapWithIndex((item, i) => {
<option key={Int.toString(i)} value=item className="opacity-0 w-0 h-0">
{item->React.string}
Expand All @@ -22,18 +21,36 @@ module CoBadgeCardSchemeDropDown = {
}

@react.component
let make = (~cardNumber, ~paymentType, ~cardBrand, ~setCardBrand, ~isCardCoBadged=false) => {
let make = (~cardNumber, ~paymentType, ~cardBrand, ~setCardBrand) => {
let cardType = React.useMemo1(_ => cardBrand->CardUtils.getCardType, [cardBrand])
let animate = cardType == NOTFOUND ? "animate-slideLeft" : "animate-slideRight"

let isCardCoBadged = Recoil.useRecoilValueFromAtom(RecoilAtoms.isCardCoBadged)
let setIsCardCoBadged = Recoil.useSetRecoilState(RecoilAtoms.isCardCoBadged)
let cardBrandIcon = React.useMemo1(
_ => CardUtils.getCardBrandIcon(cardType, paymentType),
[cardBrand],
)

let paymentMethodListValue = Recoil.useRecoilValueFromAtom(PaymentUtils.paymentMethodListValue)
let enabledCardSchemes =
paymentMethodListValue->PaymentUtils.getSupportedCardBrands->Option.getOr([])

let matchedCardSchemes = cardNumber->CardUtils.getAllMatchedCardSchemes

let eligibleCardSchemes = CardUtils.getEligibleCoBadgedCardSchemes(
~matchedCardSchemes,
~enabledCardSchemes,
)

React.useEffect1(() => {
setIsCardCoBadged(_ => eligibleCardSchemes->Array.length > 1)
None
}, [[eligibleCardSchemes]])

<div className={`${animate} flex items-center`}>
cardBrandIcon
<RenderIf condition={isCardCoBadged}>
<CoBadgeCardSchemeDropDown cardNumber setCardBrand />
<CoBadgeCardSchemeDropDown eligibleCardSchemes setCardBrand />
</RenderIf>
</div>
}
8 changes: 7 additions & 1 deletion src/CardUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -653,11 +653,17 @@ let getPaymentMethodBrand = (customerMethod: PaymentType.customerMethods) => {
}
}

let getCoBadgesCardSchemes = cardNumber => {
let getAllMatchedCardSchemes = cardNumber => {
CardPattern.cardPatterns->Array.reduce([], (acc, item) => {
if String.match(cardNumber, item.pattern)->Option.isSome {
acc->Array.push(item.issuer)
}
acc
})
}

let getEligibleCoBadgedCardSchemes = (~matchedCardSchemes, ~enabledCardSchemes) => {
matchedCardSchemes->Array.filter(ele => {
enabledCardSchemes->Array.includes(ele->String.toLowerCase)
})
}
3 changes: 1 addition & 2 deletions src/Payment.res
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ let make = (~paymentMode, ~integrateError, ~logger) => {
let isManualRetryEnabled = Recoil.useRecoilValueFromAtom(isManualRetryEnabled)
let paymentToken = Recoil.useRecoilValueFromAtom(paymentTokenAtom)
let paymentMethodListValue = Recoil.useRecoilValueFromAtom(PaymentUtils.paymentMethodListValue)
let isCardCoBadged = Recoil.useRecoilValueFromAtom(isCardCoBadged)

let {iframeId} = keys

Expand Down Expand Up @@ -358,7 +357,7 @@ let make = (~paymentMode, ~integrateError, ~logger) => {
}, [cardNumber])

let icon = React.useMemo(() => {
<CardSchemeComponent cardNumber paymentType cardBrand setCardBrand isCardCoBadged />
<CardSchemeComponent cardNumber paymentType cardBrand setCardBrand />
}, (cardType, paymentType, cardBrand, cardNumber))

let cardProps: CardUtils.cardProps = (
Expand Down

0 comments on commit 9dd637b

Please sign in to comment.