Created
November 17, 2017 12:42
-
-
Save gnujoow/4050f696409e2bd92c118a91c14a0f86 to your computer and use it in GitHub Desktop.
postRedux
This file contains hidden or 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 { | |
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