User.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import {
  2. actionOnePost,
  3. actionUploadFile,
  4. actionFullUnSubscribe,
  5. actionFullSubscribe,
  6. } from '../actions'
  7. import user from '../materials/user.png'
  8. import React, { useState, useEffect } from 'react'
  9. import { Card } from './Post'
  10. import ListFollowing from './ListFollowing'
  11. import ListFollowers from './ListFollowers'
  12. import { connect } from 'react-redux'
  13. import { Avatar, Button} from 'antd'
  14. import {
  15. actionFullProfilePageUser,
  16. actionRemoveDataUser,
  17. } from '../reducers'
  18. import { Row, Col } from 'antd'
  19. import { CEditInfo } from '../components/EditAvatar'
  20. export const EditAccount = ({ open, children }) => {
  21. const [opened, setOpened] = useState(open)
  22. return (
  23. <>
  24. <button style={{ width: '100px' }} onClick={setOpened(!opened)}>
  25. Edit account
  26. </button>
  27. {opened && children}
  28. </>
  29. )
  30. }
  31. export const PageAboutUser = ({
  32. match: {
  33. params: { _id },
  34. },
  35. my_Id,
  36. aboutUser: { login, nick, createdAt, avatar, followers, following } = {},
  37. allPosts,
  38. followId,
  39. onPost,
  40. addSubscribe,
  41. deleteSubscribe,
  42. onePost,
  43. onAboutUser,
  44. aboutUserFollowers = [],
  45. onPageData,
  46. aboutUserFollowing = [],
  47. aboutMeFollowing,
  48. countAllPostsUser,
  49. }) => {
  50. useEffect(() => {
  51. onAboutUser(_id)
  52. // console.log('USER DATA ', login, _id)
  53. }, [_id])
  54. const checkMyId = _id === my_Id
  55. return (
  56. <>
  57. <Row>
  58. <Col span={12} offset={6}>
  59. <section className="AboutMe">
  60. <Row>
  61. {avatar?.url ? (
  62. <Avatar
  63. style={{
  64. marginRight: '20px',
  65. width: '150px',
  66. height: '150px',
  67. }}
  68. src={'/' + avatar?.url}
  69. />
  70. ) : (
  71. <Avatar
  72. style={{
  73. marginRight: '20px',
  74. width: '150px',
  75. height: '150px',
  76. }}
  77. src={user}
  78. />
  79. )}
  80. <div className="Info">
  81. {login ? <h1> {login}</h1> : <h1> {'Anon'}</h1>}
  82. <h3>
  83. Created Account:{' '}
  84. {new Intl.DateTimeFormat('en-GB').format(createdAt)}
  85. </h3>
  86. <div style={{ display: 'flex' }}>
  87. {countAllPostsUser > 0 ? (
  88. <div
  89. style={{
  90. display: 'flex',
  91. justifyContent: 'space-between',
  92. }}
  93. >
  94. <h3> {countAllPostsUser} posts </h3>
  95. </div>
  96. ) : (
  97. <h3> 0 posts </h3>
  98. )}
  99. <ListFollowers aboutUserFollowers={aboutUserFollowers}
  100. followers={followers} onPageData={onPageData} />
  101. <ListFollowing aboutUserFollowing={aboutUserFollowing}
  102. following={following} onPageData={onPageData} />
  103. </div>
  104. <h3> nick: {nick == null ? login : nick}</h3>
  105. {checkMyId ? (
  106. <>
  107. <CEditInfo/>
  108. </>
  109. ) : (
  110. <Subscribe
  111. my_Id={my_Id}
  112. deleteSubscribe={deleteSubscribe}
  113. followId={followId}
  114. addSubscribe={addSubscribe}
  115. aboutMeFollowing={aboutMeFollowing}
  116. />
  117. )}
  118. </div>
  119. </Row>
  120. </section>
  121. </Col>
  122. </Row>
  123. <Row>
  124. <Col span={15} offset={6}>
  125. <div
  126. style={{
  127. display: 'flex',
  128. flexWrap: 'wrap',
  129. padding: '20px',
  130. margin: '20px',
  131. }}
  132. >
  133. {(allPosts || [])?.map((item) => (
  134. <Card post={item} onPost={onPost} />
  135. ))}
  136. </div>
  137. </Col>
  138. </Row>
  139. </>
  140. )
  141. }
  142. const Subscribe = ({
  143. my_Id,
  144. deleteSubscribe,
  145. aboutMeFollowing = [],
  146. addSubscribe,
  147. followId,
  148. }) => {
  149. const checkFollowId = aboutMeFollowing?.find(
  150. (follower) => follower?._id === followId,
  151. )?._id
  152. console.log('FOLLOWING ', checkFollowId)
  153. return (
  154. <>
  155. <div style={{ display: 'flex' }}>
  156. {checkFollowId ? (
  157. <Button
  158. size="large" type="primary"
  159. danger
  160. onClick={() => deleteSubscribe(my_Id, followId)}
  161. >
  162. Unsubscribe
  163. </Button>
  164. ) : (
  165. <Button
  166. size="large"
  167. type="primary"
  168. primary
  169. onClick={() => addSubscribe(my_Id, followId)}
  170. >
  171. Subscribe
  172. </Button>
  173. )}
  174. </div>
  175. </>
  176. )
  177. }
  178. export const CPageAboutUser = connect(
  179. (state) => ({
  180. my_Id: state.auth?.payload?.sub?.id,
  181. aboutUser: state.profilePage?.aboutUser,
  182. aboutUserFollowers: state.profilePage?.aboutUser?.followers,
  183. aboutUserFollowing: state.profilePage?.aboutUser?.following,
  184. followId: state.profilePage?.aboutUser?._id,
  185. aboutMeFollowing: state.profileData?.aboutMe?.following,
  186. countAllPostsUser: state.promise?.countAllPostsUser?.payload,
  187. allPosts: state.profilePage?.allPosts,
  188. onePost: state.promise?.onePost?.payload,
  189. }),
  190. {
  191. onAboutUser: actionFullProfilePageUser,
  192. actionRemoveDataUser: actionRemoveDataUser,
  193. onLoad: actionUploadFile,
  194. onPost: actionOnePost,
  195. onPageData: actionFullProfilePageUser,
  196. addSubscribe: actionFullSubscribe,
  197. deleteSubscribe: actionFullUnSubscribe,
  198. },
  199. )(PageAboutUser)