Skip to content

Commit

Permalink
fix(editor): fix frequency trip editing for SQL editor
Browse files Browse the repository at this point in the history
  • Loading branch information
landonreed committed Feb 12, 2018
1 parent cd01332 commit 15299f4
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 9 deletions.
10 changes: 10 additions & 0 deletions lib/editor/actions/trip.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export function fetchTripsForCalendar (feedId, pattern, calendarId) {
id: pattern_id
trips (service_id: $service_id, limit: -1) {
id
frequencies {
startTime: start_time
endTime: end_time
headwaySecs: headway_secs
exactTimes: exact_times
}
tripId: trip_id
tripHeadsign: trip_headsign
tripShortName: trip_short_name
Expand Down Expand Up @@ -160,6 +166,10 @@ export function saveMultipleTripsForCalendar (feedId, pattern, calendarId, trips
}
}

/**
* Delete multiple trips. This method takes the provided trips and maps the trips'
* IDs to a comma-separated query parameter, indicating which trips to delete.
*/
export function deleteTripsForCalendar (feedId, pattern, calendarId, trips) {
return function (dispatch, getState) {
let errorCount = 0
Expand Down
13 changes: 13 additions & 0 deletions lib/editor/actions/tripPattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,16 @@ export function saveTripPattern (feedId, tripPattern) {
})
}
}

/**
* Deletes all trips for a given pattern ID. This is used to clear the trips for
* a pattern if/when a pattern is changed from frequency-based to timetable-baed.
*/
export function deleteAllTripsForPattern (feedId, patternId) {
return function (dispatch, getState) {
const url = `/api/editor/secure/pattern/${patternId}/trips?feedId=${feedId}`
return dispatch(secureFetch(url, 'delete'))
.then(res => res.json())
.then(json => json && json.result === 'OK')
}
}
14 changes: 9 additions & 5 deletions lib/editor/components/pattern/EditSchedulePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,24 @@ export default class EditSchedulePanel extends Component {
}

