From 68a1e158256a1e3a09e816dac622dea2dd76434e Mon Sep 17 00:00:00 2001 From: Pritish Budhiraja Date: Tue, 12 Nov 2024 17:09:46 +0530 Subject: [PATCH] refactor: async await refactoring - 1 (#768) Co-authored-by: Shiva Nandan --- src/Components/SavedMethodItem.res | 2 +- src/Components/SavedPaymentManagement.res | 50 ++++----- src/Payments/PazeButton.res | 56 +++++----- src/Payments/PlaidSDKIframe.res | 127 ++++++++++------------ src/Payments/QRCodeDisplay.res | 119 ++++++++++---------- src/Utilities/PaymentUtils.res | 36 +++--- 6 files changed, 183 insertions(+), 207 deletions(-) diff --git a/src/Components/SavedMethodItem.res b/src/Components/SavedMethodItem.res index 287e712b1..9de1eee78 100644 --- a/src/Components/SavedMethodItem.res +++ b/src/Components/SavedMethodItem.res @@ -69,7 +69,7 @@ let make = (~brandIcon, ~paymentItem: PaymentType.customerMethods, ~handleDelete name="delete" style={color: themeObj.colorDanger} className="cursor-pointer ml-4 mb-[6px]" - onClick={_ => paymentItem->handleDelete} + onClick={_ => handleDelete(paymentItem)->ignore} />
diff --git a/src/Components/SavedPaymentManagement.res b/src/Components/SavedPaymentManagement.res index f213bef33..cca4e1afc 100644 --- a/src/Components/SavedPaymentManagement.res +++ b/src/Components/SavedPaymentManagement.res @@ -2,35 +2,31 @@ let make = (~savedMethods: array, ~setSavedMethods) => { open CardUtils open Utils + open RecoilAtoms - let {iframeId} = Recoil.useRecoilValueFromAtom(RecoilAtoms.keys) - let {config} = Recoil.useRecoilValueFromAtom(RecoilAtoms.configAtom) - let customPodUri = Recoil.useRecoilValueFromAtom(RecoilAtoms.customPodUri) - let logger = Recoil.useRecoilValueFromAtom(RecoilAtoms.loggerAtom) + let {iframeId} = Recoil.useRecoilValueFromAtom(keys) + let {config} = Recoil.useRecoilValueFromAtom(configAtom) + let customPodUri = Recoil.useRecoilValueFromAtom(customPodUri) + let logger = Recoil.useRecoilValueFromAtom(loggerAtom) - let removeSavedMethod = ( - savedMethods: array, - paymentMethodId, - ) => { - savedMethods->Array.filter(savedMethod => { - savedMethod.paymentMethodId !== paymentMethodId - }) - } + let removeSavedMethod = (savedMethods: array, paymentMethodId) => + savedMethods->Array.filter(savedMethod => savedMethod.paymentMethodId !== paymentMethodId) - let handleDelete = (paymentItem: PaymentType.customerMethods) => { + let handleDelete = async (paymentItem: PaymentType.customerMethods) => { messageParentWindow([ ("fullscreen", true->JSON.Encode.bool), ("param", "paymentloader"->JSON.Encode.string), ("iframeId", iframeId->JSON.Encode.string), ]) - open Promise - PaymentHelpers.deletePaymentMethod( - ~ephemeralKey=config.ephemeralKey, - ~paymentMethodId=paymentItem.paymentMethodId, - ~logger, - ~customPodUri, - ) - ->then(res => { + + try { + let res = await PaymentHelpers.deletePaymentMethod( + ~ephemeralKey=config.ephemeralKey, + ~paymentMethodId=paymentItem.paymentMethodId, + ~logger, + ~customPodUri, + ) + let dict = res->getDictFromJson let paymentMethodId = dict->getString("payment_method_id", "") let isDeleted = dict->getBool("deleted", false) @@ -44,19 +40,15 @@ let make = (~savedMethods: array, ~setSavedMethods) } else { logger.setLogError(~value=res->JSON.stringify, ~eventName=DELETE_SAVED_PAYMENT_METHOD) } - messageParentWindow([("fullscreen", false->JSON.Encode.bool)]) - resolve() - }) - ->catch(err => { + } catch { + | err => let exceptionMessage = err->formatException->JSON.stringify logger.setLogError( ~value=`Error Deleting Saved Payment Method: ${exceptionMessage}`, ~eventName=DELETE_SAVED_PAYMENT_METHOD, ) - messageParentWindow([("fullscreen", false->JSON.Encode.bool)]) - resolve() - }) - ->ignore + } + messageParentWindow([("fullscreen", false->JSON.Encode.bool)]) } savedMethods diff --git a/src/Payments/PazeButton.res b/src/Payments/PazeButton.res index 411beee9e..4c0b0772e 100644 --- a/src/Payments/PazeButton.res +++ b/src/Payments/PazeButton.res @@ -5,47 +5,40 @@ let make = (~token: SessionsType.token) => { let {iframeId, publishableKey, clientSecret} = Recoil.useRecoilValueFromAtom(keys) let {themeObj} = Recoil.useRecoilValueFromAtom(configAtom) let options = Recoil.useRecoilValueFromAtom(optionAtom) - let (showLoader, setShowLoader) = React.useState(() => false) - let setIsShowOrPayUsing = Recoil.useSetRecoilState(RecoilAtoms.isShowOrPayUsing) + let setIsShowOrPayUsing = Recoil.useSetRecoilState(isShowOrPayUsing) let loggerState = Recoil.useRecoilValueFromAtom(loggerAtom) - let intent = PaymentHelpers.usePaymentIntent(Some(loggerState), Paze) let isManualRetryEnabled = Recoil.useRecoilValueFromAtom(isManualRetryEnabled) + let intent = PaymentHelpers.usePaymentIntent(Some(loggerState), Paze) let paymentIntentID = clientSecret->Option.getOr("")->getPaymentId - - React.useEffect0(() => { - setIsShowOrPayUsing(_ => true) - None - }) - + let (showLoader, setShowLoader) = React.useState(() => false) let onClick = _ => { setShowLoader(_ => true) + let metadata = + [ + ("wallet", (token.walletName :> string)->JSON.Encode.string), + ("clientId", token.clientId->JSON.Encode.string), + ("clientName", token.clientName->JSON.Encode.string), + ("clientProfileId", token.clientProfileId->JSON.Encode.string), + ("sessionId", paymentIntentID->JSON.Encode.string), + ("publishableKey", publishableKey->JSON.Encode.string), + ("emailAddress", token.email_address->JSON.Encode.string), + ("transactionAmount", token.transaction_amount->JSON.Encode.string), + ("transactionCurrencyCode", token.transaction_currency_code->JSON.Encode.string), + ]->getJsonFromArrayOfJson + messageParentWindow([ ("fullscreen", true->JSON.Encode.bool), ("param", "pazeWallet"->JSON.Encode.string), ("iframeId", iframeId->JSON.Encode.string), - ( - "metadata", - [ - ("wallet", (token.walletName :> string)->JSON.Encode.string), - ("clientId", token.clientId->JSON.Encode.string), - ("clientName", token.clientName->JSON.Encode.string), - ("clientProfileId", token.clientProfileId->JSON.Encode.string), - ("sessionId", paymentIntentID->JSON.Encode.string), - ("publishableKey", publishableKey->JSON.Encode.string), - ("emailAddress", token.email_address->JSON.Encode.string), - ("transactionAmount", token.transaction_amount->JSON.Encode.string), - ("transactionCurrencyCode", token.transaction_currency_code->JSON.Encode.string), - ]->getJsonFromArrayOfJson, - ), + ("metadata", metadata), ]) } - React.useEffect0(() => { - let onPazeCallback = (ev: Window.event) => { + React.useEffect(() => { + let handlePazeCallback = ev => { let json = ev.data->safeParse let dict = json->Utils.getDictFromJson->getDictFromDict("data") - let isPaze = dict->getBool("isPaze", false) - if isPaze { + if dict->getBool("isPaze", false) { setShowLoader(_ => false) if dict->getOptionString("completeResponse")->Option.isSome { let completeResponse = dict->getString("completeResponse", "") @@ -61,14 +54,15 @@ let make = (~token: SessionsType.token) => { } } } - Window.addEventListener("message", onPazeCallback) - Some(() => Window.removeEventListener("message", ev => onPazeCallback(ev))) - }) + setIsShowOrPayUsing(_ => true) + Window.addEventListener("message", handlePazeCallback) + Some(() => Window.removeEventListener("message", handlePazeCallback)) + }, []) diff --git a/src/Utilities/PaymentUtils.res b/src/Utilities/PaymentUtils.res index 150978000..b1c071b66 100644 --- a/src/Utilities/PaymentUtils.res +++ b/src/Utilities/PaymentUtils.res @@ -406,24 +406,30 @@ let useGetPaymentMethodList = (~paymentOptions, ~paymentType, ~sessions) => { } let useStatesJson = setStatesJson => { - open Promise - React.useEffect0(() => { - AddressPaymentInput.importStates("./../States.json") - ->then(res => { - setStatesJson(_ => res.states) - resolve() - }) - ->ignore + React.useEffect0(_ => { + let fetchStates = async () => { + try { + let res = await AddressPaymentInput.importStates("./../States.json") + setStatesJson(_ => res.states) + } catch { + | err => Console.error2("Error importing states:", err) + } + } + fetchStates()->ignore None }) } -let getStateJson = () => { - open Promise - AddressPaymentInput.importStates("./../States.json")->then(res => { - res.states->resolve - }) +let getStateJson = async _ => { + try { + let res = await AddressPaymentInput.importStates("./../States.json") + res.states + } catch { + | err => + Console.error2("Error importing states:", err) + JSON.Encode.null + } } let sortCustomerMethodsBasedOnPriority = ( @@ -477,9 +483,7 @@ let sortCustomerMethodsBasedOnPriority = ( } } -let getSupportedCardBrands = ( - paymentMethodListValue: PaymentMethodsRecord.paymentMethodList, -) => { +let getSupportedCardBrands = (paymentMethodListValue: PaymentMethodsRecord.paymentMethodList) => { let cardPaymentMethod = paymentMethodListValue.payment_methods->Array.find(ele => ele.payment_method === "card")