UserCreate.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. return (
  27. <Mutation
  28. mutation={USER_CREATE}
  29. variables={this.state}
  30. refetchQueries={[{ query: CURRENT_USER }]}
  31. >
  32. {(userCreate, { error, loading }) => (
  33. <form
  34. method="POST"
  35. onSubmit={async event => {
  36. event.preventDefault()
  37. await userCreate()
  38. this.setState({ name: '', email: '', abbreviation: '', password: '' })
  39. }}
  40. >
  41. <fieldset>
  42. <label htmlFor="name">User name</label>
  43. <input type="text" name="name" id="name"
  44. placeholder="User name"
  45. value={this.state.name}
  46. onChange={this.handleInput} />
  47. <label htmlFor="abbreviation">Abbreviation</label>
  48. <input type="text" name="abbreviation" id="abbreviation"
  49. placeholder="Abbreviation"
  50. value={this.state.abbreviation}
  51. onChange={this.handleInput} />
  52. <label htmlFor="email">Email</label>
  53. <input type="email" name="email" id="email"
  54. placeholder="Email"
  55. value={this.state.email}
  56. onChange={this.handleInput} />
  57. <label htmlFor="password">Password</label>
  58. <input type="password" name="password" id="password"
  59. placeholder="Password"
  60. value={this.state.password}
  61. onChange={this.handleInput} />
  62. <button type="submit">Create</button>
  63. </fieldset>
  64. </form>
  65. )}
  66. </Mutation>
  67. )
  68. }
  69. }
  70. export default UserCreate