PageCommentFormBehavior.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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-is-markdown').prop('checked', false);
  34. $('#comment-form-message').text('');
  35. }
  36. else {
  37. $('#comment-form-message').text(data.error);
  38. }
  39. }).fail(function(data) {
  40. if (data.status !== 200) {
  41. $('#comment-form-message').text(data.statusText);
  42. }
  43. });
  44. return false;
  45. });
  46. }
  47. render() {
  48. // render nothing
  49. return <div></div>;
  50. }
  51. }
  52. PageCommentFormBehavior.propTypes = {
  53. pageComments: PropTypes.instanceOf(PageComments),
  54. crowi: PropTypes.object.isRequired,
  55. };