_onChangeUseFrequency = key => {
const {activePattern, deleteAllTripsForPattern, feedSource, saveActiveEntity, showConfirmModal, updateActiveEntity} = this.props
const useFrequency = key !== 'timetables' ? 1 : 0
const unselectedOption = key === 'timetables' ? 'frequencies' : 'timetables'
this.props.showConfirmModal({
title: `Use ${key} for ${this.props.activePattern.name}?`,
showConfirmModal({
title: `Use ${key} for ${activePattern.name}?`,
body: `Are you sure you want to use ${key} for this trip pattern? Any trips created using ${unselectedOption} will be lost.`,
onConfirm: () => {
this.props.updateActiveEntity(this.props.activePattern, 'trippattern', {useFrequency})
this.props.saveActiveEntity('trippattern')
// Update and save useFrequency field
updateActiveEntity(activePattern, 'trippattern', {useFrequency})
saveActiveEntity('trippattern')
// Then, delete all trips for the pattern.
.then(() => deleteAllTripsForPattern(feedSource.id, activePattern.patternId))
}
})
}

render () {
const { activePattern, activePatternId } = this.props
const {activePattern, activePatternId} = this.props
const timetableOptions = [
<span><Icon type='table' /> Use timetables</span>,
<span><Icon type='clock-o' /> Use frequencies</span>
Expand Down
1 change: 1 addition & 0 deletions lib/editor/components/timetable/Timetable.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default class Timetable extends Component {
updateCellValue={updateCellValue}
columns={columns}
hideDepartureTimes={hideDepartureTimes}
saveEditedTrips={this.saveEditedTrips}
setActiveCell={this.setActiveCell}
{...this.props}
{...stepperProps}
Expand Down
6 changes: 6 additions & 0 deletions lib/editor/components/timetable/TimetableEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ export default class TimetableEditor extends Component {
objectPath.set(newRow, 'id', ENTITY.NEW_ID)
objectPath.set(newRow, 'tripId', null)
objectPath.set(newRow, 'useFrequency', activePattern.useFrequency)
if (activePattern.useFrequency) {
// If a frequency-based trip, never use exact times. NOTE: there is no
// column to edit this field in the timetable grid.
objectPath.set(newRow, 'frequencies.0.exactTimes', 0)
}
objectPath.set(newRow, 'feedId', this.props.feedSource.id)
objectPath.set(newRow, 'patternId', activePattern.patternId)
objectPath.set(newRow, 'routeId', activePattern.routeId)
Expand Down Expand Up @@ -249,6 +254,7 @@ export default class TimetableEditor extends Component {
toggleRowSelection={toggleRowSelection}
toggleAllRows={toggleAllRows}
scrollToRow={scrollToRow}
saveEditedTrips={this.saveEditedTrips}
scrollToColumn={scrollToColumn}
{...this.props} />
: <p className='lead text-center'>
Expand Down
14 changes: 13 additions & 1 deletion lib/editor/components/timetable/TimetableGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class TimetableGrid extends Component {
static propTypes = {
updateCellValue: PropTypes.func,
toggleAllRows: PropTypes.func,
saveEditedTrips: PropTypes.func,
selected: PropTypes.array,
toggleRowSelection: PropTypes.func
}
Expand All @@ -50,7 +51,10 @@ export default class TimetableGrid extends Component {
handleKeyPress = (evt) => {
const {
activeCell,
activePattern,
activeScheduleId,
data,
saveEditedTrips,
setActiveCell,
scrollToColumn,
scrollToRow,
Expand All @@ -67,7 +71,14 @@ export default class TimetableGrid extends Component {
break
case 13: // Enter
if (!activeCell) {
return setActiveCell(`${scrollToRow}-${scrollToColumn}`)
if (evt.metaKey || evt.ctrlKey) {
// If Enter is pressed with CTRL or CMD and no cell is active, save
// any unsaved trips.
return saveEditedTrips(activePattern, activeScheduleId)
} else {
// Otherwise, set active cell
return setActiveCell(`${scrollToRow}-${scrollToColumn}`)
}
} else {
return setActiveCell(null)
}
Expand Down Expand Up @@ -242,6 +253,7 @@ export default class TimetableGrid extends Component {
(selected[0] !== '*' && selected.indexOf(rowIndex) !== -1)

let val = objectPath.get(data[rowIndex], col.key)
// console.log(val, data[rowIndex], col.key)
if (col.key === 'tripId' && val === null) {
val = objectPath.get(data[rowIndex], 'id') !== ENTITY.NEW_ID ? objectPath.get(data[rowIndex], 'id') : null
}
Expand Down
2 changes: 2 additions & 0 deletions lib/editor/containers/ActiveTripPatternList.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {addStopToPattern, removeStopFromPattern} from '../actions/map/stopStrate
import {updatePatternGeometry} from '../actions/map'
import {setErrorMessage} from '../../manager/actions/status'
import {
deleteAllTripsForPattern,
resnapStops,
setActiveStop,
setActivePatternSegment,
Expand Down Expand Up @@ -79,6 +80,7 @@ const mapDispatchToProps = {
newGtfsEntity,
// Trip pattern specific actions
addStopToPattern,
deleteAllTripsForPattern,
removeStopFromPattern,
resnapStops,
setActivePatternSegment,
Expand Down
13 changes: 10 additions & 3 deletions lib/editor/selectors/timetable.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,31 @@ export const getTimetableColumns = createSelector(
columns.push({
name: 'Start time',
width: 100,
key: 'startTime',
key: 'frequencies.0.startTime',
type: 'TIME',
placeholder: 'HH:MM:SS'
})
columns.push({
name: 'End time',
width: 100,
key: 'endTime',
key: 'frequencies.0.endTime',
type: 'TIME',
placeholder: 'HH:MM:SS'
})
columns.push({
name: 'Headway',
width: 60,
key: 'headway',
key: 'frequencies.0.headwaySecs',
type: 'MINUTES',
placeholder: '15 (min)'
})
// columns.push({
// name: 'Exact times',
// width: 60,
// key: 'frequencies.0.exactTimes',
// type: 'MINUTES',
// placeholder: '15 (min)'
// })
}
}
return columns
Expand Down

0 comments on commit 15299f4

Please sign in to comment.