CommentForm.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. /**
  4. *
  5. * @author Yuki Takei <yuki@weseek.co.jp>
  6. *
  7. * @export
  8. * @class Comment
  9. * @extends {React.Component}
  10. */
  11. export default class CommentForm extends React.Component {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. comment: '',
  16. isMarkdown: false,
  17. };
  18. this.handleChange = this.handleChange.bind(this);
  19. this.postComment = this.postComment.bind(this);
  20. this.reload = this.reload.bind(this);
  21. }
  22. handleChange(event) {
  23. const target = event.target;
  24. const value = target.type === 'checkbox' ? target.checked : target.value;
  25. const name = target.name;
  26. this.setState({
  27. [name]: value
  28. });
  29. }
  30. postComment(event) {
  31. event.preventDefault();
  32. this.props.crowi.apiPost('/comments.add', {
  33. commentForm: {
  34. comment: this.state.comment,
  35. _csrf: this.props.crowi.csrfToken,
  36. page_id: this.props.pageId,
  37. revision_id: this.props.revisionId,
  38. is_markdown: this.state.isMarkdown,
  39. }
  40. })
  41. .then(() => {
  42. this.reload();
  43. });
  44. }
  45. reload() {
  46. this.props.pageComments.init();
  47. this.setState({
  48. comment: '',
  49. isMarkdown: false,
  50. });
  51. }
  52. render() {
  53. //{% if not user %}disabled{% endif %}をtextareaとbuttonに追加
  54. // denounce/throttle
  55. return (
  56. <div>
  57. <form className="form page-comment-form" id="page-comment-form" onSubmit={this.postComment}>
  58. <div className="comment-form">
  59. <div className="comment-form-user">
  60. <img src="{{ user|picture }}" className="picture img-circle" width="25" alt="{{ user.name }}" title="{{ user.name }}" />
  61. </div>
  62. <div className="comment-form-main">
  63. <div className="comment-write" id="comment-write">
  64. <textarea className="comment-form-comment form-control" id="comment-form-comment" name="comment"
  65. required placeholder="Write comments here..." value={this.state.comment} onChange={this.handleChange}></textarea>
  66. <input type="checkbox" id="comment-form-is-markdown" name="isMarkdown" checked={this.state.isMarkdown} value="1" onChange={this.handleChange} /> Markdown<br />
  67. </div>
  68. <div className="comment-submit">
  69. <div className="pull-right">
  70. <span className="text-danger" id="comment-form-message"></span>
  71. <button type="submit" value="Submit" id="comment-form-button" className="fcbtn btn btn-sm btn-outline btn-rounded btn-primary btn-1b">
  72. Comment
  73. </button>
  74. </div>
  75. <div className="clearfix"></div>
  76. </div>
  77. </div>
  78. </div>
  79. </form>
  80. </div>
  81. );
  82. }
  83. }
  84. CommentForm.propTypes = {
  85. pageComments: PropTypes.object.isRequired,
  86. crowi: PropTypes.object.isRequired,
  87. pageId: PropTypes.string,
  88. revisionId: PropTypes.string,
  89. };