Created
September 1, 2018 07:17
-
-
Save jamesattard/5fa8e44c4fe364230ccb1ef4257ce994 to your computer and use it in GitHub Desktop.
Reducer Architecture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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