Profile.js 5.9 KB

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