From a45f57795199ac90f8e12630c8ffca70ba4c9e7d Mon Sep 17 00:00:00 2001 From: Shiva Nandan Date: Mon, 11 Mar 2024 00:25:47 +0530 Subject: [PATCH] refactor: remove rescript-webapi as a dependency --- bsconfig.json | 1 - package-lock.json | 5 ---- package.json | 1 - src/Hooks/OutsideClick.res | 31 ++++++++++++------------ src/PaymentOptions.res | 11 ++++++--- src/Utilities/PaymentHelpers.res | 2 +- src/Window.res | 9 +++++-- src/orca-loader/LoaderPaymentElement.res | 2 +- 8 files changed, 32 insertions(+), 30 deletions(-) diff --git a/bsconfig.json b/bsconfig.json index dd5109f18..8083e7e60 100644 --- a/bsconfig.json +++ b/bsconfig.json @@ -20,7 +20,6 @@ "bs-dependencies": [ "@rescript/react", "@ryyppy/rescript-promise", - "rescript-webapi", "@rescript/core", "@glennsl/rescript-fetch" ] diff --git a/package-lock.json b/package-lock.json index 7410071ef..4c0805fb7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,6 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "recoil": "^0.1.2", - "rescript-webapi": "^0.7.0", "webpack-merge": "^5.9.0" }, "devDependencies": { @@ -24877,10 +24876,6 @@ "rescript": "rescript" } }, - "node_modules/rescript-webapi": { - "version": "0.7.0", - "license": "MIT" - }, "node_modules/resolve": { "version": "1.22.8", "dev": true, diff --git a/package.json b/package.json index bb5148bf0..3cd0e1500 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,6 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "recoil": "^0.1.2", - "rescript-webapi": "^0.7.0", "webpack-merge": "^5.9.0" }, "scripts": { diff --git a/src/Hooks/OutsideClick.res b/src/Hooks/OutsideClick.res index 10328e076..fe5b59b6d 100644 --- a/src/Hooks/OutsideClick.res +++ b/src/Hooks/OutsideClick.res @@ -1,5 +1,6 @@ -external ffToDomType: Dom.eventTarget => Dom.node_like<'a> = "%identity" -external ffToWebDom: Js.Nullable.t => Js.Nullable.t = "%identity" +external ffToDomType: {..} => Dom.node_like<'a> = "%identity" +@send external contains: (Dom.element, {..}) => bool = "contains" + type ref = | ArrayOfRef(array>>) | RefArray(React.ref>>) @@ -27,14 +28,14 @@ let useOutsideClick = ( let eventCallback = useEvent0(callback) React.useEffect1(() => { if isActive { - let handleClick = (e: Dom.event) => { - let targ = Webapi.Dom.Event.target(e) + let handleClick = (e: ReactEvent.Mouse.t) => { + let targ = e->ReactEvent.Mouse.target let isInsideClick = switch refs { | ArrayOfRef(refs) => refs->Js.Array2.reduce((acc, ref: React.ref>) => { - let isClickInsideRef = switch ffToWebDom(ref.current)->Js.Nullable.toOption { - | Some(element) => element->Webapi.Dom.Element.contains(~child=ffToDomType(targ)) + let isClickInsideRef = switch ref.current->Js.Nullable.toOption { + | Some(element) => element->contains(targ) | None => false } acc || isClickInsideRef @@ -43,8 +44,8 @@ let useOutsideClick = ( refs.current ->Js.Array2.slice(~start=0, ~end_=-1) ->Js.Array2.reduce((acc, ref: Js.Nullable.t) => { - let isClickInsideRef = switch ffToWebDom(ref)->Js.Nullable.toOption { - | Some(element) => element->Webapi.Dom.Element.contains(~child=ffToDomType(targ)) + let isClickInsideRef = switch ref->Js.Nullable.toOption { + | Some(element) => element->contains(targ) | None => false } acc || isClickInsideRef @@ -53,8 +54,8 @@ let useOutsideClick = ( let isClickInsideOfContainer = switch containerRefs { | Some(ref) => - switch ffToWebDom(ref.current)->Js.Nullable.toOption { - | Some(element) => element->Webapi.Dom.Element.contains(~child=ffToDomType(targ)) + switch ref.current->Js.Nullable.toOption { + | Some(element) => element->contains(targ) | None => false } | None => true @@ -66,18 +67,18 @@ let useOutsideClick = ( } Js.Global.setTimeout(() => { - events->Js.Array2.forEach( + events->Array.forEach( event => { - Webapi.Dom.window->Webapi.Dom.Window.addEventListener(event, handleClick) + Window.addEventListener(event, handleClick) }, ) }, 50)->ignore Some( () => { - events->Js.Array2.forEach(event => - Webapi.Dom.window->Webapi.Dom.Window.removeEventListener(event, handleClick) - ) + events->Array.forEach(event => { + Window.removeEventListener(event, handleClick) + }) }, ) } else { diff --git a/src/PaymentOptions.res b/src/PaymentOptions.res index a6873afcd..9949c2a4b 100644 --- a/src/PaymentOptions.res +++ b/src/PaymentOptions.res @@ -59,7 +59,7 @@ let make = ( let (toggleIconElement, setToggleIconElement) = React.useState(_ => false) React.useEffect2(() => { let width = switch payOptionsRef.current->Js.Nullable.toOption { - | Some(ref) => Webapi.Dom.Element.clientWidth(ref) + | Some(ref) => ref->Window.Element.clientWidth | None => 0 } setCardsContainerWidth(_ => width) @@ -87,9 +87,12 @@ let make = ( setMoreIconIndex(prev => mod(prev + 1, dropDownOptionsDetails->Belt.Array.length)) setToggleIconElement(_ => true) - Js.Global.setTimeout(() => { - setToggleIconElement(_ => false) - }, 10)->ignore + Js.Global.setTimeout( + () => { + setToggleIconElement(_ => false) + }, + 10, + )->ignore } }, 5000) diff --git a/src/Utilities/PaymentHelpers.res b/src/Utilities/PaymentHelpers.res index a67feac5f..35bacc01b 100644 --- a/src/Utilities/PaymentHelpers.res +++ b/src/Utilities/PaymentHelpers.res @@ -135,7 +135,7 @@ let rec intentCall = ( ) let handleOpenUrl = url => { if isPaymentSession { - Window.location.replace(. url) + Window.Location.replace(. url) } else { openUrl(url) } diff --git a/src/Window.res b/src/Window.res index c96077da3..6806147fd 100644 --- a/src/Window.res +++ b/src/Window.res @@ -126,5 +126,10 @@ let isInteg = hostname === "dev.hyperswitch.io" let isProd = hostname === "checkout.hyperswitch.io" -type location = {replace: (. string) => unit} -@val @scope("window") external location: location = "location" +module Location = { + @val @scope(("window", "location")) external replace: (. string) => unit = "replace" +} + +module Element = { + @get external clientWidth: Dom.element => int = "clientWidth" +} diff --git a/src/orca-loader/LoaderPaymentElement.res b/src/orca-loader/LoaderPaymentElement.res index b618f27fc..359155a91 100644 --- a/src/orca-loader/LoaderPaymentElement.res +++ b/src/orca-loader/LoaderPaymentElement.res @@ -185,7 +185,7 @@ let make = (componentType, options, setIframeRef, iframeRef, mountPostMessage) = switch eventDataObject->getOptionalJsonFromJson("openurl") { | Some(val) => { let url = val->getStringfromjson("") - Window.location.replace(. url) + Window.Location.replace(. url) } | None => () }