ReplayComments.tsx 2.6 KB

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