Profile.js 5.3 KB

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