From 0724fa9e36defba751fb1493bc19ce03125c81de Mon Sep 17 00:00:00 2001 From: Kashif Date: Thu, 7 Nov 2024 14:09:17 +0530 Subject: [PATCH] refactor: use custom hook in a right way --- src/Hooks/UtilityHooks.res | 14 ++++++++++++++ src/LoaderController.res | 14 +++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/Hooks/UtilityHooks.res b/src/Hooks/UtilityHooks.res index ed06c76c5..ec1e28674 100644 --- a/src/Hooks/UtilityHooks.res +++ b/src/Hooks/UtilityHooks.res @@ -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 +} diff --git a/src/LoaderController.res b/src/LoaderController.res index 41816e36a..795c65a30 100644 --- a/src/LoaderController.res +++ b/src/LoaderController.res @@ -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) @@ -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)]) @@ -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) => @@ -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) =>