Skip to content

Commit

Permalink
fix(animatedcheckbox): Save Card Details checkbox changes (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
vsrivatsa-edinburgh authored Feb 27, 2024
1 parent 55dd561 commit 8cf979c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
22 changes: 16 additions & 6 deletions src/Payments/CardPayment.res
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,20 @@ let make = (
React.useEffect1(() => {
switch customerPaymentMethods {
| LoadingSavedCards => ()
| LoadedSavedCards(arr) => {
| LoadedSavedCards(arr, isGuestCustomer) => {
let savedCards = arr->Js.Array2.filter((item: PaymentType.customerMethods) => {
item.paymentMethod == "card"
})
setSavedMethods(_ => savedCards)
setLoadSavedCards(_ =>
savedCards->Js.Array2.length == 0 ? NoResult : LoadedSavedCards(savedCards)
savedCards->Js.Array2.length == 0
? NoResult(isGuestCustomer)
: LoadedSavedCards(savedCards, isGuestCustomer)
)
setShowFields(.prev => savedCards->Js.Array2.length == 0 || prev)
}
| NoResult => {
setLoadSavedCards(_ => NoResult)
| NoResult(isGuestCustomer) => {
setLoadSavedCards(_ => NoResult(isGuestCustomer))
setShowFields(._ => true)
}
}
Expand All @@ -105,7 +107,7 @@ let make = (
React.useEffect1(() => {
if disableSaveCards {
setShowFields(._ => true)
setLoadSavedCards(_ => LoadedSavedCards([]))
setLoadSavedCards(_ => LoadedSavedCards([], true))
}
None
}, [disableSaveCards])
Expand Down Expand Up @@ -140,6 +142,14 @@ let make = (
None
}, (empty, complete))

let isGuestCustomer = React.useMemo1(() => {
switch customerPaymentMethods {
| LoadedSavedCards(_, false)
| NoResult(false) => false
| _ => true
}
}, [customerPaymentMethods])

let isCvcValidValue = CardUtils.getBoolOptionVal(isCVCValid)
let (cardEmpty, cardComplete, cardInvalid) = CardUtils.useCardDetails(
~cvcNumber,
Expand Down Expand Up @@ -326,7 +336,7 @@ let make = (
cvcProps={Some(cvcProps)}
isBancontact
/>
<RenderIf condition={!isBancontact && !options.disableSaveCards}>
<RenderIf condition={!isBancontact && !options.disableSaveCards && !isGuestCustomer}>
<div className="pt-4 pb-2 flex items-center justify-start">
<AnimatedCheckbox isChecked=isSaveCardsChecked setIsChecked=setIsSaveCardsChecked />
</div>
Expand Down
15 changes: 11 additions & 4 deletions src/Types/PaymentType.res
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ type customerMethods = {
paymentMethodIssuer: option<string>,
card: customerCard,
}
type savedCardsLoadState = LoadingSavedCards | LoadedSavedCards(array<customerMethods>) | NoResult
type savedCardsLoadState =
LoadingSavedCards | LoadedSavedCards(array<customerMethods>, bool) | NoResult(bool)

type billingAddress = {
isUseBillingAddress: bool,
Expand Down Expand Up @@ -247,7 +248,7 @@ let defaultBillingAddress = {
let defaultOptions = {
defaultValues: defaultDefaultValues,
business: defaultBusiness,
customerPaymentMethods: NoResult,
customerPaymentMethods: NoResult(true),
layout: ObjectLayout(defaultLayout),
paymentMethodOrder: None,
fields: defaultFields,
Expand Down Expand Up @@ -777,6 +778,12 @@ let createCustomerObjArr = dict => {
->Belt.Option.flatMap(Js.Json.decodeArray)
->Belt.Option.getWithDefault([])

let isGuestCustomer =
customerDict
->Js.Dict.get("is_guest_customer")
->Belt.Option.flatMap(Js.Json.decodeBoolean)
->Belt.Option.getWithDefault(false)

let customerPaymentMethods =
customerArr
->Belt.Array.keepMap(Js.Json.decodeObject)
Expand All @@ -789,7 +796,7 @@ let createCustomerObjArr = dict => {
card: getCardDetails(json, "card"),
}
})
LoadedSavedCards(customerPaymentMethods)
LoadedSavedCards(customerPaymentMethods, isGuestCustomer)
}

let getCustomerMethods = (dict, str) => {
Expand All @@ -809,7 +816,7 @@ let getCustomerMethods = (dict, str) => {
card: getCardDetails(json, "card"),
}
})
LoadedSavedCards(customerPaymentMethods)
LoadedSavedCards(customerPaymentMethods, false)
} else {
LoadingSavedCards
}
Expand Down

0 comments on commit 8cf979c

Please sign in to comment.