Skip to content

Instantly share code, notes, and snippets.

@gnujoow gnujoow/postRedux.js
Created Nov 17, 2017

Embed
What would you like to do?
postRedux
import {
GET_ALL_POSTS,
GET_POSTS,
POST_POST,
PUT_POST,
DELETE_POST,
POST_VOTE_POST,
} from '../actions/ActionTypes';
const INITIAL_STATE = {
allPosts: [],
};
export default (state = INITIAL_STATE, action) => {
switch (action.type) {
case GET_ALL_POSTS:
return {
...state,
allPosts: action.posts,
};
case POST_VOTE_POST:
const votedPosts = state.allPosts.map(post => {
if (post.id === action.id) {
post.voteScore = action.voteScore;
console.log('action', action.voteScore, post);
}
return post;
});
console.log('votedPosts', votedPosts);
return { ...state, allPosts: votedPosts };
// case GET_POSTS:
// return state;
// case POST_POST:
// return state;
// case PUT_POST:
// return state;
case DELETE_POST:
const allPosts = state.allPosts.filter(post => {
return post.id !== action.id;
});
return { ...state, allPosts };z
default:
return state;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.