summaryrefslogtreecommitdiffstats
path: root/frontend/components/Signup.js
blob: 746f1e4e7936f6ff178c9e872a79c97692a5f6cc (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
83
84
85
86
87
88
89
90
91
92
import React, { Component } from 'react';
import { graphql, compose } from 'react-apollo';
import { SIGNUP_MUTATION, CURRENT_USER_QUERY } from '../queries';

class Signup extends Component {
  state = {
    email: `wesbos+${Date.now()}@gmail.com`,
    name: 'Wes Bos',
    password: 'abc123',
    error: undefined,
  };

  createUser = async e => {
    e.preventDefault();
    // pull the values from state
    const { email, name, password } = this.state;
    // create a mutation
    // TODO: handle any errors
    // turn loading on
    this.setState({ loading: true });
    console.log({ name, email, password });
    try {
      const res = await this.props.signup({
        // pass in those variables from state
        variables: { name, email, password },
      });
      localStorage.setItem('token', res.data.signup.token);
      // TODO refetch current user query
      this.props.currentUser.refetch();
    } catch (error) {
      this.setState({ error });
      console.dir(error);
    }
    this.setState({ loading: false });
  };

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

  render() {
    return (
      <div>
        {this.state.loading ? 'LOADING...' : 'Ready!'}

        {this.state.error ? <p>{this.state.error.message}</p> : null}

        <form onSubmit={this.createUser}>
          <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="password">
            Password
            <input
              type="password"
              name="password"
              id="password"
              className="password"
              placeholder="password"
              value={this.state.password}
              onChange={this.saveToState}
            />
          </label>

          <button type="submit">Submit</button>
        </form>
      </div>
    );
  }
}

// When we submit this mutation, we need to update our store - we have a few ways to do that:
// One - we can go nucular and run refetchQueries() which will just go get everything - this is easy, but at the cost of efficiency.

const userEnhancer = graphql(CURRENT_USER_QUERY, { name: 'currentUser' });

const signupEnhancer = graphql(SIGNUP_MUTATION, {
  name: 'signup',
  options: {
    refetchQueries: ['currentUser'],
  },
});

export default compose(userEnhancer, signupEnhancer)(Signup);