2
0

DeleteCommentModal.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import Button from 'react-bootstrap/es/Button';
  4. import Modal from 'react-bootstrap/es/Modal';
  5. import dateFnsFormat from 'date-fns/format';
  6. import ReactUtils from '../ReactUtils';
  7. import UserPicture from '../User/UserPicture';
  8. export default class DeleteCommentModal extends React.Component {
  9. /*
  10. * the threshold for omitting body
  11. */
  12. static get OMIT_BODY_THRES() { return 400 };
  13. constructor(props) {
  14. super(props);
  15. }
  16. componentWillMount() {
  17. }
  18. render() {
  19. if (this.props.comment === undefined) {
  20. return <div></div>
  21. }
  22. const comment = this.props.comment;
  23. const commentDate = dateFnsFormat(comment.createdAt, 'YYYY/MM/DD HH:mm');
  24. // generate body
  25. let commentBody = comment.comment;
  26. if (commentBody.length > DeleteCommentModal.OMIT_BODY_THRES) { // omit
  27. commentBody = commentBody.substr(0, DeleteCommentModal.OMIT_BODY_THRES) + '...';
  28. }
  29. commentBody = ReactUtils.nl2br(commentBody);
  30. return (
  31. <Modal show={this.props.isShown} onHide={this.props.cancel} className="page-comment-delete-modal">
  32. <Modal.Header closeButton>
  33. <Modal.Title>Delete comment?</Modal.Title>
  34. </Modal.Header>
  35. <Modal.Body>
  36. <UserPicture user={comment.creator} size="xs" /> <strong>{comment.creator.username}</strong> wrote on {commentDate}:
  37. <p className="comment-body">{commentBody}</p>
  38. </Modal.Body>
  39. <Modal.Footer>
  40. <span className="text-danger">{this.props.errorMessage}</span>&nbsp;
  41. <Button onClick={this.props.cancel}>Cancel</Button>
  42. <Button onClick={this.props.confirmedToDelete} className="btn-danger">Delete</Button>
  43. </Modal.Footer>
  44. </Modal>
  45. );
  46. }
  47. }
  48. DeleteCommentModal.propTypes = {
  49. isShown: PropTypes.bool.isRequired,
  50. comment: PropTypes.object,
  51. errorMessage: PropTypes.string,
  52. cancel: PropTypes.func.isRequired, // for cancel evnet handling
  53. confirmedToDelete: PropTypes.func.isRequired, // for confirmed event handling
  54. };