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