colouring-montreal/app/src/frontend/my-account.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

import React, { Component } from 'react';
import { Redirect } from 'react-router';
2018-09-13 12:02:27 -04:00
import ErrorBox from './error-box';
class MyAccountPage extends Component {
constructor(props) {
super(props);
2018-09-13 12:02:27 -04:00
this.state = {
error: undefined
};
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event) {
event.preventDefault();
2018-09-13 12:02:27 -04:00
this.setState({error: undefined});
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})
} else {
2018-09-13 12:02:27 -04:00
this.props.logout();
}
2018-09-13 12:02:27 -04:00
}.bind(this)).catch(
(err) => this.setState({error: err})
);
}
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} />
<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;