Skip to content

Commit fb581ac

Browse files
authored
Merge pull request #400 from opentripplanner/trip-navigation-away-warning
Trip page: Add warning about navigating away with unsaved changes
2 parents f18fa7e + 01d34e1 commit fb581ac

File tree

2 files changed

+55
-22
lines changed

2 files changed

+55
-22
lines changed

Diff for: lib/components/user/monitored-trip/trip-basics-pane.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import {
1010
ProgressBar
1111
} from 'react-bootstrap'
1212
import { connect } from 'react-redux'
13+
import { Prompt } from 'react-router'
1314
import styled from 'styled-components'
1415

1516
import * as userActions from '../../../actions/user'
1617
import { getErrorStates } from '../../../util/ui'
18+
1719
import TripStatus from './trip-status'
1820
import TripSummary from './trip-summary'
1921

@@ -87,9 +89,25 @@ class TripBasicsPane extends Component {
8789
}
8890

8991
render () {
90-
const { errors, isCreating, itineraryExistence, values: monitoredTrip } = this.props
92+
const {
93+
canceled,
94+
dirty,
95+
errors,
96+
isCreating,
97+
isSubmitting,
98+
itineraryExistence,
99+
values: monitoredTrip
100+
} = this.props
91101
const { itinerary } = monitoredTrip
92102

103+
// Prevent user from leaving when form has been changed,
104+
// but don't show it when they click submit or cancel.
105+
const unsavedChanges = dirty && !isSubmitting && !canceled
106+
// Message changes depending on if the new or existing trip is being edited
107+
const unsavedChangesNewTripMessage = 'You haven\'t saved your new trip yet. If you leave, it will be lost.'
108+
const unsavedChangesExistingTripMessage = 'You haven\'t saved your trip yet. If you leave, changes will be lost.'
109+
const unsavedChangesMessage = isCreating ? unsavedChangesNewTripMessage : unsavedChangesExistingTripMessage
110+
93111
if (!itinerary) {
94112
return <div>No itinerary to display.</div>
95113
} else {
@@ -107,6 +125,13 @@ class TripBasicsPane extends Component {
107125

108126
return (
109127
<div>
128+
{/* TODO: This component does not block navigation on reload or using the back button.
129+
This will have to be done at a higher level. See #376 */}
130+
<Prompt
131+
when={unsavedChanges}
132+
message={unsavedChangesMessage}
133+
/>
134+
110135
{/* Do not show trip status when saving trip for the first time
111136
(it doesn't exist in backend yet). */}
112137
{!isCreating && <TripStatus monitoredTrip={monitoredTrip} />}

Diff for: lib/components/user/stacked-pane-display.js

+29-21
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
11
import PropTypes from 'prop-types'
2-
import React from 'react'
2+
import React, {useState} from 'react'
33

44
import FormNavigationButtons from './form-navigation-buttons'
55
import { PageHeading, StackedPaneContainer } from './styled'
66

77
/**
88
* This component handles the flow between screens for new OTP user accounts.
99
*/
10-
const StackedPaneDisplay = ({ onCancel, paneSequence, title }) => (
11-
<>
12-
{title && <PageHeading>{title}</PageHeading>}
13-
{
14-
paneSequence.map(({ pane: Pane, props, title }, index) => (
10+
const StackedPaneDisplay = ({ onCancel, paneSequence, title }) => {
11+
// Create indicator of if cancel button was clicked so that child components can know
12+
const [isBeingCanceled, updateBeingCanceled] = useState(false)
13+
14+
return (
15+
<>
16+
{title && <PageHeading>{title}</PageHeading>}
17+
{paneSequence.map(({ pane: Pane, props, title }, index) => (
1518
<StackedPaneContainer key={index}>
1619
<h3>{title}</h3>
17-
<div><Pane {...props} /></div>
20+
<div>
21+
<Pane canceled={isBeingCanceled} {...props} />
22+
</div>
1823
</StackedPaneContainer>
19-
))
20-
}
24+
))}
2125

22-
<FormNavigationButtons
23-
backButton={{
24-
onClick: onCancel,
25-
text: 'Cancel'
26-
}}
27-
okayButton={{
28-
text: 'Save Preferences',
29-
type: 'submit'
30-
}}
31-
/>
32-
</>
33-
)
26+
<FormNavigationButtons
27+
backButton={{
28+
onClick: () => {
29+
updateBeingCanceled(true)
30+
onCancel()
31+
},
32+
text: 'Cancel'
33+
}}
34+
okayButton={{
35+
text: 'Save Preferences',
36+
type: 'submit'
37+
}}
38+
/>
39+
</>
40+
)
41+
}
3442

3543
StackedPaneDisplay.propTypes = {
3644
onCancel: PropTypes.func.isRequired,

0 commit comments

Comments
 (0)