Handle error on logout

This commit is contained in:
Tom Russell 2018-09-13 17:02:27 +01:00
parent 1ea6b0b75b
commit bdb4e45d8b
3 changed files with 16 additions and 8 deletions

View File

@ -29,6 +29,8 @@ class Login extends Component {
handleSubmit(event) { handleSubmit(event) {
event.preventDefault(); event.preventDefault();
this.setState({error: undefined})
fetch('/login', { fetch('/login', {
method: 'POST', method: 'POST',
body: JSON.stringify(this.state), body: JSON.stringify(this.state),
@ -41,7 +43,6 @@ class Login extends Component {
if (res.error) { if (res.error) {
this.setState({error: res.error}) this.setState({error: res.error})
} else { } else {
this.setState({error: undefined})
fetch('/users/me').then( fetch('/users/me').then(
(res) => res.json() (res) => res.json()
).then( ).then(

View File

@ -1,28 +1,33 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { Redirect } from 'react-router'; import { Redirect } from 'react-router';
import ErrorBox from './error-box';
class MyAccountPage extends Component { class MyAccountPage extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = {
error: undefined
};
this.handleSubmit = this.handleSubmit.bind(this); this.handleSubmit = this.handleSubmit.bind(this);
} }
handleSubmit(event) { handleSubmit(event) {
const logout = this.props.logout;
event.preventDefault(); event.preventDefault();
this.setState({error: undefined});
fetch('/logout', { fetch('/logout', {
method: 'POST' method: 'POST'
}).then( }).then(
res => res.json() res => res.json()
).then(function(res){ ).then(function(res){
if (res.error) { if (res.error) {
console.error(res.error); // tell user this.setState({error: res.error})
} else { } else {
logout(); this.props.logout();
console.log(res); // redirect back
} }
}).catch( }.bind(this)).catch(
err => console.error(err) (err) => this.setState({error: err})
); );
} }
@ -32,6 +37,7 @@ class MyAccountPage extends Component {
<article> <article>
<section className="main-col"> <section className="main-col">
<h1 className="h3">Welcome, {this.props.user.username}</h1> <h1 className="h3">Welcome, {this.props.user.username}</h1>
<ErrorBox msg={this.state.error} />
<form method="POST" action="/logout" onSubmit={this.handleSubmit}> <form method="POST" action="/logout" onSubmit={this.handleSubmit}>
<input className="btn btn-secondary" type="submit" value="Log out"/> <input className="btn btn-secondary" type="submit" value="Log out"/>
</form> </form>

View File

@ -32,6 +32,8 @@ class SignUp extends Component {
handleSubmit(event) { handleSubmit(event) {
event.preventDefault(); event.preventDefault();
this.setState({error: undefined})
fetch('/users', { fetch('/users', {
method: 'POST', method: 'POST',
body: JSON.stringify(this.state), body: JSON.stringify(this.state),
@ -44,7 +46,6 @@ class SignUp extends Component {
if (res.error) { if (res.error) {
this.setState({error: res.error}) this.setState({error: res.error})
} else { } else {
this.setState({error: undefined})
fetch('/users/me').then( fetch('/users/me').then(
(res) => res.json() (res) => res.json()
).then( ).then(