Guest User

Untitled

a guest
Feb 16th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.03 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. <form style={{display: this.state.showEditForm ? 'block' : 'none'}}>
  136. <input value={this.state.updatedName} placeholder={response.data.player.name}
  137. onChange={e => this.setState({updatedName: e.target.value})}/>
  138. <input value={this.state.updatedPosition} placeholder={response.data.player.position}
  139. onChange={e => this.setState({updatedPosition: e.target.value})}/>
  140. <input value={this.state.updatedTeam} placeholder={response.data.player.team}
  141. onChange={e => this.setState({updatedTeam: e.target.value})}/>
  142. <input value={this.state.updatedJerseyNumber} placeholder={response.data.player.jerseyNumber}
  143. onChange={e => this.setState({updatedJerseyNumber: e.target.value})}/>
  144. <div className="yes-no-buttons-div">
  145. <button onClick={e => {
  146. e.preventDefault();
  147. this.setState({updatedWonSuperBowl: true})
  148. }} className="yes-button" style={{background: this.state.updatedWonSuperBowl ? 'green' : 'transparent'}}>
  149. Yes
  150. </button>
  151.  
  152. <button onClick={e => {
  153. e.preventDefault();
  154. this.setState({updatedWonSuperBowl: false})
  155. }} className="no-button" style={{background: !this.state.updatedWonSuperBowl ? 'red' : 'transparent'}}>
  156. No
  157. </button>
  158. </div>
  159. </form>
  160. </div>
  161. );
  162. }
  163. //Else return a loading html.
  164. return <p>Loading....</p>
  165. }}
  166. </Query>
  167. <Mutation
  168. mutation={updatePlayerMutation}>
  169. {(updatePlayer, error) => {
  170. if(error) {
  171. console.log("update player mutation error------", error);
  172. }
  173. //If the updatedPlayer mutate method is returned then return the button that will open the edit form or update the player itself.
  174. if(updatePlayer) {
  175. //If your showEditForm state is true the text of the button will be update
  176. //Else if it is false, have the text be edit.
  177. //For the onClick event handler have it conditional use your mutation function, or alter your state to open the edit form.
  178. return (
  179. <button className='update-button'
  180. onClick={e => {
  181. e.preventDefault();
  182. console.log("thi.stat.'-------", this.state.showEditForm)
  183. if(this.state.showEditForm)
  184. this.updatePlayerFunc(updatePlayer);
  185. else
  186. return this.setState({showEditForm: true})
  187. }}>
  188. {this.state.showEditForm ? 'Update' : 'Edit'}
  189. </button>
  190. );
  191. }
  192. return <div/>
  193. }}
  194. </Mutation>
  195. <Mutation
  196. mutation={deletePlayerMutation}>
  197. {(deletePlayer, error) => {
  198. if(error) {
  199. console.log("update player mutation error------", error);
  200. }
  201. if(deletePlayer) {
  202. //If the edit form is not showing then you can delete a player
  203. // else return an alert the user they can't delete that specified player.
  204. return (
  205. <button className='delete-button'
  206. onClick={(e) => {
  207. e.preventDefault();
  208. if(this.state.showEditForm)
  209. alert("Can't delete player becuase your edit form is displayed.");
  210. else
  211. this.deletePlayerFunc(deletePlayer);
  212. }}
  213. >
  214. Delete
  215. </button>
  216. );
  217. }
  218. return <div/>
  219. }}
  220. </Mutation>
  221. </div>
  222. );
  223. }
  224. }
Add Comment
Please, Sign In to add comment