login.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { Mutation } from 'react-apollo'
  2. import gql from 'graphql-tag'
  3. //import { CURRENT_USER_QUERY } from './User'
  4. const LOGIN_MUTATION = gql`
  5. mutation LOGIN_MUTATION($email: String!, $password: String!) {
  6. login(email: $email, password: $password) {
  7. id
  8. email
  9. name
  10. }
  11. }
  12. `
  13. class Login extends React.Component {
  14. state = {
  15. email: "",
  16. password: ""
  17. }
  18. saveToState = (ev) => {
  19. this.setState({ [ev.target.name]: ev.target.value })
  20. }
  21. render() {
  22. return <Mutation mutation={LOGIN_MUTATION} variables={this.state} refetchQueries={[{ query: CURRENT_USER_QUERY }]}>
  23. {(signin, { error, loading }) => {
  24. return <form method="post" onSubmit={async ev => {
  25. ev.preventDefault()
  26. const res = await signin()
  27. console.log(res)
  28. this.setState({ name: '', email: '', password: '' })
  29. }}>
  30. <fieldset disabled={loading} aria-busy={loading}>
  31. <h2>Sign in.</h2>
  32. <Error error={error} />
  33. <label htmlFor="email">email
  34. <input type="email" name="email" placeholder="email" value={this.state.email} onChange={this.saveToState} />
  35. </label>
  36. <label htmlFor="password">password
  37. <input type="password" name="password" placeholder="password" value={this.state.password} onChange={this.saveToState} />
  38. </label>
  39. <button type="submit">Sign In!</button>
  40. </fieldset>
  41. </form>
  42. }}
  43. </Mutation>
  44. }
  45. }
  46. export default Login