Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rdmo/projects/assets/js/project/actions/projectActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import * as actionTypes from './actionTypes'
export function setPage(page) {
return function(dispatch) {
dispatch(updateConfig('page', page))
updateLocation(page)
updateLocation({ page })
}
}

Expand Down
7 changes: 4 additions & 3 deletions rdmo/projects/assets/js/project/store/configureStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ export default function configureStore() {
)

const getConfigFromLocation = () => {
const { page, itemId, itemAction } = parseLocation()
const { page, pageId, action, actionId } = parseLocation()

store.dispatch(configActions.updateConfig('page', page, false))
store.dispatch(configActions.updateConfig('itemId', itemId, false))
store.dispatch(configActions.updateConfig('itemAction', itemAction, false))
store.dispatch(configActions.updateConfig('pageId', pageId, false))
store.dispatch(configActions.updateConfig('action', action, false))
store.dispatch(configActions.updateConfig('actionId', actionId, false))
}

// this event is triggered when the page first loads
Expand Down
46 changes: 20 additions & 26 deletions rdmo/projects/assets/js/project/utils/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,41 @@ import { isEmpty } from 'lodash'
export const parseLocation = () => {
const pathname = window.location.pathname

const m1 = pathname.match(/\/projects\/\d+\/(?<page>[a-z-]+)\/(?<itemId>\d+)\/(?<itemAction>[a-z-]+)[/]*$/)
if (m1) {
return m1.groups
}

const m2 = pathname.match(/\/projects\/\d+\/(?<page>[a-z-]+)\/(?<itemId>\d+)[/]*$/)
if (m2) {
return m2.groups
}

const m3 = pathname.match(/\/projects\/\d+\/(?<page>[a-z-]+)[/]*$/)
if (m3) {
return m3.groups
const patterns = [
/\/projects\/\d+\/(?<page>[a-z-]+)\/(?<pageId>\d+)\/(?<action>[a-z-]+)\/(?<actionId>\d+)[/]*$/,
/\/projects\/\d+\/(?<page>[a-z-]+)\/(?<pageId>\d+)\/(?<action>[a-z-]+)[/]*$/,
/\/projects\/\d+\/(?<page>[a-z-]+)\/(?<action>[a-z-]+)\/(?<actionId>\d+)[/]*$/,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need this pattern also:
/\/projects\/\d+\/(?<page>[a-z-]+)\/(?<pageId>\d+)[/]*$/,

to reflect eg. /projects/1/snapshots/1/ for the documents page with a snapshot selected

/\/projects\/\d+\/(?<page>[a-z-]+)\/(?<action>[a-z-]+)[/]*$/,
/\/projects\/\d+\/(?<page>[a-z-]+)[/]*$/
]

for (const pattern of patterns) {
const match = pathname.match(pattern)
if (match) return match.groups
}

return {
page: ''
}
}

export const updateLocation = (page, itemId, itemAction) => {
const pathname = buildPath(page, itemId, itemAction)
export const updateLocation = ({ page, pageId, action, actionId }) => {
const pathname = buildPath({ page, pageId, action, actionId })
if (pathname != window.location.pathname) {
history.pushState(null, null, pathname)
}
}

export const buildPath = (page, itemId, itemAction) => {
let path = `${baseUrl}/projects/${projectId}/`
export const buildPath = ({ page, pageId, action, actionId }) => {
const segments = [baseUrl, 'projects', projectId]

if (!isEmpty(page)) {
path += page + '/'

if (!isEmpty(itemId)) {
path += itemId + '/'
segments.push(page)

if (!isEmpty(itemAction)) {
path += itemAction + '/'
}
}
if (!isEmpty(pageId)) segments.push(pageId)
if (!isEmpty(action)) segments.push(action)
if (!isEmpty(actionId)) segments.push(actionId)
}

return path
return segments.join('/') + '/'
}