2018-09-09 17:22:44 -04:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { Redirect } from 'react-router';
|
|
|
|
|
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-09-09 17:22:44 -04:00
|
|
|
this.handleSubmit = this.handleSubmit.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
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('/logout', {
|
|
|
|
method: 'POST'
|
|
|
|
}).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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.props && this.props.user) {
|
|
|
|
return (
|
|
|
|
<article>
|
|
|
|
<section className="main-col">
|
|
|
|
<h1 className="h3">Welcome, {this.props.user.username}</h1>
|
2018-09-13 12:02:27 -04:00
|
|
|
<ErrorBox msg={this.state.error} />
|
2018-09-09 17:22:44 -04:00
|
|
|
<form method="POST" action="/logout" onSubmit={this.handleSubmit}>
|
|
|
|
<input className="btn btn-secondary" type="submit" value="Log out"/>
|
|
|
|
</form>
|
|
|
|
</section>
|
|
|
|
</article>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<Redirect to="/login.html" />
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MyAccountPage;
|