Skip to content

Commit

Permalink
remove site alias and locale from location.state.directedFrom path
Browse files Browse the repository at this point in the history
  • Loading branch information
sandragolden committed Mar 15, 2023
1 parent cd4e64f commit a75d49f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/template-retail-react-app/app/pages/login/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Seo from '../../components/seo'
import {useForm} from 'react-hook-form'
import {useLocation} from 'react-router-dom'
import useEinstein from '../../commerce-api/hooks/useEinstein'

import {removeSiteLocaleFromPath} from '../../utils/url'
import LoginForm from '../../components/login'

const Login = () => {
Expand Down Expand Up @@ -45,7 +45,7 @@ const Login = () => {
useEffect(() => {
if (customer.authType != null && customer.isRegistered) {
if (location?.state?.directedFrom) {
navigate(location.state.directedFrom)
navigate(removeSiteLocaleFromPath(location.state.directedFrom))
} else {
navigate('/account')
}
Expand Down
26 changes: 26 additions & 0 deletions packages/template-retail-react-app/app/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,29 @@ export const removeQueryParamsFromPath = (path, keys) => {

return `${pathname}${paramStr && '?'}${paramStr}`
}

/*
* Remove site alias and locale from a given url, to be used for "navigate" urls
*
* @param {string} pathName - The part of url to have site alias and locale removed from
* @returns {string} - the path after site alias and locale have been removed
* @example
* import {removeSiteLocaleFromPath} from /path/to/util/url
*
* removeSiteLocaleFromPath(/RefArch/en-US/account/wishlist)
* // returns '/account/wishlist'
*/
export const removeSiteLocaleFromPath = (pathName) => {
let {siteRef, localeRef} = getParamsFromPath(`${pathName}`)

// remove the site alias from the current pathName
if (siteRef) {
pathName = pathName.replace(`/${siteRef}`, '')
}
// remove the locale from the current pathName
if (localeRef) {
pathName = pathName.replace(`/${localeRef}`, '')
}

return pathName
}

0 comments on commit a75d49f

Please sign in to comment.