Skip to content

Instantly share code, notes, and snippets.

@AntonioErdeljac
Created May 25, 2018 19:04
Show Gist options
  • Save AntonioErdeljac/6491aaed1dbe72134e10df5e5cc2484c to your computer and use it in GitHub Desktop.
Save AntonioErdeljac/6491aaed1dbe72134e10df5e5cc2484c to your computer and use it in GitHub Desktop.
export default (state={articles: []}, action) => {
switch(action.type) {
case 'HOME_PAGE_LOADED':
return {
...state,
articles: action.data.articles,
};
case 'SUBMIT_ARTICLE':
return {
...state,
articles: ([action.data.article]).concat(state.articles),
};
case 'DELETE_ARTICLE':
return {
...state,
articles: state.articles.filter((article) => article._id !== action.id),
};
case 'SET_EDIT':
return {
...state,
articleToEdit: action.article,
};
case 'EDIT_ARTICLE':
return {
...state,
articles: state.articles.map((article) => {
if(article._id === action.data.article._id) {
return {
...action.data.article,
}
}
return article;
}),
articleToEdit: undefined,
}
default:
return state;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment