1
1
import update from 'react-addons-update'
2
2
import { getConfigProperty } from '../../common/util/config'
3
3
import { defaultSorter } from '../../common/util/util'
4
-
5
4
const projects = ( state = {
6
5
isFetching : false ,
7
6
all : null ,
@@ -132,14 +131,12 @@ const projects = (state = {
132
131
}
133
132
} )
134
133
} 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 } }
141
138
}
142
- )
139
+ } )
143
140
} else {
144
141
return update ( state ,
145
142
{
@@ -154,26 +151,14 @@ const projects = (state = {
154
151
}
155
152
projectIndex = state . all . findIndex ( p => p . id === action . feedSource . projectId )
156
153
const existingSources = state . all [ projectIndex ] . feedSources || [ ]
157
- let updatedSources
158
154
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 ] }
171
158
return update ( state , {
172
159
all : {
173
160
[ projectIndex ] : {
174
- $merge : {
175
- feedSources : updatedSources
176
- }
161
+ feedSources : updatedSources
177
162
}
178
163
} ,
179
164
isFetching : { $set : false }
@@ -208,28 +193,15 @@ const projects = (state = {
208
193
versionIndex = state . all [ projectIndex ] . feedSources [ sourceIndex ] . feedVersions
209
194
? state . all [ projectIndex ] . feedSources [ sourceIndex ] . feedVersions . findIndex ( v => v . id === action . feedVersion . id )
210
195
: - 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 ] }
225
199
return update ( state , {
226
200
all : {
227
201
[ projectIndex ] : {
228
202
feedSources : {
229
203
[ sourceIndex ] : {
230
- $merge : {
231
- feedVersions : updatedVersions
232
- }
204
+ feedVersions : updatedVersions
233
205
}
234
206
}
235
207
}
@@ -280,6 +252,33 @@ const projects = (state = {
280
252
}
281
253
}
282
254
} )
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
+ } )
283
282
case 'RECEIVE_FEEDVERSION_ISOCHRONES' :
284
283
projectIndex = state . all . findIndex ( p => p . id === action . feedSource . projectId )
285
284
sourceIndex = state . all [ projectIndex ] . feedSources . findIndex ( s => s . id === action . feedSource . id )
@@ -366,14 +365,13 @@ const projects = (state = {
366
365
case 'RECEIVE_NOTES_FOR_FEEDSOURCE' :
367
366
projectIndex = state . all . findIndex ( p => p . id === action . feedSource . projectId )
368
367
sourceIndex = state . all [ projectIndex ] . feedSources . findIndex ( s => s . id === action . feedSource . id )
368
+ const { notes} = action
369
369
return update ( state , {
370
370
all : {
371
371
[ projectIndex ] : {
372
372
feedSources : {
373
373
[ sourceIndex ] : {
374
- $merge : {
375
- notes : action . notes
376
- }
374
+ $merge : { notes}
377
375
}
378
376
}
379
377
}
@@ -424,4 +422,21 @@ const projects = (state = {
424
422
}
425
423
}
426
424
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
+
427
442
export default projects
0 commit comments