Skip to content

Instantly share code, notes, and snippets.

@sidzan
Created September 29, 2018 19:35
Show Gist options
  • Save sidzan/5835aa26d819dafa1c1f279f16db237c to your computer and use it in GitHub Desktop.
Save sidzan/5835aa26d819dafa1c1f279f16db237c to your computer and use it in GitHub Desktop.
/*
* projectReducer
*
* The reducer takes care of our data. Using actions, we can change our
* application state.
* To add a new action, add it to the switch statement in the reducer function
*
* Example:
* case YOUR_ACTION_CONSTANT:
* return state.set('yourStateVariable', true);
*/
import {
fromJS,
toJS
} from 'immutable';
import {
CHANGE_USERNAME,
LOAD_DATA,
LOAD_DATA_ERROR,
LOAD_DATA_SUCCESS,
ADD_CONTENT,
ADD_CONTENT_SUCCESS,
ADD_CONTENT_FAILURE
} from './constants';
// The initial state of the App
const initialState = fromJS({
content: []
});
function projectReducer(state = initialState, action) {
console.log("project.reduicer", action)
switch (action.type) {
case LOAD_DATA:
return state
.set('loading', true)
.set('error', false)
.setIn(['contents'], []);
case LOAD_DATA_SUCCESS:
return state
.setIn(['contents'], action.data)
.set('loading', false)
.set('error', false);
case ADD_CONTENT:
console.log("reducer.ADD_CONTENT")
return state
.set('loading', true)
.set('error', false)
// return return update(state[state.set(['data'],data)
case ADD_CONTENT_SUCCESS:
console.log('addContentSuccess', action.vals);
// let data = state.get('contents');
// data.push(action.vals)
return state
.updateIn(['contents'], list => list.push(action.vals)) //<<<<<THis is failing
.set('loading', false)
.set('error', false)
break;
default:
return state;
}
}
export default projectReducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment