Skip to content

Commit

Permalink
chore: url path after OMP switch (#1770)
Browse files Browse the repository at this point in the history
Co-authored-by: Gitanjli Chopra <[email protected]>
  • Loading branch information
gitanjli525 and Gitanjli Chopra authored Nov 25, 2024
1 parent 9bb9284 commit db22304
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/screens/OMPSwitch/MerchantSwitch.res
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -152,6 +153,7 @@ let make = () => {
try {
setShowSwitchingMerch(_ => true)
let _ = await merchSwitch(~expectedMerchantId=value, ~currentMerchantId=merchantId)
RescriptReactRouter.replace(GlobalVars.extractModulePath(url))
setShowSwitchingMerch(_ => false)
} catch {
| _ => {
Expand Down
2 changes: 2 additions & 0 deletions src/screens/OMPSwitch/OrgSwitch.res
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -37,6 +38,7 @@ let make = () => {
try {
setShowSwitchingOrg(_ => true)
let _ = await orgSwitch(~expectedOrgId=value, ~currentOrgId=orgId)
RescriptReactRouter.replace(GlobalVars.extractModulePath(url))
setShowSwitchingOrg(_ => false)
} catch {
| _ => {
Expand Down
4 changes: 3 additions & 1 deletion src/screens/OMPSwitch/ProfileSwitch.res
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
| _ => {
Expand Down
14 changes: 13 additions & 1 deletion src/utils/GlobalVars.res
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand All @@ -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
Expand Down

0 comments on commit db22304

Please sign in to comment.