PageCommentFormBehavior.js 1.5 KB

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