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. const CURRENT_USER = gql`
  16. query CURRENT_USER() {
  17. currentUser() {
  18. }
  19. }
  20. `
  21. class UserLogin extends React.Component {
  22. state = {
  23. email: '',
  24. password: ''
  25. }
  26. handleInput = event => {
  27. this.setState({ [event.target.name]: event.target.value })
  28. }
  29. render() {
  30. return (
  31. <Mutation
  32. mutation={USER_LOGIN}
  33. variables={this.state}
  34. refetchQueries={[{ query: CURRENT_USER }]}
  35. >
  36. {(userLogin, { error, loading }) => (
  37. <form
  38. method="POST"
  39. onSubmit={event => {
  40. event.preventDefault()
  41. await userLogin()
  42. this.setState({ email: '', password: '' })
  43. }}
  44. >
  45. <fieldset disabled={loading}>
  46. <h2>Log in</h2>
  47. <input type="email" name="email" id="email" placeholder="" value={this.state.email} onChange={this.handleInput} />
  48. <label htmlFor="email"></label>
  49. <input type="password" name="password" id="password" placeholder="" value={this.state.email} 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. }