PostFeed.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import React, { useMemo, useState, useEffect } from 'react'
  2. import {
  3. actionAllPostsFeed,
  4. actionFullAllGetPosts,
  5. actionAddFullComment,
  6. actionFindSubComment,
  7. actionAddSubFullComment,
  8. actionFindLikes,
  9. actionGetFindLiked,
  10. actionDeleteFullLike,
  11. actionAddFullLikeForFeed,
  12. actionDeleteFullLikeForFeed,
  13. actionAddFullLike,
  14. } from '../actions'
  15. import {
  16. actionFullFeed,
  17. actionClearFeedPosts,
  18. actionAddFullCommentFeed,
  19. actionAddFullLikeFeed,
  20. actionDeleteFullLikeFeed,
  21. } from '../reducers'
  22. import { Link } from 'react-router-dom'
  23. import { Provider, connect } from 'react-redux'
  24. import { Upload, Button, DatePicker, Space } from 'antd'
  25. import user from '../materials/user.png'
  26. import { Avatar, Image, Divider, Radio } from 'antd'
  27. import { CPost, MyCarousel } from './Post'
  28. import { Row, Col } from 'antd'
  29. import LinkToUser from './LinkToUser'
  30. import { AddComment, Comments } from './Post_Comment'
  31. import { Like, Likes } from './Like'
  32. const MyPostFeed = ({
  33. my_Id,
  34. postsFeed = [],
  35. onPostsFeed,
  36. addComment,
  37. }) => {
  38. const [checkScroll, setCheckScroll] = useState(true)
  39. useEffect(() => {
  40. if (checkScroll) {
  41. onPostsFeed()
  42. setCheckScroll(false)
  43. }
  44. }, [checkScroll])
  45. useEffect(() => {
  46. document.addEventListener('scroll', scrollHandler)
  47. return () => {
  48. document.removeEventListener('scroll', scrollHandler)
  49. }
  50. }, [postsFeed])
  51. const scrollHandler = (e) => {
  52. if (
  53. e.target.documentElement.scrollHeight - e.target.documentElement.scrollTop - e.target.documentElement.clientHeight <20
  54. ) {
  55. setCheckScroll(true)
  56. }
  57. }
  58. return (
  59. <>
  60. <div className="PostsFeed">
  61. <Row>
  62. <Col span={12} offset={6}>
  63. <div>
  64. {postsFeed?.length == 0 && (
  65. <div>
  66. <center>
  67. <h1> You have no posts to feed! </h1>
  68. <h1>
  69. {' '}
  70. Post and follow other users!{' '}
  71. </h1>
  72. </center>
  73. </div>
  74. )}
  75. {(postsFeed || []).map(
  76. ({ _id, images, title, text, owner, comments, likes }) => (
  77. <div className="PostFeed">
  78. <LinkToUser owner={owner} size="70px" />
  79. <MyCarousel images={images} style={{ marginTop: '60px' }} />
  80. <h1 className='Title'> Title: {title || ''}</h1>
  81. <h1 className='Title'> Text: {text || ''}</h1>
  82. <Divider>Comments</Divider>
  83. <div className="ScrollForFeed">
  84. <CCommentsForFeed
  85. postId={_id}
  86. comments={comments || []}
  87. />
  88. </div>
  89. <center>
  90. <div style={{ display: 'flex', padding: '20px', marginLeft:'100px' }}>
  91. <CLikeForFeed likes={likes} postId={_id} />
  92. <AddComment addComment={addComment} postId={_id} />
  93. </div>
  94. </center>
  95. </div>
  96. ),
  97. )}
  98. </div>
  99. </Col>
  100. </Row>
  101. </div>
  102. </>
  103. )
  104. }
  105. const CCommentsForFeed = connect(
  106. (state) => ({
  107. addComment: state.promise?.addComment?.payload,
  108. addSubComment: state.promise?.addSubComment,
  109. }),
  110. {
  111. addComment: actionAddFullCommentFeed,
  112. addCommentReply: actionAddSubFullComment,
  113. findSubComment: actionFindSubComment,
  114. },
  115. )(Comments)
  116. export const CPostForFeed = connect(
  117. (state) => ({
  118. my_Id: state.auth?.payload?.sub?.id || '',
  119. postsFeed: state.feed?.postsFeed,
  120. addComment: state.promise?.addComment?.payload,
  121. }),
  122. {
  123. onPostsFeed: actionFullAllGetPosts,
  124. clearDataProfile: actionClearFeedPosts,
  125. addComment: actionAddFullCommentFeed,
  126. addCommentReply: actionAddSubFullComment,
  127. addLike: actionAddFullLikeForFeed,
  128. },
  129. )(MyPostFeed)
  130. const AllLikeComp = ({ my_Id, addLike, deleteLike, likes, postId }) => (
  131. <Like
  132. my_Id={my_Id}
  133. addLike={addLike}
  134. deleteLike={deleteLike}
  135. likes={likes}
  136. postId={postId}
  137. >
  138. <Likes likes={likes} />
  139. </Like>
  140. )
  141. export const CLikeForFeed = connect(
  142. (state) => ({
  143. my_Id: state.auth.payload?.sub?.id || '',
  144. addLike: state.promise?.addLike?.payload,
  145. deleteLike: state.promise?.deleteLike?.payload,
  146. }),
  147. {
  148. addLike: actionAddFullLikeFeed,
  149. deleteLike: actionDeleteFullLikeFeed,
  150. },
  151. )(AllLikeComp)