UserCreate.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import gql from 'graphql-tag'
  2. import { Mutation } from 'react-apollo'
  3. import { CURRENT_USER } from './User'
  4. const USER_CREATE = gql`
  5. mutation USER_CREATE($email: String!, $name: String!, $abbreviation: String!, $password: String!) {
  6. createUser(email: $email, name: $name, abbreviation: $abbreviation, password: $password) {
  7. id
  8. name
  9. email
  10. abbreviation
  11. }
  12. }
  13. `
  14. class UserCreate extends React.Component {
  15. state = {
  16. name: '',
  17. email: '',
  18. abbreviation: '',
  19. password: ''
  20. }
  21. handleInput = event => {
  22. this.setState({ [event.target.name]: event.target.value })
  23. }
  24. render() {
  25. return (
  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. <fieldset>
  41. <label htmlFor="name">User name</label>
  42. <input type="text" name="name" id="name"
  43. placeholder="User name"
  44. value={this.state.name}
  45. onChange={this.handleInput} />
  46. <label htmlFor="abbreviation">Abbreviation</label>
  47. <input type="text" name="abbreviation" id="abbreviation"
  48. placeholder="Abbreviation"
  49. value={this.state.abbreviation}
  50. onChange={this.handleInput} />
  51. <label htmlFor="email">Email</label>
  52. <input type="email" name="email" id="email"
  53. placeholder="Email"
  54. value={this.state.email}
  55. onChange={this.handleInput} />
  56. <label htmlFor="password">Password</label>
  57. <input type="password" name="password" id="password"
  58. placeholder="Password"
  59. value={this.state.password}
  60. onChange={this.handleInput} />
  61. <button type="submit">Create</button>
  62. </fieldset>
  63. </form>
  64. )}
  65. </Mutation>
  66. )
  67. }
  68. }
  69. export default UserCreate