PageCommentFormBehavior.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import PageComments from './PageComments';
  4. /**
  5. * Set the behavior that post comments to #page-comment-form
  6. *
  7. * This is transplanted from legacy/crowi.js -- 2017.06.03 Yuki Takei
  8. *
  9. * @author Yuki Takei <yuki@weseek.co.jp>
  10. *
  11. * @export
  12. * @class PageCommentFormBehavior
  13. * @extends {React.Component}
  14. */
  15. export default class PageCommentFormBehavior extends React.Component {
  16. constructor(props) {
  17. super(props);
  18. }
  19. componentWillMount() {
  20. const pageComments = this.props.pageComments;
  21. if (pageComments === undefined) {
  22. return;
  23. }
  24. $('#page-comment-form').on('submit', function() {
  25. var $button = $('#comment-form-button');
  26. $button.attr('disabled', 'disabled');
  27. $.post('/_api/comments.add', $(this).serialize(), function(data) {
  28. $button.prop('disabled', false);
  29. if (data.ok) {
  30. // reload comments
  31. pageComments.init();
  32. $('#comment-form-comment').val('');
  33. $('#comment-form-message').text('');
  34. } else {
  35. $('#comment-form-message').text(data.error);
  36. }
  37. }).fail(function(data) {
  38. if (data.status !== 200) {
  39. $('#comment-form-message').text(data.statusText);
  40. }
  41. });
  42. return false;
  43. });
  44. }
  45. render() {
  46. // render nothing
  47. return <div></div>
  48. }
  49. }
  50. PageCommentFormBehavior.propTypes = {
  51. pageComments: PropTypes.instanceOf(PageComments),
  52. crowi: PropTypes.object.isRequired,
  53. };