Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. import React from 'react';
  2. import gql from 'graphql-tag';
  3. import { Mutation } from 'react-apollo';
  4.  
  5. // Create a GraphQL mutation for comment submissions.
  6. const commentSubmitQuery = gql`
  7. mutation($author: String, $commentOn: Int, $content: String, $authorEmail: String) {
  8. createComment(
  9. input: {
  10. clientMutationId: "CreateComment"
  11. author: $author
  12. commentOn: $commentOn
  13. content: $content
  14. authorEmail: $authorEmail
  15. }
  16. ) {
  17. success
  18. }
  19. }
  20. `;
  21.  
  22. // Our main component class.
  23. class CommentForm extends React.Component {
  24. constructor(props) {
  25. super(props);
  26. // Set the initial state.
  27. this.state = {
  28. commentStatus: false,
  29. post: props.postId,
  30. comment: '',
  31. author: '',
  32. email: '',
  33. url: '',
  34. };
  35.  
  36. // Bind input changes.
  37. this.handleInputChange = this.handleInputChange.bind(this);
  38. }
  39.  
  40. // Handles input change events.
  41. handleInputChange(event) {
  42. const target = event.target;
  43. const value = event.type === 'checkbox' ? target.checked : target.value;
  44. const name = target.name;
  45.  
  46. // Sets the state of the input field.
  47. this.setState({
  48. [name]: value,
  49. });
  50. }
  51.  
  52. // Renders the comment form elements.
  53. renderCommentForm() {
  54. return (
  55. // Wrap it in our mutation.
  56. <Mutation
  57. mutation={commentSubmitQuery}
  58. // Set completion state.
  59. onCompleted={() => {
  60. this.setState({ commentStatus: 'success' });
  61. }}
  62. // Set error state.
  63. onError={() => {
  64. this.setState({ commentStatus: 'error' });
  65. }}
  66. >
  67. {(addComment) => (
  68. // Render the form.
  69. <form
  70. onSubmit={(event) => {
  71. // Prevent default form submit behavior.
  72. event.preventDefault();
  73. // Set initial loading state on submission.
  74. this.setState({ commentStatus: 'loading' });
  75. // Run the mutation.
  76. addComment({
  77. variables: {
  78. author: this.state.author,
  79. commentOn: this.state.post,
  80. content: this.state.comment,
  81. authorEmail: this.state.email,
  82. },
  83. });
  84. }}
  85. >
  86. <label htmlFor="author">Author</label>
  87. <input name="author" value={this.state.author} onChange={this.handleInputChange} />
  88. <label htmlFor="email">Email</label>
  89. <input name="email" value={this.state.email} onChange={this.handleInputChange} />
  90. <label htmlFor="author">Website</label>
  91. <input name="url" value={this.state.url} onChange={this.handleInputChange} />
  92. <label htmlFor="comment">Comment</label>
  93. <textarea name="comment" value={this.state.comment} onChange={this.handleInputChange} />
  94. <input name="submit" type="submit" value="Post Comment" />
  95. </form>
  96. )}
  97. </Mutation>
  98. );
  99. }
  100.  
  101. // Render the comment form.
  102. render() {
  103. // Check comment status from component state and display messages or form.
  104. switch (this.state.commentStatus) {
  105. case 'success':
  106. return 'Your comment has been successfully submitted.';
  107. case 'loading':
  108. return 'Please wait. Your comment is being submitted.';
  109. case 'error':
  110. return 'There was an error in your submission. Please try again later.';
  111. default:
  112. return this.renderCommentForm();
  113. }
  114. }
  115. }
  116.  
  117. export default CommentForm;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement