CommentForm.js 3.1 KB

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