2019-08-16 08:21:12 -04:00
|
|
|
import React, { Component, FormEvent } 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
|
|
|
|
2020-01-02 05:59:13 -05:00
|
|
|
import { apiDelete, apiPost } from '../apiHelpers';
|
2019-08-23 09:16:40 -04:00
|
|
|
import ConfirmationModal from '../components/confirmation-modal';
|
2019-08-14 03:45:00 -04:00
|
|
|
import ErrorBox from '../components/error-box';
|
2019-11-05 15:13:10 -05:00
|
|
|
import { User } from '../models/user';
|
2018-09-13 12:02:27 -04:00
|
|
|
|
2019-11-05 15:13:10 -05:00
|
|
|
interface MyAccountPageProps {
|
|
|
|
user: User;
|
|
|
|
updateUser: (user: User) => void;
|
|
|
|
logout: () => void;
|
|
|
|
}
|
2019-08-09 13:49:43 -04:00
|
|
|
|
2019-11-05 15:13:10 -05:00
|
|
|
interface MyAccountPageState {
|
|
|
|
showDeleteConfirm: boolean;
|
|
|
|
error: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class MyAccountPage extends Component<MyAccountPageProps, MyAccountPageState> {
|
2018-09-09 17:22:44 -04:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2018-09-13 12:02:27 -04:00
|
|
|
this.state = {
|
2019-08-16 08:21:12 -04:00
|
|
|
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-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});
|
|
|
|
|
2020-01-02 05:59:13 -05:00
|
|
|
apiPost('/api/logout')
|
|
|
|
.then(function(res){
|
2018-09-09 17:22:44 -04:00
|
|
|
if (res.error) {
|
2019-11-07 03:13:30 -05: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});
|
|
|
|
|
2020-01-02 05:59:13 -05:00
|
|
|
apiPost('/api/api/key')
|
|
|
|
.then(res => {
|
2018-10-20 07:20:10 -04:00
|
|
|
if (res.error) {
|
2019-11-07 03:13:30 -05:00
|
|
|
this.setState({error: res.error});
|
2018-10-20 07:20:10 -04:00
|
|
|
} else {
|
|
|
|
this.props.updateUser(res);
|
|
|
|
}
|
2020-01-02 05:59:13 -05:00
|
|
|
}).catch(
|
2018-10-20 07:20:10 -04:00
|
|
|
(err) => this.setState({error: err})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-16 08:21:12 -04:00
|
|
|
confirmDelete(event: FormEvent<HTMLFormElement>) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.setState({ showDeleteConfirm: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
hideConfirmDelete() {
|
|
|
|
this.setState({ showDeleteConfirm: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleDelete() {
|
|
|
|
this.setState({ error: undefined });
|
|
|
|
|
|
|
|
try {
|
2020-01-02 05:59:13 -05:00
|
|
|
const data = await apiDelete('/api/users/me');
|
2019-08-14 03:37:27 -04:00
|
|
|
|
2019-08-16 08:21:12 -04:00
|
|
|
if(data.error) {
|
|
|
|
this.setState({ error: data.error });
|
|
|
|
} else {
|
|
|
|
this.props.logout();
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
this.setState({ error: err });
|
|
|
|
} finally {
|
|
|
|
this.hideConfirmDelete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-10-03 02:36:17 -04:00
|
|
|
Colouring London is under active development. Please <a href="https://discuss.colouring.london/">discuss
|
2018-11-21 16:08:55 -05:00
|
|
|
suggestions for improvements</a> and <a
|
2020-12-09 08:25:38 -05:00
|
|
|
href="https://github.com/colouring-london/colouring-london/issues">
|
2018-11-21 16:08:55 -05:00
|
|
|
report issues or problems</a>.
|
2018-09-13 19:19:19 -04:00
|
|
|
|
2019-10-03 02:36:17 -04:00
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
For reference, here are the <Link
|
|
|
|
to="/privacy-policy.html">privacy policy</Link>, <Link
|
|
|
|
to="/contributor-agreement.html">contributor agreement</Link> and <Link
|
|
|
|
to="/data-accuracy.html">data accuracy agreement</Link>.
|
2018-09-13 19:19:19 -04:00
|
|
|
</p>
|
2018-09-13 12:02:27 -04:00
|
|
|
<ErrorBox msg={this.state.error} />
|
2019-08-15 07:19:43 -04:00
|
|
|
<form onSubmit={this.handleLogout}>
|
2018-10-20 07:20:10 -04:00
|
|
|
<div className="buttons-container">
|
2019-09-08 20:11:45 -04:00
|
|
|
<Link to="/edit/age" 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>
|
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/>
|
|
|
|
|
2019-10-02 10:06:55 -04:00
|
|
|
<h2 className="h2">Technical details</h2>
|
2019-04-04 07:25:32 -04:00
|
|
|
<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>
|
2019-08-15 07:19:43 -04:00
|
|
|
<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-10-02 10:07:12 -04:00
|
|
|
<h3 className="h3">Open Source Code</h3>
|
2020-12-09 08:25:38 -05:00
|
|
|
Colouring London site code is developed at <a href="http://github.com/colouring-london/colouring-london/">colouring-london</a> on Github
|
2019-04-04 07:25:32 -04:00
|
|
|
|
2019-08-16 08:21:12 -04:00
|
|
|
<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"
|
2019-08-16 08:21:12 -04:00
|
|
|
onConfirm={() => this.handleDelete()}
|
|
|
|
onCancel={() => this.hideConfirmDelete()}
|
|
|
|
/>
|
|
|
|
|
2018-09-09 17:22:44 -04:00
|
|
|
</section>
|
|
|
|
</article>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<Redirect to="/login.html" />
|
2019-11-07 03:13:30 -05:00
|
|
|
);
|
2018-09-09 17:22:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MyAccountPage;
|