Guest User

Untitled

a guest
Feb 16th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. import React, { PureComponent } from 'react';
  2. //import your UpdatePlayer.css and CreatePlayer.css for your Player styles.
  3. import './CreatePlayer.css';
  4. import './UpdatePlayer.css';
  5. //import your QUery and Mutation component for retrieving and manipulating data via it's query and mutation arguments.
  6. import { Query, Mutation } from 'react-apollo';
  7. import gql from 'graphql-tag';
  8.  
  9. //Define your Schema that will be responsible for retrieving data via the route id argument.
  10. //We will alias our query to player since it is more readable.
  11. const getPlayerQuery = gql`
  12. query GetPlayer($id: String) {
  13. player: getPlayer(id: $id) {
  14. name
  15. position
  16. team
  17. jerseyNumber
  18. wonSuperBowl
  19. }
  20. }
  21. `;
  22.  
  23.  
  24. //Define a mutation schema that will be responsible for updating a player, with nullable arguments.
  25. const updatePlayerMutation = gql`
  26. mutation UpdatePlayer($id: String, $name: String, $position: String, $team: String, $jerseyNumber: Int, $wonSuperBowl: Boolean) {
  27. updatePlayer(id: $id, name: $name, position: $position, team: $team, jerseyNumber: $jerseyNumber, wonSuperBowl: $wonSuperBowl) {
  28. name
  29. position
  30. team
  31. jerseyNumber
  32. wonSuperBowl
  33. }
  34. }
  35. `;
  36.  
  37. //Define a mutation schema that will be responsible for deleting a player using an id.
  38. const deletePlayerMutation = gql`
  39. mutation DeletePlayer($id: String) {
  40. deletePlayer(id: $id) {
  41. id
  42. }
  43. }
  44. `;
  45.  
  46.  
  47. export default class Player extends PureComponent {
  48. //Define your state that will be responsible for opening a edit form.
  49. constructor() {
  50. //Use super to initialize the parent props.
  51. super();
  52. //Define a boolean for showing your editForm, and fields to use to update your player.
  53. this.state = {
  54. showEditForm: false,
  55. updatedName: '',
  56. updatedPosition: '',
  57. updatedTeam: '',
  58. updatedJerseyNumber: '',
  59. updatedWonSuperBowl: false
  60. }
  61. }
  62.  
  63. //Define your updatePlayerFunc for updating a player.
  64. //Make it asynchronous so your page will refresh with your new data
  65. async updatePlayerFunc(func) {
  66. //Assign a variable to your url's id.
  67. const id = this.props.match.params.id;
  68. //Define your updatedPlayer.
  69. //Which will hold all your state for updating a player.
  70. let updatedPlayer = {
  71. id,
  72. name: this.state.updatedName,
  73. position: this.state.updatedPosition,
  74. team: this.state.updatedTeam,
  75. jerseyNumber: this.state.updatedJerseyNumber,
  76. wonSuperBowl: this.state.updatedWonSuperBowl
  77. };
  78.  
  79. //Now loop through your updatedPlayer keys, and delete the property that are null or an empty string, and if it is not a boolean
  80. //Since false is falsey.
  81. for(var key in updatedPlayer) {
  82. if(!updatedPlayer[key] && typeof updatedPlayer[key] !== 'boolean') {
  83. delete updatedPlayer[key]
  84. }
  85. }
  86.  
  87. //Now pass your updatePLayer as a property for your variables for your func argument.
  88. //And assign a optimistic resposne which will be the returnedPlayer.
  89. await func({
  90. variables: updatedPlayer
  91. })
  92. //Now refresh your location using your history object's go method.
  93. //No need to refresh since our webpage will refresh and automatically set our state to it's initial values.
  94. this.props.history.go();
  95. }
  96.  
  97. //Define your deletePlayerFunc for deleting a player,
  98. //and also have the method asynchronous so you would delete player before redirecting the user to the player's list.
  99. async deletePlayerFunc(func) {
  100. //Now assign the id of the current player from the url.
  101. const id = this.props.match.params.id;
  102. //Now pass your id to the func which would be where your mutate delete method will take a object with a variables property of your id.
  103. await func({ variables: { id } });
  104. //Then redirect your users to the player's list.
  105. this.props.history.push('/players');
  106. }
  107.  
  108. render() {
  109. //Assign a variable from your route.
  110. const playerId = this.props.match.params.id;
  111. console.log('this.state.updatePlayer', this.state.returnedPlayer);
  112. return (
  113. <div className="container update-container">
  114. {/*Have your Mutations and your result from your getPlayerQuery nested in your Query component. */}
  115. {/*Pass your id to your variables of your Query */}
  116. <Query
  117. query={getPlayerQuery}
  118. variables={{"id": playerId}}
  119. >
  120. {(response, error) => {
  121. if(error) {
  122. //If there is an error, log an error to the console.
  123. //And return html holding your error.
  124. console.log('Get Players Query Error-------', error);
  125. return <p>{JSON.stringify(error)}</p>
  126. }
  127. if(response && response.data && response.data.player) {
  128. return (
  129. <div>
  130. <h2>{response.data.player.name}</h2>
  131. <h4>Position: {response.data.player.position}</h4>
  132. <h4>Team: {response.data.player.team}</h4>
  133. <h4>#: {response.data.player.jerseyNumber}</h4>
  134. <h4>{response.data.player.wonSuperBowl ? 'Yes' : 'No'}</h4>
  135. </div>
  136. );
  137. }
  138. //Else return a loading html.
  139. return <p>Loading....</p>
  140. }}
  141. </Query>
  142. </div>
  143. );
  144. }
  145. }
Add Comment
Please, Sign In to add comment