CommentForm.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.testComment = new Comment();
  19. this.handleChange = this.handleChange.bind(this);
  20. this.postComment = this.postComment.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. console.log(this.props.crowi.csrfToken);
  33. this.props.crowi.apiPost('/comments.add', JSON.stringify({comment: this.state.comment, page_id: this.props.pageId, revision_id: this.props.revisionId}));
  34. }
  35. render() {
  36. //{% if not user %}disabled{% endif %}をtextareaとbuttonに追加
  37. return (
  38. <div>
  39. <form className="form page-comment-form" id="page-comment-form" onSubmit={this.postComment}>
  40. <div className="comment-form">
  41. <div className="comment-form-user">
  42. <img src="{{ user|picture }}" className="picture img-circle" width="25" alt="{{ user.name }}" title="{{ user.name }}" />
  43. </div>
  44. <div className="comment-form-main">
  45. <div className="comment-write" id="comment-write">
  46. <textarea className="comment-form-comment form-control" id="comment-form-comment" name="comment"
  47. required placeholder="Write comments here..." onChange={this.handleChange}></textarea>
  48. <input type="checkbox" id="comment-form-is-markdown" name="isMarkdown" value="1" onChange={this.handleChange} /> Markdown<br />
  49. </div>
  50. <div className="comment-submit">
  51. <div className="pull-right">
  52. <span className="text-danger" id="comment-form-message"></span>
  53. <button type="submit" value="Submit" id="comment-form-button" className="fcbtn btn btn-sm btn-outline btn-rounded btn-primary btn-1b">
  54. Comment
  55. </button>
  56. </div>
  57. <div className="clearfix"></div>
  58. </div>
  59. </div>
  60. </div>
  61. </form>
  62. </div>
  63. );
  64. }
  65. }
  66. CommentForm.propTypes = {
  67. crowi: PropTypes.object.isRequired,
  68. pageId: PropTypes.string,
  69. revisionId: PropTypes.string,
  70. };