UserLogin.js 1.6 KB

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