2018-09-09 17:22:44 -04:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { Redirect, Link } from 'react-router-dom';
|
|
|
|
|
2018-09-13 11:58:05 -04:00
|
|
|
import ErrorBox from './error-box';
|
2018-09-13 19:19:19 -04:00
|
|
|
import InfoBox from './info-box';
|
2018-09-17 16:27:52 -04:00
|
|
|
import SupporterLogos from './supporter-logos';
|
2018-09-13 11:58:05 -04:00
|
|
|
|
2018-09-09 17:22:44 -04:00
|
|
|
class SignUp extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
username: '',
|
|
|
|
email: '',
|
|
|
|
confirm_email: '',
|
|
|
|
password: '',
|
|
|
|
show_password: '',
|
2018-09-13 11:58:05 -04:00
|
|
|
confirm_conditions: false,
|
|
|
|
error: undefined
|
2018-09-09 17:22:44 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
this.handleChange = this.handleChange.bind(this);
|
|
|
|
this.handleSubmit = this.handleSubmit.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChange(event) {
|
|
|
|
const target = event.target;
|
|
|
|
const value = target.type === 'checkbox' ? target.checked : target.value;
|
|
|
|
const name = target.name;
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
[name]: value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit(event) {
|
|
|
|
event.preventDefault();
|
2018-09-13 12:02:27 -04:00
|
|
|
this.setState({error: undefined})
|
|
|
|
|
2018-09-09 17:22:44 -04:00
|
|
|
fetch('/users', {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(this.state),
|
|
|
|
headers:{
|
|
|
|
'Content-Type': 'application/json'
|
2018-09-30 15:29:46 -04:00
|
|
|
},
|
|
|
|
credentials: 'same-origin'
|
2018-09-09 17:22:44 -04:00
|
|
|
}).then(
|
|
|
|
res => res.json()
|
|
|
|
).then(function(res){
|
|
|
|
if (res.error) {
|
2018-09-13 11:58:05 -04:00
|
|
|
this.setState({error: res.error})
|
2018-09-09 17:22:44 -04:00
|
|
|
} else {
|
2018-09-30 15:29:46 -04:00
|
|
|
fetch('/users/me', {
|
|
|
|
credentials: 'same-origin'
|
|
|
|
}).then(
|
2018-09-13 11:58:05 -04:00
|
|
|
(res) => res.json()
|
|
|
|
).then(
|
|
|
|
(user) => this.props.login(user)
|
|
|
|
).catch(
|
|
|
|
(err) => this.setState({error: err})
|
|
|
|
)
|
2018-09-09 17:22:44 -04:00
|
|
|
}
|
2018-09-13 11:58:05 -04:00
|
|
|
}.bind(this)).catch(
|
|
|
|
(err) => this.setState({error: err})
|
2018-09-09 17:22:44 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.props.user) {
|
|
|
|
return <Redirect to="/my-account.html" />
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<article>
|
|
|
|
<section className="main-col">
|
|
|
|
<h1 className="h2">Sign up</h1>
|
2018-09-13 19:19:19 -04:00
|
|
|
<InfoBox msg="Colouring London is under active development - at the moment, user accounts
|
|
|
|
and data may be deleted without notice. Caveat visitor!" />
|
2018-09-09 17:22:44 -04:00
|
|
|
<p>
|
|
|
|
Create an account to start colouring in.
|
|
|
|
</p>
|
2018-09-13 11:58:05 -04:00
|
|
|
<ErrorBox msg={this.state.error} />
|
2018-09-09 17:22:44 -04:00
|
|
|
<form onSubmit={this.handleSubmit}>
|
|
|
|
<label htmlFor="username">Username*</label>
|
|
|
|
<input name="username" id="username"
|
|
|
|
className="form-control" type="text"
|
|
|
|
value={this.state.username} onChange={this.handleChange}
|
|
|
|
placeholder="not-your-real-name" required
|
|
|
|
/>
|
|
|
|
|
|
|
|
<label htmlFor="email">Email (optional)</label>
|
|
|
|
<input name="email" id="email"
|
|
|
|
className="form-control" type="email"
|
|
|
|
value={this.state.email} onChange={this.handleChange}
|
|
|
|
placeholder="someone@example.com"
|
|
|
|
/>
|
|
|
|
|
|
|
|
<label htmlFor="confirm_email">Confirm email (optional)</label>
|
|
|
|
<input name="confirm_email" id="confirm_email"
|
|
|
|
className="form-control" type="email"
|
|
|
|
value={this.state.confirm_email} onChange={this.handleChange}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<label htmlFor="password">Password</label>
|
|
|
|
<input name="password" id="password"
|
|
|
|
className="form-control"
|
|
|
|
type={(this.state.show_password)? 'text': 'password'}
|
|
|
|
value={this.state.password} onChange={this.handleChange}
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div className="form-check">
|
|
|
|
<input id="show_password" name="show_password"
|
2018-09-13 11:58:05 -04:00
|
|
|
className="form-check-input" type="checkbox"
|
2018-09-09 17:22:44 -04:00
|
|
|
checked={this.state.show_password}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
/>
|
2018-09-13 11:58:05 -04:00
|
|
|
<label className="form-check-label" htmlFor="show_password">
|
|
|
|
Show password?
|
|
|
|
</label>
|
2018-09-09 17:22:44 -04:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="form-check">
|
|
|
|
<input id="confirm_conditions" name="confirm_conditions"
|
2018-09-13 11:58:05 -04:00
|
|
|
className="form-check-input" type="checkbox"
|
2018-09-09 17:22:44 -04:00
|
|
|
checked={this.state.confirm_conditions}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
required />
|
2018-09-13 11:58:05 -04:00
|
|
|
<label className="form-check-label" htmlFor="confirm_conditions">
|
2018-09-09 17:22:44 -04:00
|
|
|
I confirm that I have read and agree to the <a
|
|
|
|
href="/privacy-policy">privacy policy</a> and <a
|
|
|
|
href="/user-agreement">contributor agreement</a>.
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
|
2018-09-13 11:09:41 -04:00
|
|
|
<div className="buttons-container">
|
|
|
|
<input type="submit" value="Sign Up" className="btn btn-primary" />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
Do you already have an account?
|
|
|
|
|
|
|
|
<div className="buttons-container">
|
|
|
|
<Link to="login.html" className="btn btn-outline-dark">Log in</Link>
|
|
|
|
</div>
|
|
|
|
|
2018-09-09 17:22:44 -04:00
|
|
|
</form>
|
|
|
|
</section>
|
2018-09-17 16:27:52 -04:00
|
|
|
<hr />
|
|
|
|
<section className="main-col">
|
|
|
|
<SupporterLogos />
|
|
|
|
</section>
|
2018-09-09 17:22:44 -04:00
|
|
|
</article>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SignUp;
|