DeleteCommentModal.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. type DeleteCommentModalProps = {
  10. isShown: boolean,
  11. comment: ICommentHasId,
  12. errorMessage: string,
  13. cancel: () => void, // for cancel evnet handling
  14. confirmedToDelete: () => void, // for confirmed event handling
  15. }
  16. export const DeleteCommentModal = (props: DeleteCommentModalProps): JSX.Element => {
  17. const {
  18. isShown, comment, errorMessage, cancel, confirmedToDelete,
  19. } = props;
  20. /*
  21. * the threshold for omitting body
  22. */
  23. const OMIT_BODY_THRES = 400;
  24. const commentDate = format(new Date(comment.createdAt), 'yyyy/MM/dd HH:mm');
  25. // generate body
  26. let commentBody = comment.comment;
  27. if (commentBody.length > OMIT_BODY_THRES) { // omit
  28. commentBody = `${commentBody.substr(0, OMIT_BODY_THRES)}...`;
  29. }
  30. const commentBodyElement = <span style={{ whiteSpace: 'pre-wrap' }}>{commentBody}</span>;
  31. return (
  32. <Modal isOpen={isShown} toggle={cancel} className="page-comment-delete-modal">
  33. <ModalHeader tag="h4" toggle={cancel} className="bg-danger text-light">
  34. <span>
  35. <i className="icon-fw icon-fire"></i>
  36. Delete comment?
  37. </span>
  38. </ModalHeader>
  39. <ModalBody>
  40. <UserPicture user={comment.creator} size="xs" /> <strong><Username user={comment.creator}></Username></strong> wrote on {commentDate}:
  41. <p className="card well comment-body mt-2 p-2">{commentBodyElement}</p>
  42. </ModalBody>
  43. <ModalFooter>
  44. <span className="text-danger">{errorMessage}</span>&nbsp;
  45. <Button onClick={cancel}>Cancel</Button>
  46. <Button color="danger" onClick={confirmedToDelete}>
  47. <i className="icon icon-fire"></i>
  48. Delete
  49. </Button>
  50. </ModalFooter>
  51. </Modal>
  52. );
  53. };