userData.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. import { url } from "../../App"
  2. import React, { useContext, useState } from 'react';
  3. import { useHistory, useParams } from 'react-router-dom';
  4. import { useDispatch, useSelector } from 'react-redux';
  5. import { CardContent, Typography, Stack, Avatar, Box, Container, Badge, TextField, InputAdornment } from '@mui/material';
  6. import Button from '@mui/material-next/Button';
  7. import { PersonRemoveRounded, PersonAddRounded, ManageAccountsRounded, BorderColorRounded, ImageSearchRounded, AccountCircle, HttpsRounded } from '@mui/icons-material';
  8. import { actionUserPageSubscribing } from "../../redux/thunks";
  9. import ModalWindow from '../structure/modal';
  10. import ChangeData from "./change_data";
  11. import { ModalForCountsContext, UpdateProfile } from "../../App";
  12. import { EditProfile } from ".";
  13. // Функция отображения кнопки редактирования своего профиля/отписки или подписки на другого юзера
  14. function UptadeProfileButton({ data }) {
  15. // console.log('UserData: ', data)
  16. const history = useHistory()
  17. const { userId } = useParams()
  18. const dispatch = useDispatch()
  19. // отслеживание нажатия кнопки редактирования профиля
  20. const [isEditProfile, setIsEditProfile] = useContext(EditProfile)
  21. // контекст обновления профиля
  22. const [updateProfile, setUpdateProfile] = useContext(UpdateProfile)
  23. // console.log('updateProfile: ', updateProfile)
  24. // определяем мой id
  25. const myId = updateProfile?._id
  26. // определяем всех моих подписчиков
  27. const myFollowingList = updateProfile?.following?.map(user => user?._id)
  28. // проверка, является ли пользователь моим подписчиком
  29. const isFollowing = myFollowingList && (myFollowingList)?.some(item => item === (data?.owner?._id || data?._id))
  30. // функция подписки/отписки - это чисто кнопка
  31. function isSubscribing() {
  32. // при клике на посте
  33. const newData = {
  34. ...updateProfile, following: (isFollowing
  35. ? updateProfile?.following?.filter(item => item?._id !== (data?.owner?._id || data?._id)) // отписка
  36. // : [...updateProfile?.following, { _id: (data?.owner?._id || data?._id) }] // подписка
  37. : updateProfile?.following
  38. ? [...updateProfile?.following, { _id: (data?.owner?._id || data?._id) }] // подписка если уже есть кто-то в подписках
  39. : [{ _id: (data?.owner?._id || data?._id) }] // подписка, если это первая подписка
  40. )
  41. }
  42. dispatch(actionUserPageSubscribing(newData, userId))
  43. }
  44. // функция перехода на редактирование профиля
  45. function upsertProfile() {
  46. // history.push('/updateprofile')
  47. setIsEditProfile(true)
  48. }
  49. return (
  50. // <React.Fragment>
  51. // {userId === myId && <Box
  52. // onClick={upsertProfile}
  53. // >
  54. // <Button
  55. // size="large"
  56. // variant="elevated"
  57. // sx={{
  58. // minWidth: '300px'
  59. // }}
  60. // startIcon={
  61. // <ManageAccountsRounded />
  62. // }
  63. // disableRipple
  64. // >
  65. // <Typography
  66. // variant='button'
  67. // display='block'
  68. // >
  69. // Редактировать аккаунт
  70. // </Typography>
  71. // </Button>
  72. // </Box>}
  73. // {myFollowingList?.includes(userId) && <Box
  74. // onClick={isSubscribing}
  75. // >
  76. // <Button
  77. // size="large"
  78. // variant="elevated"
  79. // sx={{
  80. // minWidth: '300px'
  81. // }}
  82. // startIcon={
  83. // <PersonRemoveRounded />
  84. // }
  85. // disableRipple
  86. // >
  87. // <Typography
  88. // variant='button'
  89. // display='block'
  90. // >
  91. // Отписаться
  92. // </Typography>
  93. // </Button>
  94. // </Box>}
  95. // {(userId !== myId && !myFollowingList?.includes(userId)) && <Box
  96. // onClick={isSubscribing}
  97. // >
  98. // <Button
  99. // size="large"
  100. // variant="filledTonal"
  101. // sx={{
  102. // minWidth: '300px'
  103. // }}
  104. // startIcon={
  105. // <PersonAddRounded />
  106. // }
  107. // disableRipple
  108. // >
  109. // <Typography
  110. // variant='button'
  111. // display='block'
  112. // >
  113. // Подписаться
  114. // </Typography>
  115. // </Button>
  116. // </Box>}
  117. // </React.Fragment>
  118. <Box
  119. onClick={
  120. (userId === myId && upsertProfile) ||
  121. (myFollowingList?.includes(userId) && isSubscribing) ||
  122. ((userId !== myId && !myFollowingList?.includes(userId)) && isSubscribing)}
  123. >
  124. <Button
  125. size="large"
  126. // variant="elevated"
  127. variant={
  128. ((userId === myId) && 'elevated') ||
  129. ((myFollowingList?.includes(userId)) && 'elevated') ||
  130. ((userId !== myId && !myFollowingList?.includes(userId)) && 'filledTonal')
  131. }
  132. sx={{
  133. minWidth: '300px'
  134. }}
  135. startIcon={
  136. ((userId === myId) && <ManageAccountsRounded />) ||
  137. ((myFollowingList?.includes(userId)) && <PersonRemoveRounded />) ||
  138. ((userId !== myId && !myFollowingList?.includes(userId)) && <PersonAddRounded />)
  139. }
  140. disableRipple
  141. >
  142. <Typography
  143. variant='button'
  144. display='block'
  145. >
  146. {(userId === myId) && 'Редактировать аккаунт' ||
  147. (myFollowingList?.includes(userId)) && 'Отписаться' ||
  148. (userId !== myId && !myFollowingList?.includes(userId)) && 'Подписаться'
  149. }
  150. </Typography>
  151. </Button>
  152. </Box>
  153. )
  154. }
  155. // блок пользовательских данных
  156. export default function BasicCard({ userData }) {
  157. // console.log('userData: ', userData)
  158. // определяем количество постов пользоваеля
  159. const userPostsCount = useSelector(state => state?.promise?.UserPostsCount?.payload)
  160. // контекст обновления профиля
  161. const [updateProfile, setUpdateProfile] = useContext(UpdateProfile)
  162. // console.log('updateProfile: ', updateProfile)
  163. // отслеживание нажатия кнопки редактирования профиля
  164. const [isEditProfile, setIsEditProfile] = useContext(EditProfile)
  165. // контекст модального окна
  166. const [modalName, setModalName, modalArray, setModalArray, openModal, setOpenModal, handleOpenModal, handleCloseModal] = useContext(ModalForCountsContext)
  167. // отслеживание статуса изменения пароля
  168. const [changePassword, setChangePassword] = useState(false)
  169. // открытие поля редактирвоания пароля
  170. function changePasswordButton() {
  171. setChangePassword(true)
  172. }
  173. function userFollowers() {
  174. handleOpenModal({
  175. arr: userData?.followers, name: `Подписчики ${userData?.login}`
  176. })
  177. }
  178. function userFollowing() {
  179. handleOpenModal({
  180. arr: userData?.following, name: `Подписки ${userData?.login}`
  181. })
  182. }
  183. return (
  184. <Container sx={{
  185. display: 'flex',
  186. alignItems: 'center'
  187. }}
  188. >
  189. {/* <Avatar
  190. alt={userData?.login}
  191. src={url + userData?.avatar?.url}
  192. sx={{
  193. width: 130,
  194. height: 130
  195. }}
  196. // children={
  197. // <BorderColorRounded />
  198. // }
  199. >
  200. <BorderColorRounded sx={{
  201. color: 'black',
  202. zIndex: 999
  203. }} />
  204. </Avatar> */}
  205. <Box
  206. sx={{
  207. position: 'relative',
  208. display: 'inline-block'
  209. }}>
  210. <Avatar
  211. alt={userData?.login}
  212. src={url + userData?.avatar?.url}
  213. sx={{
  214. width: 130,
  215. height: 130
  216. }}
  217. />
  218. {isEditProfile &&
  219. <Box
  220. sx={{
  221. position: 'absolute',
  222. top: 0,
  223. left: 0,
  224. width: '100%',
  225. height: '100%',
  226. display: 'flex',
  227. justifyContent: 'center',
  228. alignItems: 'center',
  229. zIndex: 1,
  230. '&:before': {
  231. content: '""',
  232. position: 'absolute',
  233. top: 0,
  234. left: 0,
  235. width: '100%',
  236. height: '100%',
  237. backgroundColor: 'rgba(0,0,0,0.5)',
  238. zIndex: -1,
  239. borderRadius: '50%',
  240. }
  241. }}
  242. >
  243. <ImageSearchRounded
  244. sx={{
  245. color: '#FFF',
  246. fontSize: '40px'
  247. }}
  248. />
  249. </Box>}
  250. </Box>
  251. {!isEditProfile &&
  252. <Box
  253. sx={{
  254. display: 'flex',
  255. flexDirection: 'column',
  256. marginLeft: '50px '
  257. }}
  258. >
  259. <CardContent sx={{
  260. flex: '1 0 auto'
  261. }}
  262. >
  263. <Stack
  264. sx={{
  265. justifyContent: 'space-between'
  266. }}
  267. direction="row"
  268. spacing={2}
  269. >
  270. <Typography
  271. component="div"
  272. variant="h4"
  273. >
  274. {userData?.login}
  275. </Typography>
  276. <UptadeProfileButton data={userData} />
  277. </Stack>
  278. <Typography
  279. variant="subtitle1"
  280. color="text.secondary"
  281. component="div"
  282. >
  283. {userData?.nick || ''}
  284. </Typography>
  285. </CardContent>
  286. <Stack
  287. direction="row"
  288. spacing={5}
  289. padding={2}
  290. paddingTop={1}
  291. >
  292. <Typography
  293. variant="subtitle1"
  294. >
  295. {userPostsCount || '0'} публикаций
  296. </Typography>
  297. <Typography
  298. sx={{
  299. cursor: 'pointer'
  300. }}
  301. variant="subtitle1"
  302. onClick={userFollowers}
  303. >
  304. {userData?.followers?.length || '0'} подписчиков
  305. </Typography>
  306. <Typography
  307. sx={{
  308. cursor: 'pointer'
  309. }}
  310. variant="subtitle1"
  311. onClick={userFollowing}
  312. >
  313. {userData?.following?.length || '0'} подписок
  314. </Typography>
  315. </Stack>
  316. </Box>}
  317. {isEditProfile && <ChangeData />
  318. // <Box
  319. // sx={{
  320. // display: 'flex',
  321. // flexDirection: 'column',
  322. // marginLeft: '50px '
  323. // }}
  324. // >
  325. // {/* <CardContent sx={{
  326. // flex: '1 0 auto'
  327. // }}
  328. // > */}
  329. // <Stack
  330. // sx={{
  331. // justifyContent: 'space-between',
  332. // marginBottom: '10px'
  333. // }}
  334. // direction="row"
  335. // spacing={2}
  336. // >
  337. // <Box
  338. // sx={{
  339. // display: 'flex',
  340. // alignItems: 'flex-end'
  341. // }}>
  342. // <AccountCircle
  343. // sx={{
  344. // color: 'action.active',
  345. // mr: 1, my: 0.5
  346. // }} />
  347. // <TextField
  348. // id="input-new-login"
  349. // label="Новый логин"
  350. // variant="standard" />
  351. // </Box>
  352. // <Box
  353. // sx={{
  354. // display: 'flex',
  355. // alignItems: 'flex-end'
  356. // }}>
  357. // <AccountCircle
  358. // sx={{
  359. // color: 'action.active',
  360. // mr: 1, my: 0.5
  361. // }} />
  362. // <TextField
  363. // id="input-new-nick"
  364. // label="Новый ник-нейм"
  365. // variant="standard" />
  366. // </Box>
  367. // </Stack>
  368. // <Stack
  369. // sx={{
  370. // justifyContent: 'space-between',
  371. // marginBottom: '10px'
  372. // }}
  373. // direction="row"
  374. // spacing={2}
  375. // >
  376. // <Box
  377. // onClick={changePasswordButton}
  378. // >
  379. // <Button
  380. // sx={{
  381. // padding: '5px 20px'
  382. // }}
  383. // size="small"
  384. // variant="text"
  385. // >
  386. // Редактировать пароль
  387. // </Button>
  388. // </Box>
  389. // {changePassword &&
  390. // <React.Fragment>
  391. // <Box
  392. // sx={{
  393. // display: 'flex',
  394. // alignItems: 'flex-end'
  395. // }}>
  396. // <HttpsRounded
  397. // sx={{
  398. // color: 'action.active',
  399. // mr: 1, my: 0.5
  400. // }} />
  401. // <TextField
  402. // id="input-new-login"
  403. // label="Старый пароль"
  404. // variant="standard" />
  405. // </Box>
  406. // <Box
  407. // sx={{
  408. // display: 'flex',
  409. // alignItems: 'flex-end'
  410. // }}>
  411. // <HttpsRounded
  412. // sx={{
  413. // color: 'action.active',
  414. // mr: 1, my: 0.5
  415. // }} />
  416. // <TextField
  417. // id="input-new-nick"
  418. // label="Новый пароль"
  419. // variant="standard" />
  420. // </Box>
  421. // </React.Fragment>}
  422. // </Stack>
  423. // <Button
  424. // size="medium"
  425. // variant="filledTonal"
  426. // sx={{
  427. // maxWidth: '300px'
  428. // }}
  429. // >
  430. // Сохранить изменения
  431. // </Button>
  432. // </Box>
  433. }
  434. {modalArray && <ModalWindow />}
  435. </Container>
  436. )
  437. }