Handle error on building update

This commit is contained in:
Tom Russell 2018-09-13 17:13:03 +01:00
parent bdb4e45d8b
commit 0449170b76

View File

@ -1,5 +1,7 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import ErrorBox from './error-box';
import Sidebar from './sidebar'; import Sidebar from './sidebar';
class BuildingEdit extends Component { class BuildingEdit extends Component {
@ -7,6 +9,7 @@ class BuildingEdit extends Component {
super(props); super(props);
const user = props.user || {}; const user = props.user || {};
this.state = { this.state = {
error: undefined,
location_name: props.location_name, location_name: props.location_name,
location_number: props.location_number, location_number: props.location_number,
location_line_two: props.location_line_two, location_line_two: props.location_line_two,
@ -61,6 +64,8 @@ class BuildingEdit extends Component {
handleSubmit(event) { handleSubmit(event) {
event.preventDefault(); event.preventDefault();
this.setState({error: undefined})
fetch(`/building/${this.props.id}.json`, { fetch(`/building/${this.props.id}.json`, {
method: 'POST', method: 'POST',
body: JSON.stringify(this.state), body: JSON.stringify(this.state),
@ -71,12 +76,12 @@ class BuildingEdit extends Component {
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 {
this.props.selectBuilding(this.state); this.props.selectBuilding(this.state);
} }
}).catch( }.bind(this)).catch(
err => console.error(err) (err) => this.setState({error: err})
); );
} }
@ -84,6 +89,9 @@ class BuildingEdit extends Component {
return ( return (
<Sidebar title={`Building ${this.props.id}`}> <Sidebar title={`Building ${this.props.id}`}>
<form action="building-view.html" method="GET" onSubmit={this.handleSubmit}> <form action="building-view.html" method="GET" onSubmit={this.handleSubmit}>
<ErrorBox msg={this.state.error} />
<fieldset className="data-section"> <fieldset className="data-section">
<legend className="h3 bullet-prefix location toggled-on">Location</legend> <legend className="h3 bullet-prefix location toggled-on">Location</legend>
<div id="data-list-location" className="data-list"> <div id="data-list-location" className="data-list">