CommentForm.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. }
  21. handleChange(event) {
  22. const target = event.target;
  23. const value = target.type === 'checkbox' ? target.checked : target.value;
  24. const name = target.name;
  25. this.setState({
  26. [name]: value
  27. });
  28. }
  29. postComment(event) {
  30. event.preventDefault();
  31. this.props.crowi.apiPost('/comments.add', {
  32. commentForm: {
  33. comment: this.state.comment,
  34. _csrf: this.props.crowi.csrfToken,
  35. page_id: this.props.pageId,
  36. revision_id: this.props.revisionId,
  37. is_markdown: this.state.isMarkdown,
  38. }
  39. })
  40. .then((res) => {
  41. if (this.props.onPostComplete != null) {
  42. this.props.onPostComplete(res.comment);
  43. }
  44. });
  45. }
  46. render() {
  47. //{% if not user %}disabled{% endif %}をtextareaとbuttonに追加
  48. // denounce/throttle
  49. return (
  50. <div>
  51. <form className="form page-comment-form" id="page-comment-form" onSubmit={this.postComment}>
  52. <div className="comment-form">
  53. <div className="comment-form-user">
  54. <img src="{{ user|picture }}" className="picture img-circle" width="25" alt="{{ user.name }}" title="{{ user.name }}" />
  55. </div>
  56. <div className="comment-form-main">
  57. <div className="comment-write" id="comment-write">
  58. <textarea className="comment-form-comment form-control" id="comment-form-comment" name="comment"
  59. required placeholder="Write comments here..." value={this.state.comment} onChange={this.handleChange}></textarea>
  60. <input type="checkbox" id="comment-form-is-markdown" name="isMarkdown" checked={this.state.isMarkdown} value="1" onChange={this.handleChange} /> Markdown<br />
  61. </div>
  62. <div className="comment-submit">
  63. <div className="pull-right">
  64. <span className="text-danger" id="comment-form-message"></span>
  65. <button type="submit" value="Submit" id="comment-form-button" className="fcbtn btn btn-sm btn-outline btn-rounded btn-primary btn-1b">
  66. Comment
  67. </button>
  68. </div>
  69. <div className="clearfix"></div>
  70. </div>
  71. </div>
  72. </div>
  73. </form>
  74. </div>
  75. );
  76. }
  77. }
  78. CommentForm.propTypes = {
  79. crowi: PropTypes.object.isRequired,
  80. onPostComplete: PropTypes.func,
  81. pageId: PropTypes.string,
  82. revisionId: PropTypes.string,
  83. };