Advertisement
Guest User

code_sample_for_tarot_draw

a guest
Jan 21st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     handleSubmit = async (event) => {
  2.         event.preventDefault()
  3.         const userId = this.props.match.params.id
  4.         var drawValue = this.state.drawCommand
  5.         /*  handleSubmit "one-shots" a couple of different axios calls.
  6.             While conceptually, a virtual "spread" does not need to have virtual "cards",
  7.             that approach doesn't make much contexual sense - a game of Poker isn't played
  8.             with players declaring they have a 'hand' and then drawing from a deck
  9.             the two consts below make an axios POST request, and then takes the data returned in state
  10.             to make a GET request, working with state.drawValue to determine which draw_x method to fire */
  11.         const newSpread = await axios.post(`/api/users/${userId}/spreads`, this.state.newSpread)
  12.         const cards = await axios.get(`/api/users/${newSpread.data.user_id}/spreads/${newSpread.data.id}/${drawValue}`)
  13.         /*  since newSpread is a JSON object with a key-value pair that IS cards
  14.             the data returned that is now inside the const cards
  15.             can be set equal to newSpread.data.cards   */
  16.         newSpread.data.cards = cards.data
  17.         this.setState({newSpread: newSpread.data})
  18.         /*  the newSpread is appended to the end of state.spread and
  19.             the setState function triggered to dynamically update the component
  20.             bad practice, maybe */
  21.         this.state.spreads.push(this.state.newSpread)
  22.         this.setState({spreads: this.state.spreads})
  23.     }
  24.    
  25.     /*  this function evaluates the onClick args to determine
  26.         which 'draw_x' method in Spreads.rb and Cards.rb makes the external API call*/
  27.     handleDraw = async (drawCommand) => {
  28.         this.setState({drawCommand})
  29.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement