From 67e4a8315a3e73d55b59d258c85be610a1851a78 Mon Sep 17 00:00:00 2001 From: Landon Reed Date: Sat, 5 May 2018 19:21:56 -0400 Subject: [PATCH] fix(editor): clone exceptions to avoid mutation of store fixes #103 --- lib/editor/util/validation.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/editor/util/validation.js b/lib/editor/util/validation.js index 486849006..dcb73bf70 100644 --- a/lib/editor/util/validation.js +++ b/lib/editor/util/validation.js @@ -1,5 +1,5 @@ // @flow - +import clone from 'lodash.clonedeep' import moment from 'moment' import validator from 'validator' @@ -22,7 +22,6 @@ export function validate ( const isRequiredButEmpty = required && doesNotExist(value) let reason = 'Required field must not be empty' const agencies = getTableById(tableData, 'agency') - const scheduleExceptions = getTableById(tableData, 'scheduleexception') switch (inputType) { case 'GTFS_ID': const indices = [] @@ -190,24 +189,24 @@ export function validate ( return false case 'EXCEPTION_DATE': // a date cannot be selected more than once (for all exceptions) const dateMap = {} - // FIXME: this declaration of exceptions is redundant - const exceptions: Array = (scheduleExceptions: Array) + // Clone exceptions to avoid mutating table in store. + const scheduleExceptions: Array = clone(getTableById(tableData, 'scheduleexception')) if (entity) { - const exceptionIndex = exceptions.findIndex(se => se.id === entity.id) + const exceptionIndex = scheduleExceptions.findIndex(se => se.id === entity.id) if (exceptionIndex !== -1) { - exceptions.splice(exceptionIndex, 1) + scheduleExceptions.splice(exceptionIndex, 1) } const castedScheduleException: ScheduleException = ((entity: any): ScheduleException) - exceptions.push(castedScheduleException) + scheduleExceptions.push(castedScheduleException) } - for (let i = 0; i < exceptions.length; i++) { - if (exceptions[i].dates) { - exceptions[i].dates.map(d => { + for (let i = 0; i < scheduleExceptions.length; i++) { + if (scheduleExceptions[i].dates) { + scheduleExceptions[i].dates.map(d => { if (typeof dateMap[d] === 'undefined') { dateMap[d] = [] } - dateMap[d].push(exceptions[i].id) + dateMap[d].push(scheduleExceptions[i].id) }) } }