DeleteCommentModal.js 2.0 KB

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