UserLogin.js 1.6 KB

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