ReplyComments.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import React, { useState } from 'react';
  2. import { Collapse } from 'reactstrap';
  3. import { RendererOptions } from '~/services/renderer/renderer';
  4. import { ICommentHasId, ICommentHasIdList } from '../../interfaces/comment';
  5. import { useIsAllReplyShown } from '../../stores/context';
  6. import { Comment } from './Comment';
  7. import styles from './ReplyComments.module.scss';
  8. type ReplycommentsProps = {
  9. isReadOnly: boolean,
  10. replyList: ICommentHasIdList,
  11. deleteBtnClicked: (comment: ICommentHasId) => void,
  12. onComment: () => void,
  13. rendererOptions: RendererOptions,
  14. }
  15. export const ReplyComments = (props: ReplycommentsProps): JSX.Element => {
  16. const {
  17. isReadOnly, replyList, deleteBtnClicked, onComment, rendererOptions,
  18. } = props;
  19. const { data: isAllReplyShown } = useIsAllReplyShown();
  20. const [isOlderRepliesShown, setIsOlderRepliesShown] = useState(false);
  21. const renderReply = (reply: ICommentHasId) => {
  22. return (
  23. <div key={reply._id} className={`${styles['page-comment-reply']} ml-4 ml-sm-5 mr-3`}>
  24. <Comment
  25. comment={reply}
  26. isReadOnly={isReadOnly}
  27. deleteBtnClicked={deleteBtnClicked}
  28. onComment={onComment}
  29. rendererOptions={rendererOptions}
  30. />
  31. </div>
  32. );
  33. };
  34. if (isAllReplyShown) {
  35. return (
  36. <>
  37. {replyList.map((reply) => {
  38. return renderReply(reply);
  39. })}
  40. </>
  41. );
  42. }
  43. const areThereHiddenReplies = (replyList.length > 2);
  44. const toggleButtonIconName = isOlderRepliesShown ? 'icon-arrow-up' : 'icon-options-vertical';
  45. const toggleButtonIcon = <i className={`icon-fw ${toggleButtonIconName}`}></i>;
  46. const toggleButtonLabel = isOlderRepliesShown ? '' : 'more';
  47. const shownReplies = replyList.slice(replyList.length - 2, replyList.length);
  48. const hiddenReplies = replyList.slice(0, replyList.length - 2);
  49. const hiddenElements = hiddenReplies.map((reply) => {
  50. return renderReply(reply);
  51. });
  52. const shownElements = shownReplies.map((reply) => {
  53. return renderReply(reply);
  54. });
  55. return (
  56. <>
  57. {areThereHiddenReplies && (
  58. <div className={`${styles['page-comments-hidden-replies']}`}>
  59. <Collapse isOpen={isOlderRepliesShown}>
  60. <div>{hiddenElements}</div>
  61. </Collapse>
  62. <div className="text-center">
  63. <button
  64. type="button"
  65. className="btn btn-link"
  66. onClick={() => setIsOlderRepliesShown(!isOlderRepliesShown)}
  67. >
  68. {toggleButtonIcon} {toggleButtonLabel}
  69. </button>
  70. </div>
  71. </div>
  72. )}
  73. {shownElements}
  74. </>
  75. );
  76. };