login.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. this.setState({ name: '', email: '', password: '' })
  28. }}>
  29. <fieldset disabled={loading} aria-busy={loading}>
  30. <h2>Sign in.</h2>
  31. <Error error={error} />
  32. <label htmlFor="email">email
  33. <input type="email" name="email" placeholder="email" value={this.state.email} onChange={this.saveToState} />
  34. </label>
  35. <label htmlFor="password">password
  36. <input type="password" name="password" placeholder="password" value={this.state.password} onChange={this.saveToState} />
  37. </label>
  38. <button type="submit">Sign In!</button>
  39. </fieldset>
  40. </form>
  41. }}
  42. </Mutation>
  43. }
  44. }
  45. export default Login