PageCommentFormBehavior.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. * @export
  10. * @class PageCommentFormBehavior
  11. * @extends {React.Component}
  12. */
  13. export default class PageCommentFormBehavior extends React.Component {
  14. constructor(props) {
  15. super(props);
  16. }
  17. componentWillMount() {
  18. const pageComments = this.props.pageComments;
  19. if (pageComments === undefined) {
  20. return;
  21. }
  22. $('#page-comment-form').on('submit', function() {
  23. var $button = $('#comment-form-button');
  24. $button.attr('disabled', 'disabled');
  25. $.post('/_api/comments.add', $(this).serialize(), function(data) {
  26. $button.prop('disabled', false);
  27. if (data.ok) {
  28. // reload comments
  29. pageComments.init();
  30. $('#comment-form-comment').val('');
  31. $('#comment-form-message').text('');
  32. } else {
  33. $('#comment-form-message').text(data.error);
  34. }
  35. }).fail(function(data) {
  36. if (data.status !== 200) {
  37. $('#comment-form-message').text(data.statusText);
  38. }
  39. });
  40. return false;
  41. });
  42. }
  43. render() {
  44. // render nothing
  45. return <div></div>
  46. }
  47. }
  48. PageCommentFormBehavior.propTypes = {
  49. pageComments: React.PropTypes.instanceOf(PageComments),
  50. crowi: PropTypes.object.isRequired,
  51. };