Skip to content

Instantly share code, notes, and snippets.

@jamesattard
Created September 1, 2018 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesattard/5fa8e44c4fe364230ccb1ef4257ce994 to your computer and use it in GitHub Desktop.
Save jamesattard/5fa8e44c4fe364230ccb1ef4257ce994 to your computer and use it in GitHub Desktop.
Reducer Architecture
import {
FETCHING_NOTES,
FETCH_NOTES,
FETCH_NEW_NOTE,
FETCH_UPDATED_NOTE,
FETCH_CLOSED_NOTE
} from "../actions/types";
const initialState = {
list: [],
isFetching: false
};
export const noteReducer = (state = initialState, action) => {
switch (action.type) {
case FETCHING_NOTES:
return { ...state, isFetching: true };
case FETCH_NOTES:
return { ...state, isFetching: false, list: action.payload.notes };
case FETCH_NEW_NOTE:
return {
...state,
isFetching: false,
list: [...state.list, action.payload.newNote] // Add newNote to list array
};
case FETCH_UPDATED_INCIDENT:
const updatedNotes = state.list.map(note => {
if (note._id === action.id) {
return { ...note, ...action.payload }; // update the particular array element
} else {
return note; // just return the rest of the notes that we don't want to update
}
});
return { ...state, isFetching: false, list: updatedNotes };
case FETCH_CLOSED_NOTE: // delete it from array as we don't want it displayed
return {
...state,
isFetching: false,
list: [...state.list.filter(note => note._id !== action.id)]
};
default:
return state;
}
};
export default noteReducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment