Skip to content

Commit f9fccf5

Browse files
committed
Add SnapshotsDropdown to Documents component and fix navigateDashboard action
1 parent 82b3ef5 commit f9fccf5

File tree

7 files changed

+31
-11
lines changed

7 files changed

+31
-11
lines changed

rdmo/projects/assets/js/project/actions/projectActions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { updateConfig } from 'rdmo/core/assets/js/actions/configActions'
77
import { baseUrl } from 'rdmo/core/assets/js/utils/meta'
88

99
import { projectId } from '../utils/meta'
10-
import { updateLocation } from '../utils/location'
10+
import { locationKeys, updateLocation } from '../utils/location'
1111

1212
import ProjectApi from '../api/ProjectApi'
1313

@@ -21,7 +21,7 @@ export function navigateDashboard(location) {
2121
updateLocation(location)
2222

2323
// update the location in the config store
24-
Object.keys(location).forEach(key => dispatch(updateConfig(key, location[key], false)))
24+
locationKeys.forEach(key => dispatch(updateConfig(key, location[key] ?? null, false)))
2525

2626
if (!isNil(location.viewId)) {
2727
dispatch(fetchView(location.snapshotId, location.viewId))

rdmo/projects/assets/js/project/components/Main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Plugins from './areas/Plugins'
1212
import View from './helper/View'
1313

1414
const Main = () => {
15-
const { area } = useSelector((state) => state.config)
15+
const { area, snapshotId } = useSelector((state) => state.config)
1616
const { project, currentView } = useSelector((state) => state.project)
1717

1818
const renderArea = () => {
@@ -24,7 +24,7 @@ const Main = () => {
2424
case 'documents':
2525
return currentView ? <View /> : <Documents />
2626
case 'snapshots':
27-
return currentView ? <View /> : <Snapshots />
27+
return currentView ? <View /> : (snapshotId ? <Documents /> : <Snapshots />)
2828
case 'information':
2929
return <Information />
3030
case 'memberships':

rdmo/projects/assets/js/project/components/areas/Documents.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
import React from 'react'
22
import { useDispatch, useSelector } from 'react-redux'
3+
import { isNil } from 'lodash'
34

45
import { downloadView, downloadAnswers, navigateDashboard } from '../../actions/projectActions'
56

7+
import SnapshotsDropdown from '../helper/SnapshotsDropdown'
68
import ViewTile from '../helper/ViewTile'
79

810
const Documents = () => {
911
const dispatch = useDispatch()
1012

13+
const { snapshotId, viewId, detail } = useSelector((state) => state.config)
1114
const { views } = useSelector((state) => state.project.project) ?? {}
15+
const area = snapshotId ? 'snapshots' : 'documents'
16+
17+
const handleSnapshotChange = (snapshot) => {
18+
if (isNil(snapshot)) {
19+
dispatch(navigateDashboard({ area, viewId, detail }))
20+
} else {
21+
dispatch(navigateDashboard({ area, snapshotId: snapshot.id, viewId, detail }))
22+
}
23+
}
1224

1325
return (
1426
<div className="project-documents">
1527
<div className="d-flex justify-content-between align-items-center mb-5">
1628
<h1 className="mb-0">{gettext('Documents')}</h1>
29+
<SnapshotsDropdown onChange={handleSnapshotChange}/>
1730
</div>
1831

1932
<h2>{gettext('Data management plans')}</h2>
@@ -24,7 +37,7 @@ const Documents = () => {
2437
<ViewTile
2538
title={view.title}
2639
help={view.help}
27-
onClick={() => dispatch(navigateDashboard({ area: 'documents', viewId: view.id }))}
40+
onClick={() => dispatch(navigateDashboard({ area, snapshotId, viewId: view.id }))}
2841
onExport={(format) => dispatch(downloadView(null, view.id, format))}
2942
/>
3043
</div>
@@ -38,15 +51,15 @@ const Documents = () => {
3851
<ViewTile
3952
title={gettext('List all questions')}
4053
help={gettext('Overview of all questions')}
41-
onClick={() => dispatch(navigateDashboard({ area: 'documents', detail: 'questions' }))}
54+
onClick={() => dispatch(navigateDashboard({ area, snapshotId, detail: 'questions' }))}
4255
onExport={(format) => {console.log(format)}}
4356
/>
4457
</div>
4558
<div className="col-lg-6">
4659
<ViewTile
4760
title={gettext('List all answers')}
4861
help={gettext('Overview of all questions and answers')}
49-
onClick={() => dispatch(navigateDashboard({ area: 'documents', detail: 'answers' }))}
62+
onClick={() => dispatch(navigateDashboard({ area, snapshotId, detail: 'answers' }))}
5063
onExport={(format) => dispatch(downloadAnswers(null, format))}
5164
/>
5265
</div>

rdmo/projects/assets/js/project/components/areas/snapshots/SnapshotTable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const SnapshotTable = ({ snapshots }) => {
2121
const [selectedSnapshot, setSelectedSnapshot] = useState(null)
2222

2323
const handleShowAnswers = (snapshotId) => {
24-
dispatch(navigateDashboard({ area: 'snapshots', snapshotId, detail: 'answers' }))
24+
dispatch(navigateDashboard({ area: 'snapshots', snapshotId }))
2525
}
2626

2727
const openRollbackModal = (snapshot) => {

rdmo/projects/assets/js/project/components/helper/SnapshotsDropdown.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const SnapshotsDropdown = ({ onChange }) => {
99
const { snapshotId } = useSelector((state) => state.config)
1010
const { snapshots } = useSelector((state) => state.project?.project)
1111

12+
const currentLabel = gettext('Current data')
13+
const currentSnapshot = snapshots.find(snapshot => snapshot.id == snapshotId)
14+
1215
const handleClick = (event, snapshot) => {
1316
event.stopPropagation()
1417

@@ -31,13 +34,14 @@ const SnapshotsDropdown = ({ onChange }) => {
3134
onClick={(event) => event.stopPropagation()}
3235
title={gettext('Snapshots')}
3336
>
34-
{gettext('Snapshot')} <i className="bi bi-caret-down-fill ms-1" />
37+
<span>{currentSnapshot ? currentSnapshot?.title : currentLabel}</span>
38+
<i className="bi bi-caret-down-fill ms-1" />
3539
</button>
3640

3741
<ul className="dropdown-menu">
3842
<button className={classNames('dropdown-item', { active: isNil(snapshotId) })}
3943
onClick={(event) => handleClick(event, null)}>
40-
{gettext('Current')}
44+
{currentLabel}
4145
</button>
4246
{
4347
snapshots.map((snapshot) => (

rdmo/projects/assets/js/project/components/helper/View.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const View = () => {
3535
if (isNil(snapshotId)) {
3636
dispatch(navigateDashboard({ area: 'documents' }))
3737
} else {
38-
dispatch(navigateDashboard({ area: 'snapshots' }))
38+
dispatch(navigateDashboard({ area: 'snapshots', snapshotId }))
3939
}
4040
}
4141

rdmo/projects/assets/js/project/utils/location.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { baseUrl } from 'rdmo/core/assets/js/utils/meta'
22
import { projectId } from '../utils/meta'
33
import { isNil } from 'lodash'
44

5+
export const locationKeys = ['area', 'snapshotId', 'viewId', 'detail']
6+
57
export const parseLocation = () => {
68
let pathname = window.location.pathname
79
let location = {
@@ -15,6 +17,7 @@ export const parseLocation = () => {
1517
const patterns = [
1618
/\/projects\/\d+\/(?<area>(snapshots))\/(?<snapshotId>\d+)\/views\/(?<viewId>\d+)[/]*$/,
1719
/\/projects\/\d+\/(?<area>(snapshots))\/(?<snapshotId>\d+)\/(?<detail>[a-z-]+)[/]*$/,
20+
/\/projects\/\d+\/(?<area>(snapshots))\/(?<snapshotId>\d+)[/]*$/,
1821
/\/projects\/\d+\/(?<area>[a-z-]+)\/views\/(?<viewId>\d+)[/]*$/,
1922
/\/projects\/\d+\/(?<area>[a-z-]+)\/(?<detail>[a-z-]+)[/]*$/,
2023
/\/projects\/\d+\/(?<area>[a-z-]+)[/]*$/

0 commit comments

Comments
 (0)