ProfilePage.jsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import React, { useEffect, useState } from 'react'
  2. import { Button, Card, Col, Row } from 'antd'
  3. import postNoData from '../images/profile-post-no.jpeg'
  4. import { connect } from 'react-redux'
  5. import { Link } from 'react-router-dom'
  6. import { actionFindFollowers, actionFindFollowing, actionProfilePageData, actionRemovePostsFeedAC, actionSubscribe, actionUnSubscribe } from '../actions'
  7. import { backURL, CircularGalleryIcon } from '../helpers'
  8. import { UserAvatar } from './Header'
  9. import { CModalFollowers, CModalFollowing } from '../components/main/profilePage/ModalFollow'
  10. import { DateCreated } from '../components/main/DateCreated'
  11. import Text from 'antd/lib/typography/Text'
  12. const ProfileFollow = ({ myID, userId, followers, onSubsuscribe, onUnSubsuscribe }) => {
  13. const followCheck = followers.find(f => f._id === myID && true)
  14. return (
  15. <Col className='Profile__seting' offset={4}>
  16. {!!followCheck ?
  17. <Button onClick={() => onUnSubsuscribe(userId)}>UnSubscribe</Button> :
  18. <Button onClick={() => onSubsuscribe(userId)} type="primary">Subscribe</Button>}
  19. </Col>
  20. )
  21. }
  22. const CProfileSetting = connect(state => ({
  23. myID: state?.auth?.payload?.sub.id,
  24. followers: state?.postsFeed?.userData?.followers || []
  25. }), { onSubsuscribe: actionSubscribe, onUnSubsuscribe: actionUnSubscribe })(ProfileFollow)
  26. const ProfilePageData = ({ data: { _id, avatar, login, nick, createdAt = '', followers, following }, count, setFollowing, setFollowers }) =>
  27. <Row className='Profile' >
  28. <Col span={8}>
  29. <UserAvatar avatarSize={'150px'} avatar={avatar} />
  30. </Col>
  31. <Col span={14} offset={1}>
  32. <Row align='top' className='Profile__name'>
  33. <Col>
  34. <h1>{nick || login || 'No Name'}</h1>
  35. <span className='Profile__login'>{login || '----'}</span>
  36. </Col>
  37. <Col>
  38. <CProfileSetting userId={_id} />
  39. </Col>
  40. </Row>
  41. <Row align='middle'>
  42. <Col >
  43. <Text type='secondary'>Account created: <DateCreated date={createdAt} /></Text>
  44. </Col>
  45. <Col offset={2}>
  46. <Link className='Profile__link-message' to='/message'>Send message</Link>
  47. </Col>
  48. </Row>
  49. <Row className='Profile__count' align='middle' justify='space-between'>
  50. <Col >
  51. <strong>{count || '0'}</strong>
  52. <span>Posts</span>
  53. </Col>
  54. <Col >
  55. <Button type="link" onClick={() => setFollowers(true)}>
  56. <strong>{followers?.length || '0'}</strong>
  57. <span>Followers</span>
  58. </Button>
  59. </Col>
  60. <Col >
  61. <Button type="link" onClick={() => setFollowing(true)}>
  62. <strong>{following?.length || '0'}</strong>
  63. <span>Following</span>
  64. </Button>
  65. </Col>
  66. </Row>
  67. </Col >
  68. </Row >
  69. const CProfilePageData = connect(state => ({
  70. data: state?.postsFeed?.userData || {},
  71. count: state?.postsFeed?.count || null
  72. }))(ProfilePageData)
  73. const ProfilePagePosts = ({ posts }) =>
  74. <Row gutter={[15, 15]}>
  75. {Array.isArray(posts) && posts.map(p => <Col key={p._id} span={8}>
  76. <Link to={`/post/${p._id}`}>
  77. <Card className='Profile__post' hoverable>
  78. {p?.images && p?.images[0] && p.images[0]?.url
  79. ?
  80. p.images.length === 1
  81. ?
  82. < img src={(backURL + '/' + p?.images[0].url)} alt='post Img' />
  83. :
  84. <div className='Profile__box' >
  85. <CircularGalleryIcon className='Profile__box-icon' style={{ stroke: 'black' }} />
  86. <img src={(backURL + '/' + p?.images[0]?.url)} alt='post Img' />
  87. </div>
  88. :
  89. <img src={postNoData} />}
  90. </Card>
  91. </Link>
  92. </Col>
  93. )
  94. }
  95. </Row >
  96. export const CProfilePagePosts = connect(state => ({ posts: state.postsFeed?.posts || [] }))(ProfilePagePosts)
  97. const ProfilePage = ({ match: { params: { _id } }, getProfileUser, clearDataProfile }) => {
  98. const [followers, setFollowers] = useState(false)
  99. const [following, setFollowing] = useState(false)
  100. const [checkScroll, setCheckScroll] = useState(true)
  101. useEffect(() => {
  102. document.addEventListener('scroll', scrollHandler)
  103. return () => {
  104. document.removeEventListener('scroll', scrollHandler)
  105. setCheckScroll(true)
  106. clearDataProfile()
  107. }
  108. }, [_id])
  109. useEffect(() => {
  110. if (checkScroll) {
  111. getProfileUser(_id)
  112. setCheckScroll(false)
  113. }
  114. }, [_id, checkScroll])
  115. const scrollHandler = (e) => {
  116. if (e.target.documentElement.scrollHeight - (e.target.documentElement.scrollTop + window.innerHeight) < 500) {
  117. setCheckScroll(true)
  118. }
  119. }
  120. return (
  121. <>
  122. <CProfilePageData setFollowing={setFollowing} setFollowers={setFollowers} />
  123. {followers && < CModalFollowers statusModal={setFollowers} title={'Followers'} />}
  124. {following && < CModalFollowing statusModal={setFollowing} title={'Following'} />}
  125. <CProfilePagePosts />
  126. </>
  127. )
  128. }
  129. export const CProfilePage = connect(state => ({
  130. posts: state?.postsFeed?.posts || []
  131. }), { getProfileUser: actionProfilePageData, clearDataProfile: actionRemovePostsFeedAC })(ProfilePage)