Skip to content

Commit

Permalink
fix(editor): clone exceptions to avoid mutation of store
Browse files Browse the repository at this point in the history
fixes #103
  • Loading branch information
landonreed committed May 5, 2018
1 parent 36c602c commit 67e4a83
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions lib/editor/util/validation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow

import clone from 'lodash.clonedeep'
import moment from 'moment'
import validator from 'validator'

Expand All @@ -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 = []
Expand Down Expand Up @@ -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<ScheduleException> = (scheduleExceptions: Array<ScheduleException>)
// Clone exceptions to avoid mutating table in store.
const scheduleExceptions: Array<ScheduleException> = 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)
})
}
}
Expand Down

0 comments on commit 67e4a83

Please sign in to comment.