MainPostFeed.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { Card, Col, Row, Carousel, Empty } from 'antd'
  2. import Meta from 'antd/lib/card/Meta'
  3. import React, { createRef, useEffect } from 'react'
  4. import { connect } from 'react-redux'
  5. import { Link } from 'react-router-dom'
  6. import { myFolowingPosts } from '../../actions'
  7. import { backURL } from '../../helpers'
  8. import { UserAvatar } from '../header/Header'
  9. import nodata from '../../images/nodata.png'
  10. import { LeftCircleOutlined, RightCircleOutlined } from '@ant-design/icons'
  11. const PostTitle = ({ owner }) =>
  12. <Link to={`/${owner?._id}`} className='owner'>
  13. <Row justify="start" align='middle'>
  14. <Col >
  15. <UserAvatar avatar={owner?.avatar} login={owner?.login} nick={owner?.nick} />
  16. </Col>
  17. <Col offset={1}>
  18. <span>{owner?.nick ? owner.nick : owner?.login ? owner.login : 'Null'}</span>
  19. </Col>
  20. </Row>
  21. </Link >
  22. class PostImage extends React.Component {
  23. constructor(props) {
  24. super(props);
  25. this.carouselRef = createRef();
  26. this.state = {
  27. movePrev: false,
  28. moveNext: false
  29. }
  30. }
  31. handleNext = () => this.carouselRef.current.next(this);
  32. handlePrev = () => this.carouselRef.current.prev(this);
  33. moveOnDivArray = (length, index) => {
  34. if (length === 1) {
  35. this.setState({ movePrev: false, moveNext: false })
  36. } else if (index === 0) {
  37. this.setState({ movePrev: false, moveNext: true })
  38. } else if (index === length - 1 && length > 1) {
  39. this.setState({ movePrev: true, moveNext: false })
  40. } else {
  41. this.setState({ movePrev: true, moveNext: true })
  42. }
  43. }
  44. downOnDivArray = () => this.setState({ movePrev: false, moveNext: false })
  45. render() {
  46. const { images } = this.props
  47. return (
  48. <Carousel ref={this.carouselRef}
  49. effect="fade"
  50. infinite={false}
  51. dots={{ className: 'Post__dots' }
  52. }>
  53. {!!images ?
  54. images.map((i, index) => i?.url ? <div key={i._id}
  55. onMouseEnter={() => this.moveOnDivArray(images.length, index)}
  56. onMouseLeave={this.downOnDivArray}>
  57. <button onClick={() => this.handlePrev()}
  58. className={`Post__prev Post__btn ${this.state.movePrev ? '--active' : ''}`}><LeftCircleOutlined /></button>
  59. <button onClick={() => this.handleNext()}
  60. className={`Post__next Post__btn ${this.state.moveNext ? '--active' : ''}`}><RightCircleOutlined /></button>
  61. <img src={backURL + '/' + i.url} />
  62. </div> :
  63. <Empty key={i._id} image={nodata} description={false} />) :
  64. <Empty image={nodata} description={false} />
  65. }
  66. </Carousel >
  67. );
  68. }
  69. }
  70. const Post = ({ postData: { text, title, owner, images, createdAt, comments } }) => {
  71. const date = new Date(createdAt * 1)
  72. const resultDate = new Intl.DateTimeFormat('default').format(date)
  73. return (
  74. <div className='Post'>
  75. <Card
  76. title={<PostTitle owner={owner} />}
  77. cover={<PostImage images={images} />}
  78. >
  79. <Meta title="Europe Street beat" description="www.instagram.com" />
  80. </Card>
  81. </div>
  82. )
  83. }
  84. const MainPostFeed = ({ posts, postsFollowing }) => {
  85. useEffect(() => {
  86. postsFollowing()
  87. }, [])
  88. return (
  89. <>
  90. {posts.map(p => <Post key={p._id} postData={p} />)}
  91. </>
  92. )
  93. }
  94. export const CMainPostFeed = connect(state => ({ posts: state.promise?.followingPosts?.payload || [] }), { postsFollowing: myFolowingPosts })(MainPostFeed)