Advertisement
Guest User

Untitled

a guest
May 29th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. class CreateCommentMutation extends Relay.Mutation {
  2. static fragments = {
  3. story: () => Relay.QL`
  4. fragment on Story { id }
  5. `,
  6. };
  7. getMutation() {
  8. return Relay.QL`
  9. mutation{ createComment }
  10. `;
  11. }
  12. getFatQuery() {
  13. return Relay.QL`
  14. fragment on CreateCommentPayload {
  15. story { comments },
  16. }
  17. `;
  18. }
  19. getConfigs() {
  20. return [{
  21. type: 'FIELDS_CHANGE',
  22. fieldIDs: { story: this.props.story.id },
  23. }];
  24. }
  25. getVariables() {
  26. return {
  27. name: this.props.name,
  28. comments: this.props.comments
  29. };
  30. }
  31. }
  32.  
  33. class Comment extends React.Component {
  34. render() {
  35. var {id, text} = this.props.comment;
  36. return <li key={id}>{text}</li>;
  37. }
  38. }
  39. Comment = Relay.createContainer(Comment, {
  40. fragments: {
  41. comment: () => Relay.QL`
  42. fragment on Comment {
  43. id,
  44. text,
  45. }
  46. `,
  47. },
  48. });
  49.  
  50. class Story extends React.Component {
  51. _handleSubmit = (e) => {
  52. e.preventDefault();
  53. console.log(this.refs.newCommentInput1.value);
  54. console.log(this.refs.newCommentInput2.value);
  55. Relay.Store.commitUpdate(
  56. new CreateCommentMutation({
  57. story: this.props.story,
  58. name: "Blah",
  59. comments: [
  60. {text: this.refs.newCommentInput1.value},
  61. {text: this.refs.newCommentInput2.value}
  62. ]
  63. })
  64. );
  65. this.refs.newCommentInput1.value = '';
  66. this.refs.newCommentInput2.value = '';
  67. }
  68. render() {
  69. var {comments} = this.props.story;
  70. return (
  71. <form onSubmit={this._handleSubmit}>
  72. <h1>Breaking News</h1>
  73. <p>The peanut is neither a pea nor a nut.</p>
  74. <strong>Discuss:</strong>
  75. <ul>
  76. {comments.map(
  77. comment => <Comment comment={comment} />
  78. )}
  79. </ul>
  80. <input
  81. key={1}
  82. placeholder="Weigh in&hellip;"
  83. ref="newCommentInput1"
  84. type="text"
  85. /><br/><br/>
  86. <input
  87. key={2}
  88. placeholder="Weigh in&hellip;"
  89. ref="newCommentInput2"
  90. type="text"
  91. /><br/><br/>
  92. <input type="submit" value="Submit" />
  93. </form>
  94. );
  95. }
  96. }
  97. Story = Relay.createContainer(Story, {
  98. fragments: {
  99. story: () => Relay.QL`
  100. fragment on Story {
  101. comments {
  102. ${Comment.getFragment('comment')},
  103. },
  104. ${CreateCommentMutation.getFragment('story')},
  105. }
  106. `,
  107. },
  108. });
  109.  
  110. class StoryHomeRoute extends Relay.Route {
  111. static routeName = 'Home';
  112. static queries = {
  113. story: (Component) => Relay.QL`
  114. query StoryQuery {
  115. story { ${Component.getFragment('story')} },
  116. }
  117. `,
  118. };
  119. }
  120.  
  121. ReactDOM.render(
  122. <Relay.RootContainer
  123. Component={Story}
  124. route={new StoryHomeRoute()}
  125. />,
  126. mountNode
  127. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement