UserCreate.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import React from 'react'
  2. import gql from 'graphql-tag'
  3. import { Mutation } from 'react-apollo'
  4. import { CURRENT_USER } from './User'
  5. const USER_CREATE = gql`
  6. mutation USER_CREATE($email: String!, $name: String!, $abbreviation: String!, $password: String!) {
  7. createUser(email: $email, name: $name, abbreviation: $abbreviation, password: $password) {
  8. id
  9. name
  10. email
  11. abbreviation
  12. }
  13. }
  14. `
  15. class UserCreate extends React.Component {
  16. state = {
  17. name: '',
  18. email: '',
  19. abbreviation: '',
  20. password: ''
  21. }
  22. handleInput = event => {
  23. this.setState({ [event.target.name]: event.target.value })
  24. }
  25. render() {
  26. <Mutation
  27. mutation={USER_CREATE}
  28. variables={this.state}
  29. refetchQueries={[{ query: CURRENT_USER }]}
  30. >
  31. {(userCreate, { error, loading }) => (
  32. <form
  33. method="POST"
  34. onSubmit={async event => {
  35. event.preventDefault()
  36. await userCreate()
  37. this.setState({ name: '', email: '', abbreviation: '', password: '' })
  38. }}
  39. >
  40. <input type="text" name="name" id="name" value={this.state.name} onChange={this.handleInput} />
  41. <label htmlFor="name"></label>
  42. <input type="text" name="abbreviation" id="abbreviation" value={this.state.abbreviation} onChange={this.handleInput} />
  43. <label htmlFor="abbreviation"></label>
  44. <input type="text" name="email" id="email" value={this.state.email} onChange={this.handleInput} />
  45. <label htmlFor="email"></label>
  46. <input type="password" name="password" id="password" value={this.state.password} onChange={this.handleInput} />
  47. <label htmlFor="password"></label>
  48. <button type="submit">Create</button>
  49. </form>
  50. )}
  51. </Mutation>
  52. }
  53. }
  54. export default UserCreate