Skip to content

Commit 36f5d0f

Browse files
committed
fix(pattern): fix add stop to pattern after fresh fetch
1 parent e31ad3f commit 36f5d0f

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

lib/editor/actions/map/stopStrategies.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,21 +205,27 @@ export function addStopToPattern (pattern, stop, index) {
205205
const newStop = stopToPatternStop(stop, stopSequence)
206206

207207
if (missingIndex || index === patternStops.length) {
208-
// If there is no index, handle adding stop to end (also a proxy for
209-
// extending pattern to point).
210-
// First, add stop to list of pattern stops.
208+
// Push pattern stop to cloned list.
211209
patternStops.push(newStop)
212-
dispatch(updatePatternStops(pattern, patternStops))
213210
if (hasShapePoints) {
214211
// console.log('extending pattern to new stop', stop)
215212
// If a pattern shape already exists, extend it from the current end
216213
// point to the new stop.
217214
const {shapePtLon, shapePtLat} = shapePoints[shapePoints.length - 1]
218215
const currentEndPoint = ll.toLeaflet([shapePtLon, shapePtLat])
219216
const {stop_lon: lng, stop_lat: lat} = stop
220-
// Extend pattern to the new point. NOTE: This includes saving the
221-
// pattern.
217+
// Extend pattern to the new point.
222218
return dispatch(extendPatternToPoint(pattern, currentEndPoint, {lng, lat}, stop))
219+
.then(result => {
220+
// Update pattern stops and pattern geometry together. This ensures
221+
// that a recalcuation of the control points / pattern segments does
222+
// not cause issues when the pattern stops quantity does not match
223+
// the control points. TODO: add optional pattern stops to update pattern
224+
// geometry, so that these are more closely bound.
225+
dispatch(updatePatternStops(pattern, patternStops))
226+
dispatch(updatePatternGeometry(result))
227+
return dispatch(saveActiveGtfsEntity('trippattern'))
228+
})
223229
} else {
224230
// Otherwise, check if a shape ought to be created. Then, save.
225231
if (patternStops.length === 2 && followStreets) {
@@ -235,6 +241,8 @@ export function addStopToPattern (pattern, stop, index) {
235241
const points = [previousStop, stop]
236242
.map((stop, index) => ({lng: stop.stop_lon, lat: stop.stop_lat}))
237243
const patternSegments = await getPolyline(points, true)
244+
// Update pattern stops and geometry.
245+
dispatch(updatePatternStops(pattern, patternStops))
238246
const controlPoints = controlPointsFromSegments(patternStops, patternSegments)
239247
dispatch(updatePatternGeometry({controlPoints, patternSegments}))
240248
}
@@ -434,16 +442,11 @@ function extendPatternToPoint (pattern, endPoint, newEndPoint, stop = null, spli
434442
}
435443
clonedControlPoints.push(controlPoint)
436444
}
437-
// Update pattern geometry and control points
438-
const result = {
445+
// Return updated pattern geometry and control points
446+
return {
439447
controlPoints: clonedControlPoints,
440448
patternSegments: newPatternSegments
441449
}
442-
if (stop) {
443-
dispatch(updatePatternGeometry(result))
444-
await dispatch(saveActiveGtfsEntity('trippattern'))
445-
}
446-
return result
447450
}
448451
}
449452

0 commit comments

Comments
 (0)