index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import React, { useState, useEffect } from 'react'
  2. import { actionFullAllGetPostsSaga } from '../../actions/typeSaga/feedTypesSaga'
  3. import { actionAddCommentFeedTypeSaga } from '../../actions/typeSaga/commentTypesSaga'
  4. import { actionClearFeedPostsType } from '../../actions/types/feedTypes'
  5. import { Provider, connect } from 'react-redux'
  6. import { Divider } from 'antd'
  7. import { CPost } from '../onePost'
  8. import { Row, Col } from 'antd'
  9. import LinkToUser from '../../components/LinkToUser'
  10. import { Comments } from '../../components/comment/Comment'
  11. import AddComment from '../../components/comment/AddComment'
  12. import { MyCarousel } from '../../components/post/Carousel'
  13. import load from '../../materials/load.gif'
  14. import { actionFindSubCommentTypeSaga } from '../../actions/typeSaga/postTypesSaga'
  15. import { CLikeFeed } from '../../components/like/Like'
  16. const MyPostFeed = ({
  17. postsFeed = [],
  18. onPostsFeed,
  19. addComment,
  20. onClearFeed,
  21. postsFeedPromise,
  22. }) => {
  23. const [checkScroll, setScroll] = useState(true)
  24. useEffect(() => {
  25. if (checkScroll) {
  26. onPostsFeed()
  27. }
  28. setScroll(false)
  29. }, [checkScroll])
  30. useEffect(() => {
  31. document.addEventListener('scroll', scrollHandler)
  32. return () => {
  33. document.removeEventListener('scroll', scrollHandler)
  34. onClearFeed()
  35. }
  36. }, [])
  37. useEffect(() => {
  38. document.addEventListener('scroll', scrollHandler)
  39. }, [postsFeed.length])
  40. const scrollHandler = (e) => {
  41. if (
  42. e.target.documentElement.scrollHeight -
  43. (e.target.documentElement.scrollTop + window.innerHeight) <
  44. 200
  45. ) {
  46. setScroll(true)
  47. document.removeEventListener('scroll', scrollHandler)
  48. }
  49. }
  50. return (
  51. <div style={{ marginTop: '50px' }}>
  52. <div className="PostsFeed">
  53. <Row>
  54. <Col span={12} offset={6}>
  55. <div>
  56. {postsFeed?.length == 0 && (
  57. <div style={{ textAlign: 'center' }}>
  58. <h1> You have no posts to feed! </h1>
  59. <h1> Post and follow other users! </h1>
  60. </div>
  61. )}
  62. {(postsFeed || []).map(
  63. ({ _id, images, title, text, owner, comments, likes }) => (
  64. <div className="PostFeed">
  65. <LinkToUser
  66. _id={owner?._id}
  67. key={_id}
  68. style={{ marginLeft: '50px' }}
  69. login={owner?.login}
  70. avatar={owner?.avatar}
  71. size={50}
  72. />
  73. <MyCarousel images={images} />
  74. <div style={{ margin: '0 10%' }}>
  75. <h2 className="Title"> Title: {title || ''}</h2>
  76. <h2 className="Title"> Text: {text || ''}</h2>
  77. <Divider>Comments</Divider>
  78. <div style={{ margin: '10px', position: 'relative' }}>
  79. <div className="ScrollForFeed">
  80. <CCommentsForFeed postId={_id} comments={comments} />
  81. </div>
  82. <div style={{ display: 'flex', margin: '20px 0px' }}>
  83. <CLikeFeed likes={likes} postId={_id} />
  84. <AddComment
  85. addComment={addComment}
  86. postId={_id}
  87. style={{
  88. position: 'absolute',
  89. bottom: '70px',
  90. zIndex: '100',
  91. }}
  92. width={'300px'}
  93. />
  94. </div>
  95. </div>
  96. </div>
  97. </div>
  98. ),
  99. )}
  100. </div>
  101. </Col>
  102. </Row>
  103. {postsFeedPromise?.status == 'PENDING' && (
  104. <img
  105. style={{
  106. display: 'block',
  107. margin: '0 auto',
  108. padding: '10px',
  109. }}
  110. src={load}
  111. width="100"
  112. height="100"
  113. />
  114. )}
  115. </div>
  116. </div>
  117. )
  118. }
  119. const CCommentsForFeed = connect(
  120. (state) => ({
  121. addSubComment: state.promise?.addSubComment,
  122. addComment: state.promise?.addComment?.payload,
  123. }),
  124. {
  125. findSubComment: actionFindSubCommentTypeSaga,
  126. },
  127. )(Comments)
  128. export const CPostForFeed = connect(
  129. (state) => ({
  130. postsFeed: state.feed?.postsFeed || [],
  131. addComment: state.promise?.addComment?.payload,
  132. postsFeedPromise: state.promise?.postsFeed,
  133. }),
  134. {
  135. onPostsFeed: actionFullAllGetPostsSaga,
  136. onClearFeed: actionClearFeedPostsType,
  137. addComment: actionAddCommentFeedTypeSaga,
  138. },
  139. )(MyPostFeed)