1
+ import clone from 'clone'
2
+ import moment from 'moment'
3
+ import { planParamsToQuery } from '@opentripplanner/core-utils/lib/query'
4
+ import { OTP_API_DATE_FORMAT } from '@opentripplanner/core-utils/lib/time'
5
+ import qs from 'qs'
1
6
import { createAction } from 'redux-actions'
2
7
8
+ import { routingQuery } from './api'
9
+ import { setQueryParam } from './form'
3
10
import { routeTo } from './ui'
4
11
import { secureFetch } from '../util/middleware'
5
12
import { isNewUser } from '../util/user'
@@ -192,7 +199,12 @@ export function fetchUserMonitoredTrips () {
192
199
* then, if that was successful, alerts (optional)
193
200
* and refreshes the redux monitoredTrips with the updated trip.
194
201
*/
195
- export function createOrUpdateUserMonitoredTrip ( tripData , isNew , silentOnSuccess ) {
202
+ export function createOrUpdateUserMonitoredTrip (
203
+ tripData ,
204
+ isNew ,
205
+ silentOnSuccess ,
206
+ noRedirect
207
+ ) {
196
208
return async function ( dispatch , getState ) {
197
209
const { accessToken, apiBaseUrl, apiKey } = getMiddlewareVariables ( getState ( ) )
198
210
const { id } = tripData
@@ -220,6 +232,8 @@ export function createOrUpdateUserMonitoredTrip (tripData, isNew, silentOnSucces
220
232
// Reload user's monitored trips after add/update.
221
233
await dispatch ( fetchUserMonitoredTrips ( ) )
222
234
235
+ if ( noRedirect ) return
236
+
223
237
// Finally, navigate to the saved trips page.
224
238
dispatch ( routeTo ( '/savedtrips' ) )
225
239
} else {
@@ -228,12 +242,39 @@ export function createOrUpdateUserMonitoredTrip (tripData, isNew, silentOnSucces
228
242
}
229
243
}
230
244
245
+ /**
246
+ * Toggles the isActive status of a monitored trip
247
+ */
248
+ export function togglePauseTrip ( trip ) {
249
+ return function ( dispatch , getState ) {
250
+ const clonedTrip = clone ( trip )
251
+ clonedTrip . isActive = ! clonedTrip . isActive
252
+
253
+ // Silent update of existing trip.
254
+ dispatch ( createOrUpdateUserMonitoredTrip ( clonedTrip , false , true , true ) )
255
+ }
256
+ }
257
+
258
+ /**
259
+ * Toggles the snoozed status of a monitored trip
260
+ */
261
+ export function toggleSnoozeTrip ( trip ) {
262
+ return function ( dispatch , getState ) {
263
+ const newTrip = clone ( trip )
264
+ newTrip . snoozed = ! newTrip . snoozed
265
+
266
+ // Silent update of existing trip.
267
+ dispatch ( createOrUpdateUserMonitoredTrip ( newTrip , false , true , true ) )
268
+ }
269
+ }
270
+
231
271
/**
232
272
* Deletes a logged-in user's monitored trip,
233
273
* then, if that was successful, refreshes the redux monitoredTrips state.
234
274
*/
235
- export function deleteUserMonitoredTrip ( tripId ) {
275
+ export function confirmAndDeleteUserMonitoredTrip ( tripId ) {
236
276
return async function ( dispatch , getState ) {
277
+ if ( ! confirm ( 'Would you like to remove this trip?' ) ) return
237
278
const { accessToken, apiBaseUrl, apiKey } = getMiddlewareVariables ( getState ( ) )
238
279
const requestUrl = `${ apiBaseUrl } ${ API_MONITORED_TRIP_PATH } /${ tripId } `
239
280
@@ -334,3 +375,29 @@ export function checkItineraryExistence (trip) {
334
375
}
335
376
}
336
377
}
378
+
379
+ /**
380
+ * Plans a new trip for the current date given the query parameters in the given
381
+ * monitored trip
382
+ */
383
+ export function planNewTripFromMonitoredTrip ( monitoredTrip ) {
384
+ return function ( dispatch , getState ) {
385
+ // update query params in store
386
+ const newQuery = planParamsToQuery ( qs . parse ( monitoredTrip . queryParams ) )
387
+ newQuery . date = moment ( ) . format ( OTP_API_DATE_FORMAT )
388
+
389
+ dispatch ( setQueryParam ( newQuery ) )
390
+
391
+ dispatch ( routeTo ( '/' ) )
392
+
393
+ // This prevents some kind of race condition whose origin I can't figure
394
+ // out. Unless this is called after redux catches up with routing to the '/'
395
+ // path, then the old path will be used and the screen won't change.
396
+ // Therefore, this timeout occurs so that the view of the homepage has time
397
+ // to render itself.
398
+ // FIXME: remove hack
399
+ setTimeout ( ( ) => {
400
+ dispatch ( routingQuery ( ) )
401
+ } , 300 )
402
+ }
403
+ }
0 commit comments