From db22304eff24d29171aaabc17d5bf5c14b871739 Mon Sep 17 00:00:00 2001 From: Gitanjli <96485413+gitanjli525@users.noreply.github.com> Date: Mon, 25 Nov 2024 17:00:12 +0530 Subject: [PATCH] chore: url path after OMP switch (#1770) Co-authored-by: Gitanjli Chopra --- src/screens/OMPSwitch/MerchantSwitch.res | 2 ++ src/screens/OMPSwitch/OrgSwitch.res | 2 ++ src/screens/OMPSwitch/ProfileSwitch.res | 4 +++- src/utils/GlobalVars.res | 14 +++++++++++++- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/screens/OMPSwitch/MerchantSwitch.res b/src/screens/OMPSwitch/MerchantSwitch.res index 1eb7ac6c5..2a7e123cf 100644 --- a/src/screens/OMPSwitch/MerchantSwitch.res +++ b/src/screens/OMPSwitch/MerchantSwitch.res @@ -129,6 +129,7 @@ let make = () => { let fetchDetails = useGetMethod() let showToast = ToastState.useShowToast() let merchSwitch = OMPSwitchHooks.useMerchantSwitch() + let url = RescriptReactRouter.useUrl() let {userInfo: {merchantId}} = React.useContext(UserInfoProvider.defaultContext) let (showModal, setShowModal) = React.useState(_ => false) let (merchantList, setMerchantList) = Recoil.useRecoilState(HyperswitchAtom.merchantListAtom) @@ -152,6 +153,7 @@ let make = () => { try { setShowSwitchingMerch(_ => true) let _ = await merchSwitch(~expectedMerchantId=value, ~currentMerchantId=merchantId) + RescriptReactRouter.replace(GlobalVars.extractModulePath(url)) setShowSwitchingMerch(_ => false) } catch { | _ => { diff --git a/src/screens/OMPSwitch/OrgSwitch.res b/src/screens/OMPSwitch/OrgSwitch.res index 56f93ca72..e81e13f75 100644 --- a/src/screens/OMPSwitch/OrgSwitch.res +++ b/src/screens/OMPSwitch/OrgSwitch.res @@ -8,6 +8,7 @@ let make = () => { let fetchDetails = useGetMethod() let showToast = ToastState.useShowToast() let orgSwitch = OMPSwitchHooks.useOrgSwitch() + let url = RescriptReactRouter.useUrl() let {userHasAccess} = GroupACLHooks.useUserGroupACLHook() let {userInfo: {orgId}} = React.useContext(UserInfoProvider.defaultContext) let (orgList, setOrgList) = Recoil.useRecoilState(HyperswitchAtom.orgListAtom) @@ -37,6 +38,7 @@ let make = () => { try { setShowSwitchingOrg(_ => true) let _ = await orgSwitch(~expectedOrgId=value, ~currentOrgId=orgId) + RescriptReactRouter.replace(GlobalVars.extractModulePath(url)) setShowSwitchingOrg(_ => false) } catch { | _ => { diff --git a/src/screens/OMPSwitch/ProfileSwitch.res b/src/screens/OMPSwitch/ProfileSwitch.res index db397b69b..1d822fc21 100644 --- a/src/screens/OMPSwitch/ProfileSwitch.res +++ b/src/screens/OMPSwitch/ProfileSwitch.res @@ -152,6 +152,7 @@ let make = () => { let fetchDetails = useGetMethod() let showToast = ToastState.useShowToast() let profileSwitch = OMPSwitchHooks.useProfileSwitch() + let url = RescriptReactRouter.useUrl() let (showModal, setShowModal) = React.useState(_ => false) let {userInfo: {profileId}} = React.useContext(UserInfoProvider.defaultContext) let (profileList, setProfileList) = Recoil.useRecoilState(HyperswitchAtom.profileListAtom) @@ -175,11 +176,12 @@ let make = () => { let addItemBtnStyle = "border border-t-0 w-full" let customScrollStyle = "max-h-72 overflow-scroll px-1 pt-1 border border-b-0" let dropdownContainerStyle = "min-w-[15rem] rounded-md border border-1" + let profileSwitch = async value => { try { setShowSwitchingProfile(_ => true) let _ = await profileSwitch(~expectedProfileId=value, ~currentProfileId=profileId) - RescriptReactRouter.replace(GlobalVars.appendDashboardPath(~url="/home")) + RescriptReactRouter.replace(GlobalVars.extractModulePath(url)) setShowSwitchingProfile(_ => false) } catch { | _ => { diff --git a/src/utils/GlobalVars.res b/src/utils/GlobalVars.res index 4db6c2166..2a8c35075 100644 --- a/src/utils/GlobalVars.res +++ b/src/utils/GlobalVars.res @@ -11,7 +11,8 @@ type appEnv = [#production | #sandbox | #integration | #development] let isLocalhost = Window.Location.hostname === "localhost" || Window.Location.hostname === "127.0.0.1" -let dashboardBasePath = Some("/dashboard") +let dashboardPrefix = "dashboard" +let dashboardBasePath = Some(`/${dashboardPrefix}`) let appendTrailingSlash = url => { url->String.startsWith("/") ? url : `/${url}` @@ -29,6 +30,17 @@ let appendDashboardPath = (~url) => { } } +let extractModulePath = (url: RescriptReactRouter.url) => { + let currentPathList = url.path->List.toArray + + let modulePath = if currentPathList->Array.includes(dashboardPrefix) { + currentPathList->Array.slice(~start=0, ~end=2)->Array.joinWith("/")->appendTrailingSlash + } else { + currentPathList->Array.slice(~start=0, ~end=1)->Array.joinWith("/")->appendTrailingSlash + } + modulePath +} + type hostType = Live | Sandbox | Local | Integ let hostName = Window.Location.hostname