From 4d0f9573d724d426fc7f5393d454fcb25262cedb Mon Sep 17 00:00:00 2001 From: Shiva Nandan Date: Tue, 20 Aug 2024 17:35:09 +0530 Subject: [PATCH] refactor: refactor PreMountLoader --- src/Payments/PreMountLoader.res | 222 +++++++++++++------------------- 1 file changed, 93 insertions(+), 129 deletions(-) diff --git a/src/Payments/PreMountLoader.res b/src/Payments/PreMountLoader.res index 4e3abf9a7..0ef19f3ad 100644 --- a/src/Payments/PreMountLoader.res +++ b/src/Payments/PreMountLoader.res @@ -1,63 +1,53 @@ -@react.component -let make = ( - ~sessionId, - ~publishableKey, - ~clientSecret, - ~endpoint, - ~ephemeralKey, - ~hyperComponentName: Types.hyperComponentName, - ~merchantHostname, -) => { - open Utils - let (paymentMethodsResponseSent, setPaymentMethodsResponseSent) = React.useState(_ => false) - let ( - customerPaymentMethodsResponseSent, - setCustomerPaymentMethodsResponseSent, - ) = React.useState(_ => false) - let (savedPaymentMethodsResponseSent, setSavedPaymentMethodsResponseSent) = React.useState(_ => - false - ) - let (sessionTokensResponseSent, setSessionTokensResponseSent) = React.useState(_ => false) - let logger = OrcaLogger.make( - ~sessionId, - ~source=Loader, - ~merchantId=publishableKey, - ~clientSecret, - ) +let sendPromiseData = (promise, key) => { + open Promise + promise + ->then(res => resolve(res)) + ->catch(_err => resolve(JSON.Encode.null)) + ->thenResolve(response => { + Utils.messageParentWindow([("response", response), ("data", key->JSON.Encode.string)]) + }) + ->ignore +} - let ( - paymentMethodsResponse, - customerPaymentMethodsResponse, - sessionTokensResponse, - savedPaymentMethodsResponse, - ) = React.useMemo0(() => { - let paymentMethodsResponse = switch hyperComponentName { - | Elements => - PaymentHelpers.fetchPaymentMethodList( +let useMessageHandler = getPromisesAndMessageHandler => { + React.useEffect0(() => { + let (promises, messageHandler) = getPromisesAndMessageHandler() + + open Promise + Promise.all(promises) + ->thenResolve(_ => { + Utils.messageParentWindow([("preMountLoaderIframeUnMount", true->JSON.Encode.bool)]) + Window.removeEventListener("message", messageHandler) + }) + ->ignore + + Utils.messageParentWindow([("preMountLoaderIframeMountedCallback", true->JSON.Encode.bool)]) + Window.addEventListener("message", messageHandler) + Some(() => Window.removeEventListener("message", messageHandler)) + }) +} + +module PreMountLoaderForElements = { + @react.component + let make = (~logger, ~publishableKey, ~clientSecret, ~endpoint, ~merchantHostname) => { + useMessageHandler(() => { + let paymentMethodsPromise = PaymentHelpers.fetchPaymentMethodList( ~clientSecret, ~publishableKey, ~logger, ~switchToCustomPod=false, ~endpoint, ) - | _ => JSON.Encode.null->Promise.resolve - } - let customerPaymentMethodsResponse = switch hyperComponentName { - | Elements => - PaymentHelpers.fetchCustomerPaymentMethodList( + let customerPaymentMethodsPromise = PaymentHelpers.fetchCustomerPaymentMethodList( ~clientSecret, ~publishableKey, ~optLogger=Some(logger), ~switchToCustomPod=false, ~endpoint, ) - | _ => JSON.Encode.null->Promise.resolve - } - let sessionTokensResponse = switch hyperComponentName { - | Elements => - PaymentHelpers.fetchSessions( + let sessionTokensPromise = PaymentHelpers.fetchSessions( ~clientSecret, ~publishableKey, ~optLogger=Some(logger), @@ -65,101 +55,75 @@ let make = ( ~endpoint, ~merchantHostname, ) - | _ => JSON.Encode.null->Promise.resolve - } - let savedPaymentMethodsResponse = switch hyperComponentName { - | PaymentMethodsManagementElements => - PaymentHelpers.fetchSavedPaymentMethodList( + let messageHandler = (ev: Window.event) => { + let json = ev.data->Utils.safeParse + let dict = json->Utils.getDictFromJson + if dict->Dict.get("sendPaymentMethodsResponse")->Option.isSome { + paymentMethodsPromise->sendPromiseData("payment_methods") + } else if dict->Dict.get("sendCustomerPaymentMethodsResponse")->Option.isSome { + customerPaymentMethodsPromise->sendPromiseData("customer_payment_methods") + } else if dict->Dict.get("sendSessionTokensResponse")->Option.isSome { + sessionTokensPromise->sendPromiseData("session_tokens") + } + } + + let promises = [paymentMethodsPromise, customerPaymentMethodsPromise, sessionTokensPromise] + (promises, messageHandler) + }) + + React.null + } +} + +module PreMountLoaderForPMMElements = { + @react.component + let make = (~logger, ~endpoint, ~ephemeralKey) => { + useMessageHandler(() => { + let savedPaymentMethodsPromise = PaymentHelpers.fetchSavedPaymentMethodList( ~ephemeralKey, ~optLogger=Some(logger), ~switchToCustomPod=false, ~endpoint, ) - | _ => JSON.Encode.null->Promise.resolve - } - - ( - paymentMethodsResponse, - customerPaymentMethodsResponse, - sessionTokensResponse, - savedPaymentMethodsResponse, - ) - }) - let sendPromiseData = (promise, key) => { - open Promise - promise - ->then(res => { - messageParentWindow([("response", res), ("data", key->JSON.Encode.string)]) - switch key { - | "payment_methods" => setPaymentMethodsResponseSent(_ => true) - | "session_tokens" => setSessionTokensResponseSent(_ => true) - | "customer_payment_methods" => setCustomerPaymentMethodsResponseSent(_ => true) - | "saved_payment_methods" => setSavedPaymentMethodsResponseSent(_ => true) - | _ => () + let messageHandler = (ev: Window.event) => { + let json = ev.data->Utils.safeParse + let dict = json->Utils.getDictFromJson + if dict->Dict.get("sendSavedPaymentMethodsResponse")->Belt.Option.isSome { + savedPaymentMethodsPromise->sendPromiseData("saved_payment_methods") + } } - resolve() - }) - ->catch(_err => { - messageParentWindow([("response", JSON.Encode.null), ("data", key->JSON.Encode.string)]) - resolve() + + let promises = [savedPaymentMethodsPromise] + (promises, messageHandler) }) - ->ignore - } - let handle = (ev: Window.event) => { - let json = ev.data->safeParse - let dict = json->Utils.getDictFromJson - if dict->Dict.get("sendPaymentMethodsResponse")->Option.isSome { - paymentMethodsResponse->sendPromiseData("payment_methods") - } else if dict->Dict.get("sendCustomerPaymentMethodsResponse")->Option.isSome { - customerPaymentMethodsResponse->sendPromiseData("customer_payment_methods") - } else if dict->Dict.get("sendSessionTokensResponse")->Option.isSome { - sessionTokensResponse->sendPromiseData("session_tokens") - } else if dict->Dict.get("sendSavedPaymentMethodsResponse")->Belt.Option.isSome { - savedPaymentMethodsResponse->sendPromiseData("saved_payment_methods") - } + React.null } +} - React.useEffect0(() => { - Window.addEventListener("message", handle) - messageParentWindow([("preMountLoaderIframeMountedCallback", true->JSON.Encode.bool)]) - Some( - () => { - Window.removeEventListener("message", handle) - }, - ) - }) - - React.useEffect4(() => { - let handleUnmount = () => { - messageParentWindow([("preMountLoaderIframeUnMount", true->JSON.Encode.bool)]) - Window.removeEventListener("message", handle) - } - - switch hyperComponentName { - | Elements => - if ( - paymentMethodsResponseSent && - customerPaymentMethodsResponseSent && - sessionTokensResponseSent - ) { - handleUnmount() - } - | PaymentMethodsManagementElements => - if savedPaymentMethodsResponseSent { - handleUnmount() - } - } - - None - }, ( - paymentMethodsResponseSent, - customerPaymentMethodsResponseSent, - sessionTokensResponseSent, - savedPaymentMethodsResponseSent, - )) +@react.component +let make = ( + ~sessionId, + ~publishableKey, + ~clientSecret, + ~endpoint, + ~ephemeralKey, + ~hyperComponentName: Types.hyperComponentName, + ~merchantHostname, +) => { + let logger = OrcaLogger.make( + ~sessionId, + ~source=Loader, + ~merchantId=publishableKey, + ~clientSecret, + ) - React.null + switch hyperComponentName { + | Elements => + + | PaymentMethodsManagementElements => + + } }