ReplayComments.tsx 2.5 KB

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