11import update from 'react-addons-update'
22import { getConfigProperty } from '../../common/util/config'
33import { defaultSorter } from '../../common/util/util'
4-
54const projects = ( state = {
65 isFetching : false ,
76 all : null ,
@@ -132,14 +131,12 @@ const projects = (state = {
132131 }
133132 } )
134133 } else if ( feeds . length ) { // if projectId does not match active project
135- return update ( state ,
136- {
137- isFetching : { $set : false } ,
138- all : {
139- [ projectIndex ] : { $merge : { feedSources : feeds } }
140- }
134+ return update ( state , {
135+ isFetching : { $set : false } ,
136+ all : {
137+ [ projectIndex ] : { $merge : { feedSources : feeds } }
141138 }
142- )
139+ } )
143140 } else {
144141 return update ( state ,
145142 {
@@ -154,26 +151,14 @@ const projects = (state = {
154151 }
155152 projectIndex = state . all . findIndex ( p => p . id === action . feedSource . projectId )
156153 const existingSources = state . all [ projectIndex ] . feedSources || [ ]
157- let updatedSources
158154 sourceIndex = existingSources . findIndex ( s => s . id === action . feedSource . id )
159- if ( sourceIndex === - 1 ) { // source does not currently; add it
160- updatedSources = [
161- ...existingSources ,
162- action . feedSource
163- ]
164- } else { // existing feedsource array includes this one, replace it
165- updatedSources = [
166- ...existingSources . slice ( 0 , sourceIndex ) ,
167- action . feedSource ,
168- ...existingSources . slice ( sourceIndex + 1 )
169- ]
170- }
155+ const updatedSources = sourceIndex !== - 1
156+ ? { [ sourceIndex ] : { $set : action . feedSource } }
157+ : { $set : [ action . feedSource ] }
171158 return update ( state , {
172159 all : {
173160 [ projectIndex ] : {
174- $merge : {
175- feedSources : updatedSources
176- }
161+ feedSources : updatedSources
177162 }
178163 } ,
179164 isFetching : { $set : false }
@@ -208,28 +193,15 @@ const projects = (state = {
208193 versionIndex = state . all [ projectIndex ] . feedSources [ sourceIndex ] . feedVersions
209194 ? state . all [ projectIndex ] . feedSources [ sourceIndex ] . feedVersions . findIndex ( v => v . id === action . feedVersion . id )
210195 : - 1
211- const existingVersions = state . all [ projectIndex ] . feedSources [ sourceIndex ] . feedVersions || [ ]
212- let updatedVersions
213- if ( versionIndex === - 1 ) { // version does not currently; add it
214- updatedVersions = [
215- ...existingVersions ,
216- action . feedVersion
217- ]
218- } else { // existing feedversion array includes this one, replace it
219- updatedVersions = [
220- ...existingVersions . slice ( 0 , versionIndex ) ,
221- action . feedVersion ,
222- ...existingVersions . slice ( versionIndex + 1 )
223- ]
224- }
196+ const updatedVersions = versionIndex !== - 1
197+ ? { [ versionIndex ] : { $set : action . feedVersion } }
198+ : { $set : [ action . feedVersion ] }
225199 return update ( state , {
226200 all : {
227201 [ projectIndex ] : {
228202 feedSources : {
229203 [ sourceIndex ] : {
230- $merge : {
231- feedVersions : updatedVersions
232- }
204+ feedVersions : updatedVersions
233205 }
234206 }
235207 }
@@ -280,6 +252,33 @@ const projects = (state = {
280252 }
281253 }
282254 } )
255+ case 'RECEIVE_VALIDATION_ERRORS' :
256+ projectIndex = state . all . findIndex ( p => p . id === action . payload . feedVersion . feedSource . projectId )
257+ sourceIndex = state . all [ projectIndex ] . feedSources . findIndex ( s => s . id === action . payload . feedVersion . feedSource . id )
258+ versionIndex = state . all [ projectIndex ] . feedSources [ sourceIndex ] . feedVersions . findIndex ( v => v . id === action . payload . feedVersion . id )
259+ const errorIndex = state . all [ projectIndex ] . feedSources [ sourceIndex ] . feedVersions [ versionIndex ] . validationResult . error_counts . findIndex ( e => e . type === action . payload . errorType )
260+ const newErrors = state . all [ projectIndex ] . feedSources [ sourceIndex ] . feedVersions [ versionIndex ] . validationResult . error_counts [ errorIndex ] . errors
261+ ? { $push : action . payload . errors }
262+ : { $set : action . payload . errors }
263+ return update ( state , {
264+ all : {
265+ [ projectIndex ] : {
266+ feedSources : {
267+ [ sourceIndex ] : {
268+ feedVersions : {
269+ [ versionIndex ] : {
270+ validationResult : {
271+ error_counts : {
272+ [ errorIndex ] : { errors : newErrors }
273+ }
274+ }
275+ }
276+ }
277+ }
278+ }
279+ }
280+ }
281+ } )
283282 case 'RECEIVE_FEEDVERSION_ISOCHRONES' :
284283 projectIndex = state . all . findIndex ( p => p . id === action . feedSource . projectId )
285284 sourceIndex = state . all [ projectIndex ] . feedSources . findIndex ( s => s . id === action . feedSource . id )
@@ -366,14 +365,13 @@ const projects = (state = {
366365 case 'RECEIVE_NOTES_FOR_FEEDSOURCE' :
367366 projectIndex = state . all . findIndex ( p => p . id === action . feedSource . projectId )
368367 sourceIndex = state . all [ projectIndex ] . feedSources . findIndex ( s => s . id === action . feedSource . id )
368+ const { notes} = action
369369 return update ( state , {
370370 all : {
371371 [ projectIndex ] : {
372372 feedSources : {
373373 [ sourceIndex ] : {
374- $merge : {
375- notes : action . notes
376- }
374+ $merge : { notes}
377375 }
378376 }
379377 }
@@ -424,4 +422,21 @@ const projects = (state = {
424422 }
425423}
426424
425+ // TODO: Use this function to get indexes (perhaps set to idx variable?)
426+ // function getIndexesFromFeed ({state, feedVersion, feedSource, projectId}) {
427+ // if (!feedSource && feedVersion) ({feedSource} = feedVersion)
428+ // if (!projectId) ({projectId} = feedSource)
429+ // const projectIndex = state.all.findIndex(p => p.id === projectId)
430+ // const sources = state.all[projectIndex].feedSources || []
431+ // const sourceIndex = feedSource && sources.findIndex(s => s.id === feedSource.id)
432+ // const versionIndex = feedVersion && sources[sourceIndex].feedVersions
433+ // ? sources[sourceIndex].feedVersions.findIndex(v => v.id === feedVersion.id)
434+ // : -1
435+ // return {
436+ // projectIndex,
437+ // sourceIndex,
438+ // versionIndex
439+ // }
440+ // }
441+
427442export default projects
0 commit comments