diff --git a/app/helpers/index.js b/app/helpers/index.js index 2ba34345cfc3f3ba3c112ee8dd74871ddd408be7..c1b0986da9a26f90711acf0a421e1aabd5b3e791 100644 --- a/app/helpers/index.js +++ b/app/helpers/index.js @@ -25,3 +25,10 @@ export const buildUrl = (stops) => { } return "https://www.google.com/maps/dir/" + stops.join("/") + "/" } + +export const dropTail = (array) => { + while (array.length > 0 && array.slice(-1)[0].length == 0) { + array.pop() + } + return array +} diff --git a/app/reducers/index.js b/app/reducers/index.js index a5a227d2a8120c4bf0d982c051710f25c8952178..ce6d7234f246a2d28c4eb2add5f99371db258e80 100644 --- a/app/reducers/index.js +++ b/app/reducers/index.js @@ -1,5 +1,5 @@ import * as at from '../actions/actionTypes' -import { extractStops } from '../helpers' +import { extractStops, dropTail } from '../helpers' const initialState = { @@ -12,13 +12,13 @@ const reducer = (state = initialState, action) => { return {routes: state.routes.concat([extractStops(action.payload)])} case at.DELETE_ROUTE: - return {routes: state.routes.filter((v, i) => i != action.payload)} + return {routes: dropTail(state.routes.filter((v, i) => i != action.payload))} case at.CLEAR_ROUTE: - return {routes: state.routes.map((v, i) => i == action.payload ? [] : v)} + return {routes: dropTail(state.routes.map((v, i) => i == action.payload ? [] : v))} case at.UPDATE_URL: - return {routes: state.routes.map((v, i) => i == action.payload.index ? extractStops(action.payload.url) : v)} + return {routes: dropTail(state.routes.map((v, i) => i == action.payload.index ? extractStops(action.payload.url) : v))} default: return state