Skip to content

Commit

Permalink
refactor: use custom hook in a right way
Browse files Browse the repository at this point in the history
  • Loading branch information
kashif-m committed Nov 7, 2024
1 parent d39aeaf commit 0724fa9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
14 changes: 14 additions & 0 deletions src/Hooks/UtilityHooks.res
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,17 @@ let useSendEventsToParent = eventsToSendToParent => {
Some(() => {Window.removeEventListener("message", handle)})
})
}

let useUpdateShouldUseTopRedirection = () => {
let setShouldUseTopRedirection = Recoil.useSetRecoilState(RecoilAtoms.shouldUseTopRedirectionAtom)
let updateTopRedirectionAtom = paymentOptions => {
paymentOptions
->Dict.get("shouldUseTopRedirection")
->Option.flatMap(JSON.Decode.bool)
->Option.map(useTop => {
setShouldUseTopRedirection(_ => useTop)
})
->ignore
}
updateTopRedirectionAtom
}
14 changes: 3 additions & 11 deletions src/LoaderController.res
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger, ~initTime
let (_, setSessions) = Recoil.useRecoilState(sessions)
let (options, setOptions) = Recoil.useRecoilState(elementOptions)
let (optionsPayment, setOptionsPayment) = Recoil.useRecoilState(optionAtom)
let setShouldUseTopRedirection = Recoil.useSetRecoilState(shouldUseTopRedirectionAtom)
let setSessionId = Recoil.useSetRecoilState(sessionId)
let setBlockConfirm = Recoil.useSetRecoilState(isConfirmBlocked)
let setCustomPodUri = Recoil.useSetRecoilState(customPodUri)
Expand Down Expand Up @@ -147,14 +146,7 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger, ~initTime
}
}

let updateTopRedirectionAtom = paymentOptions =>
paymentOptions
->Dict.get("shouldUseTopRedirection")
->Option.flatMap(JSON.Decode.bool)
->Option.map(useTop => {
setShouldUseTopRedirection(_ => useTop)
})
->ignore
let updateShouldUseTopRedirection = UtilityHooks.useUpdateShouldUseTopRedirection()

React.useEffect0(() => {
messageParentWindow([("iframeMounted", true->JSON.Encode.bool)])
Expand Down Expand Up @@ -286,7 +278,7 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger, ~initTime
logger.setClientSecret(clientSecret)

// Update top redirection atom
updateTopRedirectionAtom(paymentOptions)
updateShouldUseTopRedirection(paymentOptions)

switch getThemePromise(paymentOptions) {
| Some(promise) =>
Expand Down Expand Up @@ -336,7 +328,7 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger, ~initTime
logger.setClientSecret(clientSecret)

// Update top redirection atom
updateTopRedirectionAtom(paymentOptions)
updateShouldUseTopRedirection(paymentOptions)

switch getThemePromise(paymentOptions) {
| Some(promise) =>
Expand Down

0 comments on commit 0724fa9

Please sign in to comment.