MainPostsFeed.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { Card, Col, Row } from 'antd'
  2. import React, { useEffect, useState } from 'react'
  3. import { connect } from 'react-redux'
  4. import { Link } from 'react-router-dom'
  5. import { UserAvatar } from './Header'
  6. import Paragraph from 'antd/lib/typography/Paragraph'
  7. import Text from 'antd/lib/typography/Text'
  8. import { actionPostsFeed, actionRemovePostsFeedAC } from '../actions'
  9. import { DateCreated } from '../components/main/DateCreated'
  10. import PostImage from '../components/main/postsFeed/PostImage'
  11. import { CPostUserPanel } from '../components/main/postsFeed/PostUserPanel'
  12. import { CFieldCommentSend } from '../components/main/postsFeed/FieldComment'
  13. export const PostTitle = ({ owner }) =>
  14. <Row justify="start" align='middle'>
  15. <Link to={`/profile/${owner?._id}`} className='owner'>
  16. <UserAvatar avatar={owner?.avatar} login={owner?.login} avatarSize={'45px'} nick={owner?.nick} />
  17. <span>{owner?.nick ? owner.nick : owner?.login ? owner.login : 'Null'}</span>
  18. </Link >
  19. </Row>
  20. const PostDescription = ({ title, description, date }) =>
  21. <>
  22. <Row justify='space-between'>
  23. <Col >
  24. {!!title && <Text level={3} strong>{title}</Text>}
  25. </Col>
  26. <Col >
  27. <Text type='secondary'>
  28. <DateCreated date={date} />
  29. </Text>
  30. </Col>
  31. </Row>
  32. <Paragraph ellipsis={true ? { rows: 1, expandable: true, symbol: 'more' } : false}>
  33. {description}
  34. </Paragraph>
  35. </>
  36. const Comments = ({ comments }) =>
  37. <>
  38. {comments && comments.length > 2 &&
  39. <Link to={`/#`}>
  40. <Text type={'secondary'} level={3}>{`Посмотреть все ${comments.length} комментария`}</Text>
  41. </Link>}
  42. {comments && <div>
  43. <div className='Post__comments'>
  44. <Link to={`/#`}>{comments[comments?.length - 2]?.owner?.nick || comments[comments?.length - 2]?.owner?.login}: </Link>
  45. <span>{comments[comments?.length - 2]?.text}</span>
  46. </div>
  47. <div className='Post__comments'>
  48. <Link to={`/#`}>{comments[comments?.length - 1]?.owner?.login || comments[comments?.length - 1]?.owner?.login}: </Link>
  49. <span>{comments[comments?.length - 1]?.text}</span>
  50. </div>
  51. </div>}
  52. </>
  53. const Post = ({ postData: { _id, text, title, owner, images, createdAt = '', comments, likes } }) =>
  54. <div className='Post'>
  55. <Card
  56. title={<PostTitle owner={owner} />}
  57. cover={<PostImage images={images} />}
  58. actions={[<CFieldCommentSend postId={_id} />]}
  59. >
  60. <CPostUserPanel postId={_id} likes={likes} />
  61. <PostDescription title={title} description={text} date={createdAt} />
  62. <Comments comments={comments} />
  63. </Card>
  64. </div>
  65. const MainPostsFeed = ({ posts, count, postsFollowing, postsFollowingRemove, following }) => {
  66. const [checkScroll, setCheckScroll] = useState(true)
  67. useEffect(() => {
  68. if (checkScroll && following.length !== 0) {
  69. postsFollowing(following)
  70. setCheckScroll(false)
  71. }
  72. }, [checkScroll, following])
  73. useEffect(() => {
  74. document.addEventListener('scroll', scrollHandler)
  75. return () => {
  76. document.removeEventListener('scroll', scrollHandler)
  77. postsFollowingRemove()
  78. console.log(posts.length);
  79. }
  80. }, [])
  81. const scrollHandler = (e) => {
  82. if (e.target.documentElement.scrollHeight - (e.target.documentElement.scrollTop + window.innerHeight) < 500) {
  83. setCheckScroll(true)
  84. }
  85. }
  86. return (
  87. <>
  88. {posts.map(p => <Post key={p._id} postData={p} />)}
  89. </>
  90. )
  91. }
  92. export const CMainPostsFeed = connect(state => ({
  93. posts: state?.postsFeed?.posts || [],
  94. following: state?.myData.following || []
  95. }), {
  96. postsFollowing: actionPostsFeed,
  97. postsFollowingRemove: actionRemovePostsFeedAC,
  98. })(MainPostsFeed)