Skip to content

Commit 15299f4

Browse files
committed
fix(editor): fix frequency trip editing for SQL editor
1 parent cd01332 commit 15299f4

File tree

8 files changed

+64
-9
lines changed

8 files changed

+64
-9
lines changed

lib/editor/actions/trip.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ export function fetchTripsForCalendar (feedId, pattern, calendarId) {
3535
id: pattern_id
3636
trips (service_id: $service_id, limit: -1) {
3737
id
38+
frequencies {
39+
startTime: start_time
40+
endTime: end_time
41+
headwaySecs: headway_secs
42+
exactTimes: exact_times
43+
}
3844
tripId: trip_id
3945
tripHeadsign: trip_headsign
4046
tripShortName: trip_short_name
@@ -160,6 +166,10 @@ export function saveMultipleTripsForCalendar (feedId, pattern, calendarId, trips
160166
}
161167
}
162168

169+
/**
170+
* Delete multiple trips. This method takes the provided trips and maps the trips'
171+
* IDs to a comma-separated query parameter, indicating which trips to delete.
172+
*/
163173
export function deleteTripsForCalendar (feedId, pattern, calendarId, trips) {
164174
return function (dispatch, getState) {
165175
let errorCount = 0

lib/editor/actions/tripPattern.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,16 @@ export function saveTripPattern (feedId, tripPattern) {
7474
})
7575
}
7676
}
77+
78+
/**
79+
* Deletes all trips for a given pattern ID. This is used to clear the trips for
80+
* a pattern if/when a pattern is changed from frequency-based to timetable-baed.
81+
*/
82+
export function deleteAllTripsForPattern (feedId, patternId) {
83+
return function (dispatch, getState) {
84+
const url = `/api/editor/secure/pattern/${patternId}/trips?feedId=${feedId}`
85+
return dispatch(secureFetch(url, 'delete'))
86+
.then(res => res.json())
87+
.then(json => json && json.result === 'OK')
88+
}
89+
}

lib/editor/components/pattern/EditSchedulePanel.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,24 @@ export default class EditSchedulePanel extends Component {
2929
}
3030

