import React, { Component, FormEvent } from 'react'; import { Link, Redirect } from 'react-router-dom'; import PropTypes from 'prop-types'; import ErrorBox from './error-box'; import ConfirmationModal from './ConfirmationModal'; class MyAccountPage extends Component { // 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); this.state = { error: undefined, showDeleteConfirm: false }; this.handleLogout = this.handleLogout.bind(this); this.handleGenerateKey = this.handleGenerateKey.bind(this); } handleLogout(event) { event.preventDefault(); this.setState({error: undefined}); fetch('/api/logout', { method: 'POST', credentials: 'same-origin' }).then( res => res.json() ).then(function(res){ if (res.error) { this.setState({error: res.error}) } else { this.props.logout(); } }.bind(this)).catch( (err) => this.setState({error: err}) ); } handleGenerateKey(event) { event.preventDefault(); this.setState({error: undefined}); fetch('/api/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}) ); } confirmDelete(event: FormEvent) { 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 (

Welcome, {this.props.user.username}!

Colouring London is under active development, Please discuss suggestions for improvements and report issues or problems.

Start colouring

My Details

Username

{this.props.user.username}

Email Address

{this.props.user.email? this.props.user.email : '-'}

Registered

{this.props.user.registered.toString()}


Techical details

Are you a software developer? If so, you might be interested in these.

API key

{this.props.user.api_key? this.props.user.api_key : '-'}

GitHub

Colouring London Github repository

Account actions

this.confirmDelete(e)} className="form-group mb-3" >
this.handleDelete()} onCancel={() => this.hideConfirmDelete()} />
); } else { return ( ) } } } export default MyAccountPage;