Skip to content

Commit

Permalink
fix: hooks conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
vsrivatsa-juspay committed Jul 2, 2024
1 parent 9b50b1e commit 6aaae7b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/Payments/KlarnaSDK.res
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ let make = (~sessionObj: SessionsType.token) => {
let {iframeId} = Recoil.useRecoilValueFromAtom(keys)
let status = CommonHooks.useScript("https://x.klarnacdn.net/kp/lib/v1/api.js") // Klarna SDK script
let paymentMethodListValue = Recoil.useRecoilValueFromAtom(PaymentUtils.paymentMethodListValue)
let (complete, setComplete) = React.useState(_ => false)

let setAreOneClickWalletsRendered = Recoil.useSetRecoilState(areOneClickWalletsRendered)
let (stateJson, setStatesJson) = React.useState(_ => JSON.Encode.null)
Expand All @@ -40,6 +41,8 @@ let make = (~sessionObj: SessionsType.token) => {

PaymentUtils.useStatesJson(setStatesJson)

UtilityHooks.useHandlePostMessages(~complete, ~empty=!complete, ~paymentType="klarna")

React.useEffect(() => {
if (
status === "ready" &&
Expand All @@ -62,16 +65,12 @@ let make = (~sessionObj: SessionsType.token) => {
result => {
let result = result->JSON.Decode.bool->Option.getOr(false)
if result {
UtilityHooks.useHandlePostMessages(
~complete=true,
~empty=false,
~paymentType="klarna",
)
Utils.handlePostMessage([
("fullscreen", true->JSON.Encode.bool),
("param", "paymentloader"->JSON.Encode.string),
("iframeId", iframeId->JSON.Encode.string),
])
setComplete(_ => true)
authorize(
{collect_shipping_address: componentName->getIsExpressCheckoutComponent},
Dict.make()->JSON.Encode.object,
Expand Down
6 changes: 5 additions & 1 deletion src/Payments/PayPal.res
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ let make = () => {
let isManualRetryEnabled = Recoil.useRecoilValueFromAtom(RecoilAtoms.isManualRetryEnabled)

let intent = PaymentHelpers.usePaymentIntent(Some(loggerState), Paypal)
UtilityHooks.useHandlePostMessages(
~complete=paypalClicked,
~empty=!paypalClicked,
~paymentType="paypal",
)
let onPaypalClick = _ev => {
loggerState.setLogInfo(
~value="Paypal Button Clicked",
Expand All @@ -48,7 +53,6 @@ let make = () => {
->then(result => {
let result = result->JSON.Decode.bool->Option.getOr(false)
if result {
UtilityHooks.useHandlePostMessages(~complete=true, ~empty=true, ~paymentType="paypal")
let (connectors, _) =
paymentMethodListValue->PaymentUtils.getConnectors(Wallets(Paypal(Redirect)))
let body = PaymentBody.paypalRedirectionBody(~connectors)
Expand Down
3 changes: 3 additions & 0 deletions src/Payments/PaypalSDK.res
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let make = (~sessionObj: SessionsType.token, ~paymentType: CardThemeType.mode) =
let (loggerState, _setLoggerState) = Recoil.useRecoilState(RecoilAtoms.loggerAtom)
let areOneClickWalletsRendered = Recoil.useSetRecoilState(RecoilAtoms.areOneClickWalletsRendered)
let paymentMethodListValue = Recoil.useRecoilValueFromAtom(PaymentUtils.paymentMethodListValue)
let (complete, setComplete) = React.useState(_ => false)

let token = sessionObj.token
let orderDetails = sessionObj.orderDetails->getOrderDetails(paymentType)
Expand Down Expand Up @@ -51,6 +52,7 @@ let make = (~sessionObj: SessionsType.token, ~paymentType: CardThemeType.mode) =
)

PaymentUtils.useStatesJson(setStatesJson)
UtilityHooks.useHandlePostMessages(~complete, ~empty=!complete, ~paymentType="paypal")

let mountPaypalSDK = () => {
let clientId = sessionObj.token
Expand Down Expand Up @@ -84,6 +86,7 @@ let make = (~sessionObj: SessionsType.token, ~paymentType: CardThemeType.mode) =
~completeAuthorize,
~handleCloseLoader,
~areOneClickWalletsRendered,
~setComplete,
)
})
Window.body->Window.appendChild(paypalScript)
Expand Down
3 changes: 2 additions & 1 deletion src/Payments/PaypalSDKHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let loadPaypalSDK = (
~areOneClickWalletsRendered: (
RecoilAtoms.areOneClickWalletsRendered => RecoilAtoms.areOneClickWalletsRendered
) => unit,
~setComplete,
) => {
loggerState.setLogInfo(
~value="Paypal SDK Button Clicked",
Expand All @@ -30,9 +31,9 @@ let loadPaypalSDK = (
->then(result => {
let result = result->JSON.Decode.bool->Option.getOr(false)
if result {
UtilityHooks.useHandlePostMessages(~complete=true, ~empty=true, ~paymentType="paypal")
let paypalWrapper = GooglePayType.getElementById(Utils.document, "paypal-button")
paypalWrapper.innerHTML = ""
setComplete(_ => true)
paypal["Buttons"]({
style: buttonStyle,
fundingSource: paypal["FUNDING"]["PAYPAL"],
Expand Down

0 comments on commit 6aaae7b

Please sign in to comment.