2018-09-09 17:22:44 -04:00
|
|
|
import React, { Component } from 'react';
|
2018-09-13 19:19:19 -04:00
|
|
|
import { Link, Redirect } from 'react-router-dom';
|
2018-09-09 17:22:44 -04:00
|
|
|
|
2018-09-13 12:02:27 -04:00
|
|
|
import ErrorBox from './error-box';
|
|
|
|
|
2018-09-09 17:22:44 -04:00
|
|
|
class MyAccountPage extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2018-09-13 12:02:27 -04:00
|
|
|
this.state = {
|
|
|
|
error: undefined
|
|
|
|
};
|
2018-10-20 07:20:10 -04:00
|
|
|
this.handleLogout = this.handleLogout.bind(this);
|
|
|
|
this.handleGenerateKey = this.handleGenerateKey.bind(this);
|
2018-09-09 17:22:44 -04:00
|
|
|
}
|
|
|
|
|
2018-10-20 07:20:10 -04:00
|
|
|
handleLogout(event) {
|
2018-09-09 17:22:44 -04:00
|
|
|
event.preventDefault();
|
2018-09-13 12:02:27 -04:00
|
|
|
this.setState({error: undefined});
|
|
|
|
|
2018-09-09 17:22:44 -04:00
|
|
|
fetch('/logout', {
|
2018-09-30 15:29:46 -04:00
|
|
|
method: 'POST',
|
|
|
|
credentials: 'same-origin'
|
2018-09-09 17:22:44 -04:00
|
|
|
}).then(
|
|
|
|
res => res.json()
|
|
|
|
).then(function(res){
|
|
|
|
if (res.error) {
|
2018-09-13 12:02:27 -04:00
|
|
|
this.setState({error: res.error})
|
2018-09-09 17:22:44 -04:00
|
|
|
} else {
|
2018-09-13 12:02:27 -04:00
|
|
|
this.props.logout();
|
2018-09-09 17:22:44 -04:00
|
|
|
}
|
2018-09-13 12:02:27 -04:00
|
|
|
}.bind(this)).catch(
|
|
|
|
(err) => this.setState({error: err})
|
2018-09-09 17:22:44 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-20 07:20:10 -04:00
|
|
|
handleGenerateKey(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.setState({error: undefined});
|
|
|
|
|
|
|
|
fetch('/api/key', {
|
|
|
|
method: 'POST',
|
|
|
|
credentials: 'same-origin'
|
|
|
|
}).then(
|
|
|
|
res => res.json()
|
|
|
|
).then(function(res){
|
|
|
|
if (res.error) {
|
|
|
|
this.setState({error: res.error})
|
|
|
|
} else {
|
|
|
|
this.props.updateUser(res);
|
|
|
|
}
|
|
|
|
}.bind(this)).catch(
|
|
|
|
(err) => this.setState({error: err})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-09 17:22:44 -04:00
|
|
|
render() {
|
2018-09-30 15:29:46 -04:00
|
|
|
if (this.props.user && !this.props.user.error) {
|
2018-09-09 17:22:44 -04:00
|
|
|
return (
|
|
|
|
<article>
|
|
|
|
<section className="main-col">
|
2018-09-13 19:19:19 -04:00
|
|
|
<h1 className="h1">Welcome, {this.props.user.username}!</h1>
|
2018-10-20 07:20:10 -04:00
|
|
|
<p>
|
2018-09-13 19:19:19 -04:00
|
|
|
|
|
|
|
Colouring London is under active development, please report any
|
|
|
|
bugs on <a href="http://github.com/tomalrussell/colouring-london/issues">
|
|
|
|
GitHub</a>.
|
|
|
|
|
|
|
|
</p>
|
2018-09-13 12:02:27 -04:00
|
|
|
<ErrorBox msg={this.state.error} />
|
2018-10-20 07:20:10 -04:00
|
|
|
<form method="POST" action="/logout" onSubmit={this.handleLogout}>
|
|
|
|
<div className="buttons-container">
|
|
|
|
<Link to="/map/age.html" className="btn btn-primary">Start colouring</Link>
|
|
|
|
<input className="btn btn-secondary" type="submit" value="Log out"/>
|
|
|
|
</div>
|
2018-09-09 17:22:44 -04:00
|
|
|
</form>
|
2018-10-20 07:20:10 -04:00
|
|
|
|
|
|
|
<hr/>
|
|
|
|
|
|
|
|
<h2 className="h2">My Details</h2>
|
|
|
|
<h3 className="h3">Username</h3>
|
|
|
|
<p>{this.props.user.username}</p>
|
|
|
|
<h3 className="h3">Email Address</h3>
|
|
|
|
<p>{this.props.user.email? this.props.user.email : '-'}</p>
|
|
|
|
<h3 className="h3">Registered</h3>
|
|
|
|
<p>{this.props.user.registered.toString()}</p>
|
|
|
|
|
|
|
|
<hr/>
|
|
|
|
|
|
|
|
<h2 className="h2">Experimental features</h2>
|
|
|
|
<h3 className="h3">API key</h3>
|
|
|
|
<p>{this.props.user.api_key? this.props.user.api_key : '-'}</p>
|
|
|
|
<form method="POST" action="/api/key" onSubmit={this.handleGenerateKey}>
|
|
|
|
<input className="btn btn-primary" type="submit" value="Generate API key"/>
|
|
|
|
</form>
|
|
|
|
|
2018-09-09 17:22:44 -04:00
|
|
|
</section>
|
|
|
|
</article>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<Redirect to="/login.html" />
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MyAccountPage;
|