Comment.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import {
  2. actionAddFullComment,
  3. actionFindSubComment,
  4. actionAddSubFullComment,
  5. } from '../actions'
  6. import { Tooltip } from 'antd'
  7. import { connect } from 'react-redux'
  8. import { Link } from 'react-router-dom'
  9. import { Input, Button } from 'antd'
  10. import { SmileOutlined } from '@ant-design/icons'
  11. import moment from 'moment'
  12. import React, { useState } from 'react'
  13. import 'emoji-mart/css/emoji-mart.css'
  14. import { Picker } from 'emoji-mart'
  15. import { Comment,Avatar } from 'antd';
  16. import user from '../materials/user.png'
  17. export const AddComment = ({ addComment, postId }) => {
  18. const [text, setComment] = useState('')
  19. const [showEmojiPicker, setShowEmojiPicker] = useState(false)
  20. console.log('show stickers', showEmojiPicker)
  21. const handleOK = () =>
  22. setShowEmojiPicker(!showEmojiPicker)
  23. const addEmoji = ({ native }) =>
  24. setComment((text + ' ' + native).trim())
  25. return (
  26. <>
  27. {showEmojiPicker && (
  28. <Picker
  29. style={{
  30. color: '#108ee9',
  31. position: 'absolute',
  32. bottom: '160px',
  33. right: '30px',
  34. }}
  35. theme="light"
  36. native={true}
  37. showPreview={false}
  38. showSkinTones={false}
  39. onSelect={(native) =>
  40. addEmoji(native)}
  41. />
  42. )}
  43. <Input
  44. style={{
  45. display: 'flex',
  46. width: '40%',
  47. marginLeft: '10px',
  48. marginRight: '10px',
  49. }}
  50. size="large"
  51. placeholder="Add a comment..."
  52. value={text}
  53. onChange={(e) => {
  54. setComment(e.target.value)
  55. }}
  56. onPressEnter={(e) => {
  57. setComment(e.target.value)
  58. }}
  59. />
  60. {
  61. !showEmojiPicker ?
  62. <SmileOutlined
  63. className="smile-btn"
  64. style={{ fontSize: 'xx-large', marginRight: '30px' }}
  65. onClick={handleOK}
  66. />
  67. :
  68. <SmileOutlined
  69. className="smile-btn"
  70. style={{ color:'#108ee9',fontSize: 'xx-large', marginRight: '30px' }}
  71. onClick={handleOK}
  72. />
  73. }
  74. <Button
  75. size="large"
  76. disabled={text.length < 1}
  77. type="primary"
  78. onClick={(e) => {
  79. addComment(postId, text)
  80. && setComment(e.target.value = '') &&
  81. setShowEmojiPicker(false)
  82. }}
  83. >
  84. {' '}
  85. Publish{' '}
  86. </Button>
  87. </>
  88. )
  89. }
  90. export const SpoilerButton = ({ text, close, children, style }) => {
  91. const [opened, setOpened] = useState(close)
  92. return (
  93. <>
  94. <Button
  95. onClick={() => {
  96. setOpened(!opened)
  97. }}
  98. style={style}
  99. >
  100. {text}
  101. </Button>
  102. {opened && children}
  103. </>
  104. )
  105. }
  106. const CommentForReply = ({ addCommentReply, commentId, postId }) => {
  107. const [comment, setComment] = useState('')
  108. return (
  109. <>
  110. <div
  111. style={{
  112. display: 'flex',
  113. flexDirection: 'row',
  114. width: '100%',
  115. padding: '15px',
  116. marginLeft: '40px',
  117. }}
  118. >
  119. <Input
  120. placeholder="Add a comment..."
  121. value={comment}
  122. onChange={(e) => {
  123. setComment(e.target.value)
  124. }}
  125. />
  126. <Button
  127. disabled={comment.length < 1}
  128. type="primary"
  129. onClick={() => addCommentReply(postId, commentId, comment)}
  130. >
  131. {' '}
  132. Publish{' '}
  133. </Button>
  134. </div>
  135. </>
  136. )
  137. }
  138. const CommentData = ({ createdAt }) => {
  139. return (
  140. <Tooltip
  141. color={'#108ee9'}
  142. style={{ paddingLeft: '10px' }}
  143. title={moment(new Date(+createdAt)).format('lll')}
  144. >
  145. {moment(new Date(+createdAt))
  146. .startOf()
  147. .fromNow()}
  148. </Tooltip>
  149. )
  150. }
  151. export const Comments = ({
  152. comments,
  153. postId,
  154. commentsFind,
  155. addCommentReply,
  156. children,
  157. close,
  158. findSubComment,
  159. onGetLikes,
  160. onGetType
  161. }) => {
  162. const [opened, setOpened] = useState(close)
  163. return (
  164. <>
  165. {comments
  166. ?
  167. comments?.map((comment) => (
  168. <Comment
  169. author={ <Link
  170. to={`/profile/${comment?.owner?._id}`}
  171. >
  172. {comment?.owner?.login || 'Anon'}
  173. </Link>}
  174. avatar=
  175. {comment.owner?.avatar ? (
  176. <Link
  177. to={`/profile/${comment?.owner?._id}`}
  178. >
  179. <Avatar
  180. size={30}
  181. src={'/' + comment.owner?.avatar?.url}
  182. style={{ marginLeft: '15px' }}
  183. alt={comment.owner?.login || 'Anon'}
  184. />
  185. </Link>
  186. ) : (
  187. <Link
  188. to={`/profile/${comment?.owner?._id}`}
  189. >
  190. <Avatar size={30}
  191. src={user}
  192. style={{ marginLeft: '15px' }}
  193. alt={comment.owner?.login || 'Anon'}
  194. />
  195. </Link>
  196. )}
  197. content=
  198. {
  199. <p>
  200. {comment?.text}
  201. </p>
  202. }
  203. datetime={
  204. <CommentData createdAt={comment?.createdAt} />
  205. }
  206. />
  207. )) : <h3> No comments </h3>}
  208. </>)
  209. }
  210. export const CComments = connect(
  211. (state) => ({
  212. postId: state.promise.onePost?.payload?._id,
  213. addComment: state.promise?.addComment?.payload,
  214. addSubComment: state.promise?.addSubComment,
  215. }),
  216. {
  217. addCommentReply: actionAddSubFullComment,
  218. findSubComment: actionFindSubComment,
  219. },
  220. )(Comments)