summaryrefslogtreecommitdiffstats
path: root/frontend/components/Signup.js
blob: 22a729f812c8e3ac57e71879fb76b6760657a6c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import React, { Component } from 'react';
import { Mutation, ApolloConsumer } from 'react-apollo';
import { SIGNUP_MUTATION, CURRENT_USER_QUERY } from '../queries/queries.graphql';
import Form from './styles/Form';
import Error from './ErrorMessage';

class Signup extends Component {
  state = {
    email: `wesbos@gmail.com`,
    name: 'Wes Bos',
    password: 'wes',
  };

  saveToState = e => {
    const { name, value } = e.target;
    this.setState({ [name]: value });
  };

  render() {
    return (
      <ApolloConsumer>
        {client => (
          <Mutation mutation={SIGNUP_MUTATION} variables={this.state}>
            {(signup, { loading, error }) => (
              <Form
                onSubmit={async e => {
                  e.preventDefault();
                  const res = await signup();
                  // TODO can we return theuser from signup?
                  client.query({ query: CURRENT_USER_QUERY, fetchPolicy: 'network-only' });
                }}
              >
                <fieldset disabled={loading} aria-busy={loading}>
                  <Error error={error} />
                  <h2>Sign Up for an Account</h2>
                  <label htmlFor="email">
                    Email
                    <input
                      value={this.state.email}
                      onChange={this.saveToState}
                      name="email"
                      type="text"
                      placeholder="email"
                    />
                  </label>

                  <label htmlFor="name">
                    Name
                    <input
                      type="text"
                      name="name"
                      placeholder="name"
                      value={this.state.name}
                      onChange={this.saveToState}
                    />
                  </label>

                  <label htmlFor="signupPassword">
                    Password
                    <input
                      type="password"
                      name="password"
                      id="signupPassword"
                      className="password"
                      placeholder="password"
                      value={this.state.password}
                      onChange={this.saveToState}
                    />
                  </label>

                  <button type="submit">Submit</button>
                </fieldset>
              </Form>
            )}
          </Mutation>
        )}
      </ApolloConsumer>
    );
  }
}

export default Signup;