Skip to content

Commit

Permalink
refactor(payout): rename variables, remove console logs
Browse files Browse the repository at this point in the history
  • Loading branch information
kashif-m committed Sep 12, 2024
1 parent 01f8f58 commit 386107d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 57 deletions.
11 changes: 5 additions & 6 deletions src/CollectWidget.res
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let make = (
let {enabledPaymentMethodsWithDynamicFields} = Recoil.useRecoilValueFromAtom(
paymentMethodCollectOptionAtom,
)
let (_, setDynamicFields) = Recoil.useRecoilState(dynamicFieldsAtom)
let (_, setPayoutDynamicFields) = Recoil.useRecoilState(payoutDynamicFieldsAtom)
let (formData, setFormData) = Recoil.useRecoilState(formDataAtom)
let (activePmt, _) = Recoil.useRecoilState(paymentMethodTypeAtom)
let (validityDict, setValidityDict) = Recoil.useRecoilState(validityDictAtom)
Expand All @@ -32,10 +32,10 @@ let make = (

// Hook for fetching dynamic fields, and default values and their validity for payment method type update
React.useEffect(() => {
getDynamicFields(enabledPaymentMethodsWithDynamicFields, activePmt)
->Option.flatMap(dynamicFields => {
setDynamicFields(_ => dynamicFields)
getDefaultsAndValidity(dynamicFields)
getPayoutDynamicFields(enabledPaymentMethodsWithDynamicFields, activePmt)
->Option.flatMap(payoutDynamicFields => {
setPayoutDynamicFields(_ => payoutDynamicFields)
getDefaultsAndValidity(payoutDynamicFields)
})
->Option.map(((values, validity)) => {
setFormData(_ => values)
Expand All @@ -56,7 +56,6 @@ let make = (

let setValidityDictVal = (key, value) => {
setValidityDict(prevDict => {
Js.Console.info2("SETTING", (key, value, prevDict))
let copy = prevDict->Dict.copy
copy->Dict.set(key, value)
copy
Expand Down
31 changes: 15 additions & 16 deletions src/FormViewJourney.res
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let make = (
) => {
// Recoil states
let {localeString} = Recoil.useRecoilValueFromAtom(configAtom)
let dynamicFields = Recoil.useRecoilValueFromAtom(dynamicFieldsAtom)
let payoutDynamicFields = Recoil.useRecoilValueFromAtom(payoutDynamicFieldsAtom)
let formData = Recoil.useRecoilValueFromAtom(formDataAtom)
let (activePmt, setActivePmt) = Recoil.useRecoilState(paymentMethodTypeAtom)
let (validityDict, setValidityDict) = Recoil.useRecoilState(validityDictAtom)
Expand Down Expand Up @@ -106,22 +106,22 @@ let make = (
let newView = switch newPm {
| Card => {
let newPmt = Card(Debit)
let dynamicFields =
getDynamicFields(enabledPaymentMethodsWithDynamicFields, newPmt)->Option.getOr(
defaultDynamicFields(~pmt=newPmt),
let payoutDynamicFields =
getPayoutDynamicFields(enabledPaymentMethodsWithDynamicFields, newPmt)->Option.getOr(
defaultPayoutDynamicFields(~pmt=newPmt),
)
setActivePmt(_ => newPmt)
dynamicFields.address
payoutDynamicFields.address
->Option.map(address => {
let fieldsToCollect =
address->Array.filter(addressField => addressField.value == None)
if fieldsToCollect->Array.length > 0 {
AddressForm(address)
} else {
PMDForm(newPmt, dynamicFields.payoutMethodData)
PMDForm(newPmt, payoutDynamicFields.payoutMethodData)
}
})
->Option.getOr(PMDForm(newPmt, dynamicFields.payoutMethodData))
->Option.getOr(PMDForm(newPmt, payoutDynamicFields.payoutMethodData))
}
| _ => SelectPMType(newPm)
}
Expand Down Expand Up @@ -167,23 +167,23 @@ let make = (
setActivePmt(_ => newPmt)

// Update view
let dynamicFields =
getDynamicFields(enabledPaymentMethodsWithDynamicFields, newPmt)->Option.getOr(
defaultDynamicFields(~pmt=newPmt),
let payoutDynamicFields =
getPayoutDynamicFields(enabledPaymentMethodsWithDynamicFields, newPmt)->Option.getOr(
defaultPayoutDynamicFields(~pmt=newPmt),
)

let newView =
dynamicFields.address
payoutDynamicFields.address
->Option.map(address => {
let fieldsToCollect =
address->Array.filter(addressField => addressField.value == None)
if fieldsToCollect->Array.length > 0 {
AddressForm(address)
} else {
PMDForm(newPmt, dynamicFields.payoutMethodData)
PMDForm(newPmt, payoutDynamicFields.payoutMethodData)
}
})
->Option.getOr(PMDForm(newPmt, dynamicFields.payoutMethodData))
->Option.getOr(PMDForm(newPmt, payoutDynamicFields.payoutMethodData))

View.setView(newView)
}
Expand Down Expand Up @@ -247,7 +247,7 @@ let make = (

setValidityDict(_ => fieldValidity)
if isAddressValid {
View.setView(PMDForm(activePmt, dynamicFields.payoutMethodData))
View.setView(PMDForm(activePmt, payoutDynamicFields.payoutMethodData))
}
}
<>
Expand Down Expand Up @@ -282,8 +282,7 @@ let make = (

setValidityDict(_ => fieldValidity)
if isPmdValid {
Js.Console.info2("formPaymentMethodData", (formData, fieldValidity, dynamicFields))
formPaymentMethodData(formData, fieldValidity, dynamicFields)
formPaymentMethodData(formData, fieldValidity, payoutDynamicFields)
->Option.map(pmd => View.setView(FinalizeView((activePmt, pmd))))
->ignore
}
Expand Down
12 changes: 6 additions & 6 deletions src/FormViewTabs.res
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let make = (
) => {
// Recoil states
let {localeString} = Recoil.useRecoilValueFromAtom(configAtom)
let dynamicFields = Recoil.useRecoilValueFromAtom(dynamicFieldsAtom)
let payoutDynamicFields = Recoil.useRecoilValueFromAtom(payoutDynamicFieldsAtom)
let formData = Recoil.useRecoilValueFromAtom(formDataAtom)
let (activePmt, setActivePmt) = Recoil.useRecoilState(paymentMethodTypeAtom)
let validityDict = Recoil.useRecoilValueFromAtom(validityDictAtom)
Expand Down Expand Up @@ -179,7 +179,7 @@ let make = (

let onSaveHandler = () => {
let (fieldValidity, isAddressValid) =
dynamicFields.address
payoutDynamicFields.address
->Option.map(addressFields => {
addressFields->Array.reduce((Dict.make(), true), (
(fieldValidity, isAddressValid),
Expand All @@ -195,7 +195,7 @@ let make = (
})
->Option.getOr((validityDict->Dict.copy, true))

let (fieldValidity, isPmdValid) = dynamicFields.payoutMethodData->Array.reduce(
let (fieldValidity, isPmdValid) = payoutDynamicFields.payoutMethodData->Array.reduce(
(fieldValidity, isAddressValid),
((fieldValidity, isPmdValid), field) => {
let key = PayoutMethodData(field.fieldType)->getPaymentMethodDataFieldKey
Expand All @@ -208,7 +208,7 @@ let make = (
)

if isPmdValid {
formPaymentMethodData(formData, fieldValidity, dynamicFields)
formPaymentMethodData(formData, fieldValidity, payoutDynamicFields)
->Option.map(pmd => View.setView(FinalizeView(activePmt, pmd)))
->ignore
}
Expand All @@ -223,8 +223,8 @@ let make = (
| Wallet(_) => key->localeString.formHeaderWalletText
}->React.string}
</div>
{dynamicFields.payoutMethodData->renderPayoutMethodForm->React.array}
{dynamicFields.address
{payoutDynamicFields.payoutMethodData->renderPayoutMethodForm->React.array}
{payoutDynamicFields.address
->Option.map(addressFields => {
let formFields = addressFields->renderAddressForm
if formFields->Array.length > 0 {
Expand Down
20 changes: 10 additions & 10 deletions src/Types/PaymentMethodCollectTypes.res
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ type dynamicFieldType =
| BillingAddress(addressField)
| PayoutMethodData(paymentMethodDataField)

type dynamicFields = {
type payoutDynamicFields = {
address: option<array<dynamicFieldForAddress>>,
payoutMethodData: array<dynamicFieldForPaymentMethodData>,
}

type paymentMethodTypeWithDynamicFields =
| Card((card, dynamicFields))
| BankTransfer((bankTransfer, dynamicFields))
| Wallet((wallet, dynamicFields))
| Card((card, payoutDynamicFields))
| BankTransfer((bankTransfer, payoutDynamicFields))
| Wallet((wallet, payoutDynamicFields))

type paymentMethodType =
| Card(card)
Expand Down Expand Up @@ -338,7 +338,7 @@ let sortByCustomOrder = (arr: array<'a>, getKey: 'a => string, customOrder: arra
})
}

let decodeDynamicFields = (json: JSON.t): option<dynamicFields> =>
let decodePayoutDynamicFields = (json: JSON.t): option<payoutDynamicFields> =>
json
->JSON.Decode.object
->Option.map(obj => {
Expand Down Expand Up @@ -424,30 +424,30 @@ let decodePaymentMethodTypeWithRequiredFields = (
}
->Option.map(
pmt => {
let dynamicFields =
let payoutDynamicFields =
obj
->Dict.get("required_fields")
->Option.flatMap(decodeDynamicFields)
->Option.flatMap(decodePayoutDynamicFields)
->Option.getOr({
address: None,
payoutMethodData: defaultDynamicPmdFields(~pmt),
})
switch pmt {
| Card(card) => {
pmta->Array.push(Card(card))
let pmtwr: paymentMethodTypeWithDynamicFields = Card(card, dynamicFields)
let pmtwr: paymentMethodTypeWithDynamicFields = Card(card, payoutDynamicFields)
pmtr->Array.push(pmtwr)
(pmta, pmtr)
}
| BankTransfer(transfer) => {
pmta->Array.push(BankTransfer(transfer))
pmtr->Array.push(BankTransfer(transfer, dynamicFields))
pmtr->Array.push(BankTransfer(transfer, payoutDynamicFields))
(pmta, pmtr)
}
| Wallet(wallet) => {
let pmt: paymentMethodType = Wallet(wallet)
pmta->Array.push(pmt)
pmtr->Array.push(Wallet(wallet, dynamicFields))
pmtr->Array.push(Wallet(wallet, payoutDynamicFields))
(pmta, pmtr)
}
}
Expand Down
31 changes: 15 additions & 16 deletions src/Utilities/PaymentMethodCollectUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ let defaultDynamicPmdFields = (~pmt: paymentMethodType=defaultPmt()): array<
| Wallet(Venmo) => []
}
}
let defaultDynamicFields = (~pmt: paymentMethodType=defaultPmt()): dynamicFields => {
let defaultPayoutDynamicFields = (~pmt: paymentMethodType=defaultPmt()): payoutDynamicFields => {
{
address: None,
payoutMethodData: defaultDynamicPmdFields(~pmt),
Expand All @@ -565,12 +565,12 @@ let defaultEnabledPaymentMethods: array<paymentMethodType> = [
Wallet(Paypal),
]
let defaultEnabledPaymentMethodsWithDynamicFields: array<paymentMethodTypeWithDynamicFields> = [
Card((Credit, defaultDynamicFields())),
Card((Debit, defaultDynamicFields())),
BankTransfer((ACH, defaultDynamicFields())),
BankTransfer((Bacs, defaultDynamicFields())),
BankTransfer((Sepa, defaultDynamicFields())),
Wallet((Paypal, defaultDynamicFields())),
Card((Credit, defaultPayoutDynamicFields())),
Card((Debit, defaultPayoutDynamicFields())),
BankTransfer((ACH, defaultPayoutDynamicFields())),
BankTransfer((Bacs, defaultPayoutDynamicFields())),
BankTransfer((Sepa, defaultPayoutDynamicFields())),
Wallet((Paypal, defaultPayoutDynamicFields())),
]
let defaultPaymentMethodCollectOptions = {
enabledPaymentMethods: defaultEnabledPaymentMethods,
Expand Down Expand Up @@ -763,7 +763,6 @@ let processPaymentMethodDataFields = (
->Option.map(value => dataArr->Array.push((info, value->String.trim)))
->ignore
keys->Array.push(fieldKey)
Js.Console.log2("FORMING PMD DATA", (key, dataArr, keys))
(dataArr, keys)
}
}
Expand Down Expand Up @@ -937,10 +936,10 @@ let formBody = (flow: paymentMethodCollectFlow, paymentMethodData: paymentMethod
body
}

let getDynamicFields = (
let getPayoutDynamicFields = (
enabledPaymentMethodsWithDynamicFields: array<paymentMethodTypeWithDynamicFields>,
reqPmt,
): option<dynamicFields> =>
): option<payoutDynamicFields> =>
enabledPaymentMethodsWithDynamicFields
->Array.find(pmtr => {
switch (pmtr, reqPmt) {
Expand All @@ -956,14 +955,14 @@ let getDynamicFields = (
})
->Option.map(pmt => {
switch pmt {
| Card(_, dynamicFields)
| BankTransfer(_, dynamicFields)
| Wallet(_, dynamicFields) => dynamicFields
| Card(_, payoutDynamicFields)
| BankTransfer(_, payoutDynamicFields)
| Wallet(_, payoutDynamicFields) => payoutDynamicFields
}
})

let getDefaultsAndValidity = dynamicFields => {
dynamicFields.address
let getDefaultsAndValidity = payoutDynamicFields => {
payoutDynamicFields.address
->Option.map(address => {
address->Array.reduce((Dict.make(), Dict.make()), ((values, validity), field) => {
switch (field.fieldType, field.value) {
Expand All @@ -984,7 +983,7 @@ let getDefaultsAndValidity = dynamicFields => {
})
})
->Option.map(((addressValues, addressValidity)) => {
dynamicFields.payoutMethodData->Array.reduce((addressValues, addressValidity), (
payoutDynamicFields.payoutMethodData->Array.reduce((addressValues, addressValidity), (
(values, validity),
field,
) => {
Expand Down
6 changes: 3 additions & 3 deletions src/Utilities/RecoilAtoms.res
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ let paymentMethodCollectOptionAtom = Recoil.atom(
"paymentMethodCollectOptions",
PaymentMethodCollectUtils.defaultPaymentMethodCollectOptions,
)
let dynamicFieldsAtom = Recoil.atom(
"dynamicFields",
PaymentMethodCollectUtils.defaultDynamicFields(),
let payoutDynamicFieldsAtom = Recoil.atom(
"payoutDynamicFields",
PaymentMethodCollectUtils.defaultPayoutDynamicFields(),
)
let paymentMethodTypeAtom = Recoil.atom("paymentMethodType", PaymentMethodCollectUtils.defaultPmt())
let formDataAtom = Recoil.atom("formData", PaymentMethodCollectUtils.defaultFormDataDict)
Expand Down

0 comments on commit 386107d

Please sign in to comment.