PostPage.jsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import React, { useEffect, useState } from 'react'
  2. import { Row, Col, Divider } from 'antd';
  3. import { connect } from 'react-redux'
  4. import PostImage from '../components/main/postsFeed/PostImage'
  5. import { PostTitle, PostDescription } from './MainPostsFeed';
  6. import Text from 'antd/lib/typography/Text';
  7. import { CFieldCommentSend } from '../components/main/postsFeed/FieldComment';
  8. import { CPostUserPanel } from '../components/main/postsFeed/PostUserPanel';
  9. import { Comment, Tooltip, Avatar } from 'antd';
  10. import moment from 'moment';
  11. import { UserAvatar } from './Header';
  12. import { Link } from 'react-router-dom';
  13. import { LikeFilled, LikeOutlined } from '@ant-design/icons';
  14. import { actionLikeComment, actionAddLikeCommentAC, actionFindLikeComment, actionDelLikeComment } from '../actions';
  15. const PostPageTitle = ({ data: { owner } }) =>
  16. <PostTitle owner={owner} />
  17. const CPostPageTitle = connect(state => ({ data: state?.postsFeed?.posts || {} }))(PostPageTitle)
  18. //
  19. const PostCommentAuthor = ({ owner }) => {
  20. return (
  21. <Link className='PostCommentAuthor' to={`/profile/${owner?._id}`} >
  22. {owner?.nick ? owner.nick : owner?.login ? owner.login : 'Null'}
  23. </Link>
  24. )
  25. }
  26. const PostComment = ({ myID, data: { _id, answerTo, answers, createdAt, likes = [], text, owner }, addLikeComment, removeLikeComment, children }) => {
  27. const [open, setOpen] = useState(false);
  28. let likeStatus
  29. let likeId
  30. likes.find(l => {
  31. if (l?.owner?._id === myID) {
  32. likeStatus = true
  33. likeId = l._id
  34. } else {
  35. likeStatus = false
  36. }
  37. })
  38. const changeLike = () => likeStatus ? removeLikeComment(likeId, _id) : addLikeComment(_id)
  39. const actions = [
  40. <span onClick={changeLike}>
  41. {likeStatus ? <LikeFilled /> : <LikeOutlined />}
  42. <span style={{ paddingLeft: 8, cursor: 'auto' }}>{likes.length ? likes.length : ''}</span>
  43. </span>,
  44. <span onClick={() => setOpen(true)}>Reply to</span>,
  45. open && <CFieldCommentSend onFocus />
  46. ];
  47. return (
  48. <Comment
  49. actions={actions}
  50. author={<PostCommentAuthor owner={owner} />}
  51. avatar={< UserAvatar avatar={owner.avatar} avatarSize={'35px'} />}
  52. content={<p>{text}</p >}
  53. datetime={
  54. < Tooltip title={moment(new Date(+createdAt)).format('DD-MM-YYYY HH:mm:ss')} >
  55. <span>
  56. {moment(new Date(+createdAt)).startOf('seconds').fromNow()}
  57. </span>
  58. </ Tooltip>
  59. }
  60. >
  61. {children}
  62. </Comment>
  63. )
  64. }
  65. const CPostComment = connect(state => ({
  66. myID: state.auth.payload.sub.id || ''
  67. }), {
  68. addLikeComment: actionLikeComment,
  69. removeLikeComment: actionDelLikeComment,
  70. }
  71. )(PostComment)
  72. const PostComments = ({ comments }) => {
  73. return (
  74. <>
  75. {
  76. comments.map(c => <CPostComment key={c._id} data={c}></CPostComment>)
  77. }
  78. </>
  79. )
  80. }
  81. const CPostComments = connect(state => ({
  82. comments: state?.postsFeed?.posts?.comments || []
  83. }))(PostComments)
  84. const PostPageDescrption = ({ data: { _id, likes, text, title, createdAt, } }) =>
  85. <div className='PostOne__description-inner'>
  86. <div className='PostOne__description-top'>
  87. <PostDescription title={title} description={text} date={createdAt} />
  88. <Divider plain><Text type='secodary'></Text>Comments</Divider>
  89. <CPostComments />
  90. </div>
  91. <div className='PostOne__description-bottom'>
  92. <Divider ></Divider>
  93. <CPostUserPanel likes={likes} postId={_id}
  94. styleFontSize='1.3em' />
  95. <CFieldCommentSend postId={_id} />
  96. </div>
  97. </div>
  98. const CPostPageDescrption = connect(state => ({ data: state?.postsFeed?.posts || {} }))(PostPageDescrption)
  99. const PostPage = ({ data: { images } }) =>
  100. <div className='PostOne'>
  101. <div className='PostOne__inner'>
  102. <div className='PostOne__image'>
  103. <PostImage images={images} />
  104. </div>
  105. <div className='PostOne__title'>
  106. <CPostPageTitle />
  107. </div>
  108. <div className='PostOne__description'>
  109. <CPostPageDescrption />
  110. </div>
  111. </div>
  112. </div>
  113. export const CPostPage = connect(state => ({ data: state?.postsFeed?.posts || {} }))(PostPage)
  114. // xs={{ span: 24 }} sm={{ span: 20 }} md={{ span: 16 }} lg={{ span: 16 }}A