Guest User

Untitled

a guest
Oct 18th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. export function pushVotePost(option, postId) {
  2. const request = API.postVotePost(option, postId)
  3.  
  4. return dispatch => {
  5. request.then(({ data }) => {
  6. dispatch({ type: POST_VOTE_POST, payload: data, meta: postId })
  7. })
  8. }
  9. }
  10.  
  11. import {
  12. POSTS_GET_POSTS,
  13. POST_VOTE_POST,
  14. POSTS_GET_POST,
  15. } from '../actions/actionConstants'
  16.  
  17. const posts = (state = [], action) => {
  18. const { payload } = action
  19. switch (action.type) {
  20. case POST_VOTE_POST:
  21. return [...state.filter(item => item.id !== payload.id), payload]
  22. case POSTS_GET_POST:
  23. return payload
  24. case POSTS_GET_POSTS:
  25. return payload
  26. default:
  27. return state
  28. }
  29. }
  30.  
  31. export default posts
  32.  
  33. componentDidMount() {
  34. const { getPosts } = this.props
  35. getPosts()
  36. }
  37.  
  38. return _.map(posts, post => {
  39. return (
  40. <div className="post-container" key={post.id}>
  41. <PostComponent
  42. key={post.id}
  43. postId={post.id}
  44. title={post.title}
  45. body={false}
  46. readirect
  47. author={post.author}
  48. voteScore={post.voteScore}
  49. category={post.category}
  50. timestamp={post.timestamp}
  51. />
  52. </div>
  53. )
  54. })
  55.  
  56. export default connect(
  57. state => ({
  58. posts: _.filter(state.posts, ['deleted', false]),
  59. }),
  60. {
  61. getPosts,
  62. }
  63. )(Posts)
  64.  
  65. componentDidMount() {
  66. const { getPost, getComments, match } = this.props
  67. this.props.getPost(match.params.postId)
  68. this.props.getComments(match.params.postId)
  69. }
  70.  
  71. const mapStateToProps = (state, ownProps) => {
  72. const { posts } = state
  73. return {
  74. comments: state.comments[ownProps.match.params.postId],
  75. posts,
  76. }
  77. }
  78.  
  79. const { posts } = this.props
  80. return (
  81. <PostComponent
  82. key={posts.id}
  83. postId={this.props.match.params.postId}
  84. title={posts.title}
  85. body={posts.body}
  86. readirect={false}
  87. author={posts.author}
  88. voteScore={posts.voteScore}
  89. category={posts.category}
  90. timestamp={posts.timestamp}
  91. />
  92. )
  93.  
  94. export default connect(mapStateToProps, { getPost, getComments })(PostDetails)
Add Comment
Please, Sign In to add comment