Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: confirm dialog with interpretation view #1270

Closed
wants to merge 3 commits into from
Closed
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
50 changes: 39 additions & 11 deletions packages/app/src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,22 @@ export class App extends Component {
return false
}

parseLocation = location => {
const pathParts = location.pathname.slice(1).split('/')
const id = pathParts[0]
const interpretationId = pathParts[2]
return { id, interpretationId }
}

loadVisualization = async location => {
const { store } = this.context

if (location.pathname.length > 1) {
// /currentAnalyticalObject
// /${id}/
// /${id}/interpretation/${interpretationId}
const pathParts = location.pathname.slice(1).split('/')
const id = pathParts[0]
const interpretationId = pathParts[2]
const { id, interpretationId } = this.parseLocation(location)

const urlContainsCurrentAOKey = id === CURRENT_AO_KEY

if (urlContainsCurrentAOKey) {
Expand Down Expand Up @@ -153,18 +159,33 @@ export class App extends Component {

this.unlisten = history.listen(location => {
const isSaving = location.state?.isSaving
const isOpening = location.state?.isOpening
const { interpretationId } = this.parseLocation(location)

if (
// currently editing
getVisualizationState(
this.props.visualization,
this.props.current
) === STATE_DIRTY &&
this.state.locationToConfirm !== location &&
// wanting to navigate elsewhere
this.state.previousLocation !== location.pathname &&
// currently *not* viewing an interpretation
!(this.props.interpretation.id || interpretationId) &&
// not saving
!isSaving
) {
this.setState({ locationToConfirm: location })
} else {
if (
isSaving ||
isOpening ||
this.state.previousLocation !== location.pathname
) {
this.loadVisualization(location)
}

this.setState({ locationToConfirm: null })
this.loadVisualization(location)
}
})

Expand Down Expand Up @@ -258,21 +279,27 @@ export class App extends Component {
<ButtonStrip end>
<Button
secondary
onClick={() =>
onClick={() => {
this.setState({
locationToConfirm: null,
})
}

history.goBack()
}}
>
{i18n.t('No, cancel')}
</Button>

<Button
onClick={() =>
history.push(
onClick={() => {
this.loadVisualization(
this.state.locationToConfirm
)
}

this.setState({
locationToConfirm: null,
})
}}
primary
>
{i18n.t('Yes, leave')}
Expand All @@ -291,7 +318,7 @@ export class App extends Component {
const mapStateToProps = state => ({
settings: fromReducers.fromSettings.sGetSettings(state),
current: fromReducers.fromCurrent.sGetCurrent(state),
interpretations: fromReducers.fromVisualization.sGetInterpretations(state),
interpretation: fromReducers.fromUi.sGetUiInterpretation(state),
ui: fromReducers.fromUi.sGetUi(state),
visualization: sGetVisualization(state),
snackbar: fromReducers.fromSnackbar.sGetSnackbar(state),
Expand Down Expand Up @@ -326,6 +353,7 @@ App.propTypes = {
clearVisualization: PropTypes.func,
current: PropTypes.object,
d2: PropTypes.object,
interpretation: PropTypes.object,
location: PropTypes.object,
ouLevels: PropTypes.array,
setCurrentFromUi: PropTypes.func,
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/components/MenuBar/MenuBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import styles from './styles/MenuBar.module.css'
const onOpen = id => {
const path = `/${id}`
if (history.location.pathname === path) {
history.replace(path)
history.replace({ pathname: path, state: { isOpening: true } })
} else {
history.push(path)
}
Expand Down
23 changes: 21 additions & 2 deletions packages/app/src/components/__tests__/App.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,32 @@ describe('App', () => {
})
})

it('reloads visualization when same pathname pushed', done => {
it('reloads visualization when opening the same visualization', done => {
props.location.pathname = '/fluttershy'

app()

setTimeout(() => {
history.replace('/fluttershy')
history.replace({
pathname: '/fluttershy',
state: { isOpening: true },
})
expect(actions.tDoLoadVisualization).toBeCalledTimes(2)

done()
})
})

it('reloads visualization when same pathname pushed when saving', done => {
props.location.pathname = '/fluttershy'

app()

setTimeout(() => {
history.replace({
pathname: '/fluttershy',
state: { isSaving: true },
})
expect(actions.tDoLoadVisualization).toBeCalledTimes(2)

done()
Expand Down