colouring-montreal/app/src/frontend/pages/my-account.tsx

179 lines
6.3 KiB
TypeScript
Raw Normal View History

import React, { Component, FormEvent } from 'react';
import { Link, Redirect } from 'react-router-dom';
2019-05-27 13:26:29 -04:00
import PropTypes from 'prop-types';
import ConfirmationModal from '../confirmation-modal';
import ErrorBox from '../error-box';
2018-09-13 12:02:27 -04:00
class MyAccountPage extends Component<any, any> { // TODO: add proper types
static propTypes = { // TODO: generate propTypes from TS
user: PropTypes.shape({
username: PropTypes.string,
email: PropTypes.string,
registered: PropTypes.instanceOf(Date), // TODO: check if fix correct
api_key: PropTypes.string,
error: PropTypes.object
}),
updateUser: PropTypes.func,
logout: PropTypes.func
};
constructor(props) {
super(props);
2018-09-13 12:02:27 -04:00
this.state = {
error: undefined,
showDeleteConfirm: false
2018-09-13 12:02:27 -04:00
};
2018-10-20 07:20:10 -04:00
this.handleLogout = this.handleLogout.bind(this);
this.handleGenerateKey = this.handleGenerateKey.bind(this);
}
2018-10-20 07:20:10 -04:00
handleLogout(event) {
event.preventDefault();
2018-09-13 12:02:27 -04:00
this.setState({error: undefined});
2019-08-14 09:05:49 -04:00
fetch('/api/logout', {
method: 'POST',
credentials: 'same-origin'
}).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})
);
}
2018-10-20 07:20:10 -04:00
handleGenerateKey(event) {
event.preventDefault();
this.setState({error: undefined});
2019-08-14 09:05:49 -04:00
fetch('/api/api/key', {
2018-10-20 07:20:10 -04:00
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})
);
}
confirmDelete(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
this.setState({ showDeleteConfirm: true });
}
hideConfirmDelete() {
this.setState({ showDeleteConfirm: false });
}
async handleDelete() {
this.setState({ error: undefined });
try {
const res = await fetch('/api/users/me', {
method: 'DELETE',
credentials: 'same-origin'
});
const data = await res.json();
if(data.error) {
this.setState({ error: data.error });
} else {
this.props.logout();
}
} catch (err) {
this.setState({ error: err });
} finally {
this.hideConfirmDelete();
}
}
render() {
if (this.props.user && !this.props.user.error) {
return (
<article>
<section className="main-col">
<h1 className="h1">Welcome, {this.props.user.username}!</h1>
2018-10-20 07:20:10 -04:00
<p>
Colouring London is under active development, Please <a href="https://discuss.colouring.london/">discuss
suggestions for improvements</a> and <a
2019-05-27 11:39:16 -04:00
href="https://github.com/tomalrussell/colouring-london/issues">
report issues or problems</a>.
</p>
2018-09-13 12:02:27 -04:00
<ErrorBox msg={this.state.error} />
<form onSubmit={this.handleLogout}>
2018-10-20 07:20:10 -04:00
<div className="buttons-container">
2019-04-04 07:25:32 -04:00
<Link to="/edit/age.html" className="btn btn-warning">Start colouring</Link>
2018-10-20 07:20:10 -04:00
<input className="btn btn-secondary" type="submit" value="Log out"/>
</div>
</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/>
2019-04-04 07:25:32 -04:00
<h2 className="h2">Techical details</h2>
<p>Are you a software developer? If so, you might be interested in these.</p>
2018-10-20 07:20:10 -04:00
<h3 className="h3">API key</h3>
<p>{this.props.user.api_key? this.props.user.api_key : '-'}</p>
<form onSubmit={this.handleGenerateKey} className="form-group mb-3">
2019-04-04 07:25:32 -04:00
<input className="btn btn-warning" type="submit" value="Generate API key"/>
2018-10-20 07:20:10 -04:00
</form>
2019-04-04 07:25:32 -04:00
<h3 className="h3">GitHub</h3>
<a href="http://github.com/tomalrussell/colouring-london/">Colouring London Github repository</a>
<hr />
<h2 className="h2">Account actions</h2>
<form
onSubmit={e => this.confirmDelete(e)}
className="form-group mb-3"
>
<input className="btn btn-danger" type="submit" value="Delete account" />
</form>
<ConfirmationModal
show={this.state.showDeleteConfirm}
title="Confirm account deletion"
description="Are you sure you want to delete your account? This cannot be undone."
confirmButtonText="Delete account"
2019-08-16 10:20:54 -04:00
confirmButtonClass="btn-danger"
onConfirm={() => this.handleDelete()}
onCancel={() => this.hideConfirmDelete()}
/>
</section>
</article>
);
} else {
return (
<Redirect to="/login.html" />
)
}
}
}
export default MyAccountPage;