DeleteCommentModal.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import React from 'react';
  2. import { UserPicture } from '@growi/ui';
  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. confirmeToDelete: () => void,
  16. }
  17. export const DeleteCommentModal = (props: DeleteCommentModalProps): JSX.Element => {
  18. const {
  19. isShown, comment, errorMessage, cancelToDelete, confirmeToDelete,
  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. const OMIT_BODY_THRES = 400;
  37. const commentDate = format(new Date(comment.createdAt), 'yyyy/MM/dd HH:mm');
  38. let commentBody = comment.comment;
  39. if (commentBody.length > OMIT_BODY_THRES) { // omit
  40. commentBody = `${commentBody.substr(0, OMIT_BODY_THRES)}...`;
  41. }
  42. const commentBodyElement = <span style={{ whiteSpace: 'pre-wrap' }}>{commentBody}</span>;
  43. return (
  44. <>
  45. <UserPicture user={comment.creator} size="xs" /> <strong><Username user={comment.creator}></Username></strong> wrote on {commentDate}:
  46. <p className="card well comment-body mt-2 p-2">{commentBodyElement}</p>
  47. </>
  48. );
  49. };
  50. const FooterContent = () => {
  51. if (comment == null || isShown === false) {
  52. return <></>;
  53. }
  54. return (
  55. <>
  56. <span className="text-danger">{errorMessage}</span>&nbsp;
  57. <Button onClick={cancelToDelete}>Cancel</Button>
  58. <Button color="danger" onClick={confirmeToDelete}>
  59. <i className="icon icon-fire"></i>
  60. Delete
  61. </Button>
  62. </>
  63. );
  64. };
  65. return (
  66. <Modal isOpen={isShown} toggle={cancelToDelete} className={`${styles['page-comment-delete-modal']}`}>
  67. <ModalHeader tag="h4" toggle={cancelToDelete} className="bg-danger text-light">
  68. <HeaderContent/>
  69. </ModalHeader>
  70. <ModalBody>
  71. <BodyContent />
  72. </ModalBody>
  73. <ModalFooter>
  74. <FooterContent />
  75. </ModalFooter>
  76. </Modal>
  77. );
  78. };