Skip to content

Instantly share code, notes, and snippets.

@gnujoow
Created November 17, 2017 12:42
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 gnujoow/4050f696409e2bd92c118a91c14a0f86 to your computer and use it in GitHub Desktop.
Save gnujoow/4050f696409e2bd92c118a91c14a0f86 to your computer and use it in GitHub Desktop.
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