3131
_onChangeUseFrequency = key => {
32+
const {activePattern, deleteAllTripsForPattern, feedSource, saveActiveEntity, showConfirmModal, updateActiveEntity} = this.props
3233
const useFrequency = key !== 'timetables' ? 1 : 0
3334
const unselectedOption = key === 'timetables' ? 'frequencies' : 'timetables'
34-
this.props.showConfirmModal({
35-
title: `Use ${key} for ${this.props.activePattern.name}?`,
35+
showConfirmModal({
36+
title: `Use ${key} for ${activePattern.name}?`,
3637
body: `Are you sure you want to use ${key} for this trip pattern? Any trips created using ${unselectedOption} will be lost.`,
3738
onConfirm: () => {
38-
this.props.updateActiveEntity(this.props.activePattern, 'trippattern', {useFrequency})
39-
this.props.saveActiveEntity('trippattern')
39+
// Update and save useFrequency field
40+
updateActiveEntity(activePattern, 'trippattern', {useFrequency})
41+
saveActiveEntity('trippattern')
42+
// Then, delete all trips for the pattern.
43+
.then(() => deleteAllTripsForPattern(feedSource.id, activePattern.patternId))
4044
}
4145
})
4246
}
4347

4448
render () {
45-
const { activePattern, activePatternId } = this.props
49+
const {activePattern, activePatternId} = this.props
4650
const timetableOptions = [
4751
<span><Icon type='table' /> Use timetables</span>,
4852
<span><Icon type='clock-o' /> Use frequencies</span>

lib/editor/components/timetable/Timetable.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export default class Timetable extends Component {
7878
updateCellValue={updateCellValue}
7979
columns={columns}
8080
hideDepartureTimes={hideDepartureTimes}
81+
saveEditedTrips={this.saveEditedTrips}
8182
setActiveCell={this.setActiveCell}
8283
{...this.props}
8384
{...stepperProps}

lib/editor/components/timetable/TimetableEditor.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ export default class TimetableEditor extends Component {
105105
objectPath.set(newRow, 'id', ENTITY.NEW_ID)
106106
objectPath.set(newRow, 'tripId', null)
107107
objectPath.set(newRow, 'useFrequency', activePattern.useFrequency)
108+
if (activePattern.useFrequency) {
109+
// If a frequency-based trip, never use exact times. NOTE: there is no
110+
// column to edit this field in the timetable grid.
111+
objectPath.set(newRow, 'frequencies.0.exactTimes', 0)
112+
}
108113
objectPath.set(newRow, 'feedId', this.props.feedSource.id)
109114
objectPath.set(newRow, 'patternId', activePattern.patternId)
110115
objectPath.set(newRow, 'routeId', activePattern.routeId)
@@ -249,6 +254,7 @@ export default class TimetableEditor extends Component {
249254
toggleRowSelection={toggleRowSelection}
250255
toggleAllRows={toggleAllRows}
251256
scrollToRow={scrollToRow}
257+
saveEditedTrips={this.saveEditedTrips}
252258
scrollToColumn={scrollToColumn}
253259
{...this.props} />
254260
: <p className='lead text-center'>

lib/editor/components/timetable/TimetableGrid.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default class TimetableGrid extends Component {
3030
static propTypes = {
3131
updateCellValue: PropTypes.func,
3232
toggleAllRows: PropTypes.func,
33+
saveEditedTrips: PropTypes.func,
3334
selected: PropTypes.array,
3435
toggleRowSelection: PropTypes.func
3536
}
@@ -50,7 +51,10 @@ export default class TimetableGrid extends Component {
5051
handleKeyPress = (evt) => {
5152
const {
5253
activeCell,
54+
activePattern,
55+
activeScheduleId,
5356
data,
57+
saveEditedTrips,
5458
setActiveCell,
5559
scrollToColumn,
5660
scrollToRow,
@@ -67,7 +71,14 @@ export default class TimetableGrid extends Component {
6771
break
6872
case 13: // Enter
6973
if (!activeCell) {
70-
return setActiveCell(`${scrollToRow}-${scrollToColumn}`)
74+
if (evt.metaKey || evt.ctrlKey) {
75+
// If Enter is pressed with CTRL or CMD and no cell is active, save
76+
// any unsaved trips.
77+
return saveEditedTrips(activePattern, activeScheduleId)
78+
} else {
79+
// Otherwise, set active cell
80+
return setActiveCell(`${scrollToRow}-${scrollToColumn}`)
81+
}
7182
} else {
7283
return setActiveCell(null)
7384
}
@@ -242,6 +253,7 @@ export default class TimetableGrid extends Component {
242253
(selected[0] !== '*' && selected.indexOf(rowIndex) !== -1)
243254

244255
let val = objectPath.get(data[rowIndex], col.key)
256+
// console.log(val, data[rowIndex], col.key)
245257
if (col.key === 'tripId' && val === null) {
246258
val = objectPath.get(data[rowIndex], 'id') !== ENTITY.NEW_ID ? objectPath.get(data[rowIndex], 'id') : null
247259
}

lib/editor/containers/ActiveTripPatternList.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {addStopToPattern, removeStopFromPattern} from '../actions/map/stopStrate
1616
import {updatePatternGeometry} from '../actions/map'
1717
import {setErrorMessage} from '../../manager/actions/status'
1818
import {
19+
deleteAllTripsForPattern,
1920
resnapStops,
2021
setActiveStop,
2122
setActivePatternSegment,
@@ -79,6 +80,7 @@ const mapDispatchToProps = {
7980
newGtfsEntity,
8081
// Trip pattern specific actions
8182
addStopToPattern,
83+
deleteAllTripsForPattern,
8284
removeStopFromPattern,
8385
resnapStops,
8486
setActivePatternSegment,

lib/editor/selectors/timetable.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,31 @@ export const getTimetableColumns = createSelector(
7070
columns.push({
7171
name: 'Start time',
7272
width: 100,
73-
key: 'startTime',
73+
key: 'frequencies.0.startTime',
7474
type: 'TIME',
7575
placeholder: 'HH:MM:SS'
7676
})
7777
columns.push({
7878
name: 'End time',
7979
width: 100,
80-
key: 'endTime',
80+
key: 'frequencies.0.endTime',
8181
type: 'TIME',
8282
placeholder: 'HH:MM:SS'
8383
})
8484
columns.push({
8585
name: 'Headway',
8686
width: 60,
87-
key: 'headway',
87+
key: 'frequencies.0.headwaySecs',
8888
type: 'MINUTES',
8989
placeholder: '15 (min)'
9090
})
91+
// columns.push({
92+
// name: 'Exact times',
93+
// width: 60,
94+
// key: 'frequencies.0.exactTimes',
95+
// type: 'MINUTES',
96+
// placeholder: '15 (min)'
97+
// })
9198
}
9299
}
93100
return columns

0 commit comments

Comments
 (0)