-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathsignup.js
128 lines (116 loc) · 2.47 KB
/
signup.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { graphql } from 'react-apollo'
import gql from 'graphql-tag'
class signup extends React.Component {
state = {
email: null,
fullname: null,
password: null,
error: null
}
onFormSubmit = e => {
e.preventDefault()
let { email, fullname, password } = this.state
// Check non null email && password
if (typeof email === 'string' && typeof password === 'string') {
// trim fields
email = email.trim()
fullname = fullname.trim()
password = password.trim()
// Check for email && password length
if (email.length > 0 && password.length > 0) {
this.props
.mutate({
variables: {
email,
fullname,
password
}
})
.then(() => {
this.setState({ error: null })
window.location = '/'
})
.catch(({ graphQLErrors: err }) =>
this.setState({ error: err[0].message })
)
} else {
this.setState({ error: "Email & Password Field shouldn't be empty" })
}
} else {
this.setState({ error: 'Email & Password Field Required!' })
}
}
render() {
return (
<form onSubmit={this.onFormSubmit}>
<div>
<span className="error">{this.state.error}</span>
<label> Email Address </label>
<input
type="email"
onInput={e => this.setState({ email: e.target.value })}
placeholder="[email protected]"
/>
</div>
<div>
<label> Fullname </label>
<input
type="text"
onInput={e => this.setState({ fullname: e.target.value })}
placeholder="John Doe"
/>
</div>
<div>
<label> Password </label>
<input
type="password"
onInput={e => this.setState({ password: e.target.value })}
placeholder="******"
/>
</div>
<div>
<button> Sign up </button>
</div>
<style jsx>
{`
* {
box-sizing: border-box;
margin: 0;
}
h1 {
margin: 2rem 0;
}
label {
display: block;
}
form > div {
margin-top: 1rem;
}
input,
button {
padding: 0.5rem;
}
button {
width: 12rem;
border: none;
cursor: pointer;
}
.error {
color: red;
display: block;
margin: 1rem 0;
}
`}
</style>
</form>
)
}
}
const mutator = gql`
mutation createUser($email: String!, $fullname: String, $password: String!) {
createUser(email: $email, fullname: $fullname, password: $password) {
email
}
}
`
export default graphql(mutator)(signup)