-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: HS-1111: Moved HTTP requests to preMountLoader iframe"
- Loading branch information
Vrishab Srivatsa
committed
Feb 26, 2024
1 parent
e8a512c
commit 4d2ac42
Showing
8 changed files
with
614 additions
and
520 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,106 @@ | ||
@react.component | ||
let make = (~sessionId, ~publishableKey, ~clientSecret, ~endpoint) => { | ||
// open Utils | ||
// let logger = OrcaLogger.make( | ||
// ~sessionId, | ||
// ~source=Loader, | ||
// ~merchantId=publishableKey, | ||
// ~clientSecret, | ||
// (), | ||
// ) | ||
open Utils | ||
let (paymentMethodsResponseSent, setPaymentMethodsResponseSent) = React.useState(_ => false) | ||
let ( | ||
customerPaymentMethodsResponseSent, | ||
setCustomerPaymentMethodsResponseSent, | ||
) = React.useState(_ => false) | ||
let (sessionTokensResponseSent, setSessionTokensResponseSent) = React.useState(_ => false) | ||
let logger = OrcaLogger.make( | ||
~sessionId, | ||
~source=Loader, | ||
~merchantId=publishableKey, | ||
~clientSecret, | ||
(), | ||
) | ||
|
||
// // let ( | ||
// // paymentMethodsResponse, | ||
// // customerPaymentMethodsResponse, | ||
// // sessionTokensResponse, | ||
// // ) = React.useMemo0(() => { | ||
// // ( | ||
// // PaymentHelpers.usePaymentMethodList( | ||
// // ~clientSecret, | ||
// // ~publishableKey, | ||
// // ~logger, | ||
// // ~switchToCustomPod=false, | ||
// // ~endpoint, | ||
// // ), | ||
// // PaymentHelpers.useCustomerDetails( | ||
// // ~clientSecret, | ||
// // ~publishableKey, | ||
// // ~optLogger=Some(logger), | ||
// // ~switchToCustomPod=false, | ||
// // ~endpoint, | ||
// // ), | ||
// // PaymentHelpers.useSessions( | ||
// // ~clientSecret, | ||
// // ~publishableKey, | ||
// // ~optLogger=Some(logger), | ||
// // ~switchToCustomPod=false, | ||
// // ~endpoint, | ||
// // (), | ||
// // ), | ||
// // ) | ||
// // }) | ||
let ( | ||
paymentMethodsResponse, | ||
customerPaymentMethodsResponse, | ||
sessionTokensResponse, | ||
) = React.useMemo0(() => { | ||
( | ||
PaymentHelpers.usePaymentMethodList( | ||
~clientSecret, | ||
~publishableKey, | ||
~logger, | ||
~switchToCustomPod=false, | ||
~endpoint, | ||
), | ||
PaymentHelpers.useCustomerDetails( | ||
~clientSecret, | ||
~publishableKey, | ||
~optLogger=Some(logger), | ||
~switchToCustomPod=false, | ||
~endpoint, | ||
), | ||
PaymentHelpers.useSessions( | ||
~clientSecret, | ||
~publishableKey, | ||
~optLogger=Some(logger), | ||
~switchToCustomPod=false, | ||
~endpoint, | ||
(), | ||
), | ||
) | ||
}) | ||
|
||
// // let sendPromiseData = (promise, key) => { | ||
// // open Promise | ||
// // promise | ||
// // ->then(res => { | ||
// // handlePostMessage([("response", res), ("data", key->Js.Json.string)]) | ||
// // resolve() | ||
// // }) | ||
// // ->catch(_err => { | ||
// // handlePostMessage([("response", Js.Json.null), ("data", key->Js.Json.string)]) | ||
// // resolve() | ||
// // }) | ||
// // ->ignore | ||
// // } | ||
let sendPromiseData = (promise, key) => { | ||
open Promise | ||
promise | ||
->then(res => { | ||
handlePostMessage([("response", res), ("data", key->Js.Json.string)]) | ||
resolve() | ||
}) | ||
->catch(_err => { | ||
handlePostMessage([("response", Js.Json.null), ("data", key->Js.Json.string)]) | ||
resolve() | ||
}) | ||
->ignore | ||
} | ||
|
||
// // React.useEffect0(() => { | ||
// // let handle = (ev: Window.event) => { | ||
// // let json = try { | ||
// // ev.data->Js.Json.parseExn | ||
// // } catch { | ||
// // | _ => Js.Json.null | ||
// // } | ||
// // let dict = json->Utils.getDictFromJson | ||
// // if dict->Js.Dict.get("sendPaymentMethodsResponse")->Belt.Option.isSome { | ||
// // paymentMethodsResponse->sendPromiseData("payment_methods") | ||
// // } else if dict->Js.Dict.get("sendCustomerPaymentMethodsResponse")->Belt.Option.isSome { | ||
// // customerPaymentMethodsResponse->sendPromiseData("customer_payment_methods") | ||
// // } else if dict->Js.Dict.get("sendSessionTokensResponse")->Belt.Option.isSome { | ||
// // sessionTokensResponse->sendPromiseData("session_tokens") | ||
// // } | ||
// // } | ||
// // Window.addEventListener("message", handle) | ||
// // handlePostMessage([("preMountLoaderInitCallback", true->Js.Json.boolean)]) | ||
// // Some( | ||
// // () => { | ||
// // Window.removeEventListener("message", handle) | ||
// // }, | ||
// // ) | ||
// // }) | ||
let handle = (ev: Window.event) => { | ||
let json = try { | ||
ev.data->Js.Json.parseExn | ||
} catch { | ||
| _ => Js.Json.null | ||
} | ||
Js.log("preMountHandlerEventHandler") | ||
let dict = json->Utils.getDictFromJson | ||
if dict->Js.Dict.get("sendPaymentMethodsResponse")->Belt.Option.isSome { | ||
paymentMethodsResponse->sendPromiseData("payment_methods") | ||
setPaymentMethodsResponseSent(_ => true) | ||
} else if dict->Js.Dict.get("sendCustomerPaymentMethodsResponse")->Belt.Option.isSome { | ||
if dict->Utils.getBool("sendCustomerPaymentMethodsResponse", false) { | ||
customerPaymentMethodsResponse->sendPromiseData("customer_payment_methods") | ||
} | ||
setCustomerPaymentMethodsResponseSent(_ => true) | ||
} else if dict->Js.Dict.get("sendSessionTokensResponse")->Belt.Option.isSome { | ||
sessionTokensResponse->sendPromiseData("session_tokens") | ||
setSessionTokensResponseSent(_ => true) | ||
} | ||
} | ||
|
||
React.useEffect0(() => { | ||
Window.addEventListener("message", handle) | ||
handlePostMessage([("preMountLoaderIframeMountedCallback", true->Js.Json.boolean)]) | ||
Some( | ||
() => { | ||
Window.removeEventListener("message", handle) | ||
}, | ||
) | ||
}) | ||
|
||
React.useEffect3(() => { | ||
if ( | ||
paymentMethodsResponseSent && customerPaymentMethodsResponseSent && sessionTokensResponseSent | ||
) { | ||
Js.log("Removing event listener") | ||
Window.removeEventListener("message", handle) | ||
} | ||
None | ||
}, (paymentMethodsResponseSent, customerPaymentMethodsResponseSent, sessionTokensResponseSent)) | ||
|
||
React.null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.