index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import React, { useMemo, useState, useEffect } from 'react'
  2. import {
  3. actionAllPostsFeed,
  4. actionFullAllGetPosts,
  5. actionAddSubFullComment,
  6. actionFindLikes,
  7. actionGetFindLiked,
  8. actionDeleteFullLike,
  9. actionAddFullLikeForFeed,
  10. actionDeleteFullLikeForFeed,
  11. actionAddFullLike,
  12. } from '../../actions'
  13. import { actionClearFeedPostsType,actionAddCommentFeedTypeSaga } from
  14. '../../redux/reducers/feedReducer'
  15. import { Link } from 'react-router-dom'
  16. import { Provider, connect } from 'react-redux'
  17. import { Upload, Button, DatePicker, Space } from 'antd'
  18. import { Avatar, Image, Divider, Radio } from 'antd'
  19. import { CPost } from '../onePost'
  20. import { Row, Col } from 'antd'
  21. import LinkToUser from '../../components/LinkToUser'
  22. import { Comments } from '../../components/comment/Comment'
  23. import AddComment from '../../components/comment/AddComment'
  24. import { Like, Likes } from '../../components/Like'
  25. import { MyCarousel } from '../../components/Carousel'
  26. import load from '../../materials/load.gif'
  27. const MyPostFeed = ({
  28. // myData,
  29. postsFeed = [],
  30. onPostsFeed,
  31. addComment,
  32. onClearFeed,
  33. postsFeedPromise
  34. }) => {
  35. const [checkScroll, setScroll] = useState(true)
  36. useEffect(() => {
  37. if (checkScroll) {
  38. console.log('попало в новую порцию постов')
  39. onPostsFeed()
  40. }
  41. setScroll(false)
  42. }, [checkScroll])
  43. useEffect(() => {
  44. document.addEventListener('scroll', scrollHandler)
  45. return () => {
  46. document.removeEventListener('scroll', scrollHandler)
  47. onClearFeed()
  48. }
  49. }, [])
  50. console.log('check scroll ', checkScroll)
  51. useEffect(() => {
  52. document.addEventListener('scroll', scrollHandler)
  53. }, [postsFeed.length])
  54. const scrollHandler = (e) => {
  55. if (e.target.documentElement.scrollHeight -
  56. (e.target.documentElement.scrollTop + window.innerHeight) < 200) {
  57. console.log('SCROLL HANDLER', checkScroll)
  58. setScroll(true)
  59. document.removeEventListener('scroll', scrollHandler)
  60. }
  61. }
  62. return (
  63. <div style={{ marginTop: '50px'}}>
  64. <div className="PostsFeed" >
  65. <Row>
  66. <Col span={12} offset={6}>
  67. <div>
  68. {postsFeed?.length == 0 && (
  69. <div>
  70. <center>
  71. <h1> You have no posts to feed! </h1>
  72. <h1>
  73. {' '}
  74. Post and follow other users!{' '}
  75. </h1>
  76. </center>
  77. </div>
  78. )}
  79. {(postsFeed || []).map(
  80. ({ _id, images, title, text, owner, comments, likes }) => (
  81. <div className='PostFeed'>
  82. <LinkToUser _id={owner?._id} key={_id} style={{marginLeft:"50px"}}
  83. login={owner?.login} avatar={owner?.avatar}
  84. size={50} />
  85. <MyCarousel images={images} />
  86. <div style={{margin:"0 10%"}}>
  87. <h1 className='Title'> Title: {title || ''}</h1>
  88. <h1 className='Title'> Text: {text || ''}</h1>
  89. <Divider>Comments</Divider>
  90. <div style={{ margin: '10px',position: 'relative' }}>
  91. <div className="ScrollForFeed">
  92. <CCommentsForFeed
  93. postId={_id}
  94. comments={comments || []}
  95. />
  96. </div>
  97. {/* <center> */}
  98. <div style={{ display: 'flex', margin: '20px 0px' }}>
  99. {/* <CLikeForFeed likes={likes} postId={_id} /> */}
  100. <AddComment addComment={addComment}
  101. postId={_id} style={{
  102. position: 'absolute', bottom: '70px',
  103. zIndex: '100'
  104. }} />
  105. </div>
  106. </div>
  107. {/* </center> */}
  108. </div>
  109. </div>
  110. // </div>
  111. ),
  112. )}
  113. </div>
  114. </Col>
  115. </Row>
  116. {(postsFeedPromise?.status == "PENDING") &&
  117. <img style={{
  118. display: 'block',
  119. margin: '0 auto', padding: '10px'
  120. }} src={load} width="100" height="100" />
  121. }
  122. </div>
  123. </div>
  124. )
  125. }
  126. const CCommentsForFeed = connect(
  127. (state) => ({
  128. addComment: state.promise?.addComment?.payload,
  129. // addSubComment: state.promise?.addSubComment,
  130. }),
  131. {
  132. // addComment: actionAddFullCommentFeed,
  133. // addCommentReply: actionAddSubFullComment,
  134. // findSubComment: actionFindSubComment,
  135. },
  136. )(Comments)
  137. export const CPostForFeed = connect(
  138. (state) => ({
  139. // myData: state?.myData.aboutMe || '',
  140. postsFeed: state.feed?.postsFeed || [],
  141. addComment: state.promise?.addComment?.payload,
  142. postsFeedPromise :state.promise?.postsFeed
  143. }),
  144. {
  145. onPostsFeed: actionFullAllGetPosts,
  146. onClearFeed: actionClearFeedPostsType,
  147. addComment: actionAddCommentFeedTypeSaga,
  148. // addCommentReply: actionAddSubFullComment,
  149. // addLike: actionAddFullLikeForFeed,
  150. },
  151. )(MyPostFeed)
  152. const AllLikeComp = ({ my_Id, addLike, deleteLike, likes, postId }) => (
  153. <Like
  154. my_Id={my_Id}
  155. addLike={addLike}
  156. deleteLike={deleteLike}
  157. likes={likes}
  158. postId={postId}
  159. >
  160. <Likes likes={likes} />
  161. </Like>
  162. )
  163. export const CLikeForFeed = connect(
  164. (state) => ({
  165. my_Id: state.auth?.payload?.sub?.id || '',
  166. addLike: state.promise?.addLike?.payload,
  167. deleteLike: state.promise?.deleteLike?.payload,
  168. }),
  169. {
  170. // addLike: actionAddFullLikeFeed,
  171. // deleteLike: actionDeleteFullLikeFeed,
  172. },
  173. )(AllLikeComp)