Comment.js 5.9 KB

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