/
observations_reducer.js
72 lines (70 loc) · 1.86 KB
/
observations_reducer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import _ from "lodash";
import {
RECEIVE_OBSERVATIONS,
UPDATE_OBSERVATION_IN_COLLECTION,
UPDATE_ALL_LOCAL,
SET_REVIEWING,
SET_PLACES_BY_ID,
SET_LAST_REQUEST_AT
} from "../actions";
const observationsReducer = ( state = {
results: [],
reviewing: false,
placesByID: {},
lastRequestAt: null
}, action ) => {
if ( action.type === RECEIVE_OBSERVATIONS ) {
return Object.assign( {}, state, {
totalResults: action.totalResults,
page: action.page,
totalPages: action.totalPages,
results: action.results
} );
}
if ( action.type === UPDATE_OBSERVATION_IN_COLLECTION ) {
const newState = Object.assign( {}, state, {
results: _.cloneDeep( state.results ).map( obs => {
if ( obs.id !== action.observation.id ) {
return obs;
}
const newObs = _.cloneDeep( obs );
_.forOwn( action.changes, ( v, k ) => {
newObs[k] = v;
} );
return newObs;
} )
} );
return newState;
}
if ( action.type === UPDATE_ALL_LOCAL ) {
const newState = Object.assign( {}, state, {
results: _.cloneDeep( state.results ).map( obs => {
const newObs = _.cloneDeep( obs );
_.forOwn( action.changes, ( v, k ) => {
newObs[k] = v;
} );
return newObs;
} )
} );
return newState;
}
if ( action.type === SET_REVIEWING ) {
const newState = Object.assign( {}, state, {
reviewing: action.reviewing
} );
return newState;
}
if ( action.type === SET_PLACES_BY_ID ) {
const newState = Object.assign( {}, state, {
placesByID: Object.assign( state.placesByID, action.placesByID )
} );
return newState;
}
if ( action.type === SET_LAST_REQUEST_AT ) {
return Object.assign( {}, state, {
lastRequestAt: action.lastRequestAt
} );
}
return state;
};
export default observationsReducer;