ReplyComments.tsx 2.7 KB

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