DeleteCommentModal.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import React from 'react';
  2. import { UserPicture } from '@growi/ui/dist/components';
  3. import { format } from 'date-fns';
  4. import {
  5. Button, Modal, ModalHeader, ModalBody, ModalFooter,
  6. } from 'reactstrap';
  7. import { ICommentHasId } from '../../interfaces/comment';
  8. import { Username } from '../User/Username';
  9. import styles from './DeleteCommentModal.module.scss';
  10. export type DeleteCommentModalProps = {
  11. isShown: boolean,
  12. comment: ICommentHasId | null,
  13. errorMessage: string,
  14. cancelToDelete: () => void,
  15. confirmToDelete: () => void,
  16. }
  17. export const DeleteCommentModal = (props: DeleteCommentModalProps): JSX.Element => {
  18. const {
  19. isShown, comment, errorMessage, cancelToDelete, confirmToDelete,
  20. } = props;
  21. const headerContent = () => {
  22. if (comment == null || isShown === false) {
  23. return <></>;
  24. }
  25. return (
  26. <span>
  27. <i className="icon-fw icon-fire"></i>
  28. Delete comment?
  29. </span>
  30. );
  31. };
  32. const bodyContent = () => {
  33. if (comment == null || isShown === false) {
  34. return <></>;
  35. }
  36. // the threshold for omitting body
  37. const OMIT_BODY_THRES = 400;
  38. const commentDate = format(new Date(comment.createdAt), 'yyyy/MM/dd HH:mm');
  39. let commentBody = comment.comment;
  40. if (commentBody.length > OMIT_BODY_THRES) { // omit
  41. commentBody = `${commentBody.substr(0, OMIT_BODY_THRES)}...`;
  42. }
  43. const commentBodyElement = <span style={{ whiteSpace: 'pre-wrap' }}>{commentBody}</span>;
  44. return (
  45. <>
  46. <UserPicture user={comment.creator} size="xs" /> <strong><Username user={comment.creator}></Username></strong> wrote on {commentDate}:
  47. <p className="card well comment-body mt-2 p-2">{commentBodyElement}</p>
  48. </>
  49. );
  50. };
  51. const footerContent = () => {
  52. if (comment == null || isShown === false) {
  53. return <></>;
  54. }
  55. return (
  56. <>
  57. <span className="text-danger">{errorMessage}</span>&nbsp;
  58. <Button onClick={cancelToDelete}>Cancel</Button>
  59. <Button color="danger" onClick={confirmToDelete}>
  60. <i className="icon icon-fire"></i>
  61. Delete
  62. </Button>
  63. </>
  64. );
  65. };
  66. return (
  67. <Modal isOpen={isShown} toggle={cancelToDelete} className={`${styles['page-comment-delete-modal']}`}>
  68. <ModalHeader tag="h4" toggle={cancelToDelete} className="bg-danger text-light">
  69. {headerContent()}
  70. </ModalHeader>
  71. <ModalBody>
  72. {bodyContent()}
  73. </ModalBody>
  74. <ModalFooter>
  75. {footerContent()}
  76. </ModalFooter>
  77. </Modal>
  78. );
  79. };