@@ -205,21 +205,27 @@ export function addStopToPattern (pattern, stop, index) {
205
205
const newStop = stopToPatternStop ( stop , stopSequence )
206
206
207
207
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.
211
209
patternStops . push ( newStop )
212
- dispatch ( updatePatternStops ( pattern , patternStops ) )
213
210
if ( hasShapePoints ) {
214
211
// console.log('extending pattern to new stop', stop)
215
212
// If a pattern shape already exists, extend it from the current end
216
213
// point to the new stop.
217
214
const { shapePtLon, shapePtLat} = shapePoints [ shapePoints . length - 1 ]
218
215
const currentEndPoint = ll . toLeaflet ( [ shapePtLon , shapePtLat ] )
219
216
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.
222
218
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
+ } )
223
229
} else {
224
230
// Otherwise, check if a shape ought to be created. Then, save.
225
231
if ( patternStops . length === 2 && followStreets ) {
@@ -235,6 +241,8 @@ export function addStopToPattern (pattern, stop, index) {
235
241
const points = [ previousStop , stop ]
236
242
. map ( ( stop , index ) => ( { lng : stop . stop_lon , lat : stop . stop_lat } ) )
237
243
const patternSegments = await getPolyline ( points , true )
244
+ // Update pattern stops and geometry.
245
+ dispatch ( updatePatternStops ( pattern , patternStops ) )
238
246
const controlPoints = controlPointsFromSegments ( patternStops , patternSegments )
239
247
dispatch ( updatePatternGeometry ( { controlPoints, patternSegments} ) )
240
248
}
@@ -434,16 +442,11 @@ function extendPatternToPoint (pattern, endPoint, newEndPoint, stop = null, spli
434
442
}
435
443
clonedControlPoints . push ( controlPoint )
436
444
}
437
- // Update pattern geometry and control points
438
- const result = {
445
+ // Return updated pattern geometry and control points
446
+ return {
439
447
controlPoints : clonedControlPoints ,
440
448
patternSegments : newPatternSegments
441
449
}
442
- if ( stop ) {
443
- dispatch ( updatePatternGeometry ( result ) )
444
- await dispatch ( saveActiveGtfsEntity ( 'trippattern' ) )
445
- }
446
- return result
447
450
}
448
451
}
449
452
0 commit comments