colouring-montreal/app/src/frontend/building-edit.js

250 lines
11 KiB
JavaScript
Raw Normal View History

2018-09-11 15:59:59 -04:00
import React, { Component } from 'react';
2018-09-13 17:19:55 -04:00
import { Link, Redirect } from 'react-router-dom';
2018-09-13 12:13:03 -04:00
import ErrorBox from './error-box';
2018-09-13 15:41:42 -04:00
import InfoBox from './info-box';
2018-09-11 15:59:59 -04:00
import Sidebar from './sidebar';
2018-09-11 15:59:59 -04:00
class BuildingEdit extends Component {
constructor(props) {
super(props);
2018-09-11 18:30:17 -04:00
const user = props.user || {};
2018-09-11 15:59:59 -04:00
this.state = {
2018-09-13 12:13:03 -04:00
error: undefined,
2018-09-13 18:55:53 -04:00
id: props.id,
geometry_id: props.geometry_id,
2018-09-11 15:59:59 -04:00
location_name: props.location_name,
location_number: props.location_number,
location_line_two: props.location_line_two,
location_street: props.location_street,
location_postcode: props.location_postcode,
date_year: props.date_year,
date_lower: props.date_lower,
date_upper: props.date_upper,
date_source: props.date_source,
date_facade: props.date_facade,
size_attic: props.size_attic,
size_core: props.size_core,
size_basement: props.size_basement,
likes: props.likes || [],
2018-09-11 18:30:17 -04:00
liked: this.user_likes(user.id, props.likes)
2018-09-11 15:59:59 -04:00
};
this.handleChange = this.handleChange.bind(this);
this.handleLike = this.handleLike.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
user_likes(user_id, likes) {
return likes && likes.indexOf(user_id) !== -1;
}
handleChange(event) {
const target = event.target;
const value = target.value;
const name = target.name;
this.setState({
[name]: value
});
}
handleLike(event) {
const liked = event.target.checked;
const likes = this.state.likes || [];
if (liked) {
this.setState({
2018-09-11 18:30:17 -04:00
likes: likes.concat([this.props.user.id]),
2018-09-11 15:59:59 -04:00
liked: true
});
} else {
this.setState({
2018-09-11 18:30:17 -04:00
likes: likes.filter(id => id !== this.props.user.id),
2018-09-11 15:59:59 -04:00
liked: false
});
}
}
handleSubmit(event) {
event.preventDefault();
2018-09-13 12:13:03 -04:00
this.setState({error: undefined})
2018-09-11 18:30:17 -04:00
fetch(`/building/${this.props.id}.json`, {
2018-09-11 15:59:59 -04:00
method: 'POST',
body: JSON.stringify(this.state),
headers:{
'Content-Type': 'application/json'
}
}).then(
res => res.json()
).then(function(res){
if (res.error) {
2018-09-13 12:13:03 -04:00
this.setState({error: res.error})
2018-09-11 15:59:59 -04:00
} else {
2018-09-11 18:30:17 -04:00
this.props.selectBuilding(this.state);
2018-09-13 18:55:53 -04:00
this.props.history.push(`/building/${this.props.id}.html`)
2018-09-11 15:59:59 -04:00
}
2018-09-13 12:13:03 -04:00
}.bind(this)).catch(
(err) => this.setState({error: err})
2018-09-11 15:59:59 -04:00
);
}
render() {
2018-09-13 17:19:55 -04:00
if (!this.props.user){
return <Redirect to="/sign-up.html" />
}
2018-09-13 15:41:42 -04:00
if (!this.props.id){
return (
<Sidebar title="Building Not Found">
<InfoBox msg="We can't find that one anywhere - try the map again?" />
<div className="buttons-container">
<Link to="/map/date_year.html" className="btn btn-secondary">Back to maps</Link>
</div>
</Sidebar>
);
}
2018-09-11 15:59:59 -04:00
return (
2018-09-11 18:30:17 -04:00
<Sidebar title={`Building ${this.props.id}`}>
2018-09-11 15:59:59 -04:00
<form action="building-view.html" method="GET" onSubmit={this.handleSubmit}>
2018-09-13 12:13:03 -04:00
<ErrorBox msg={this.state.error} />
2018-09-11 15:59:59 -04:00
<fieldset className="data-section">
<legend className="h3 bullet-prefix location toggled-on">Location</legend>
<div id="data-list-location" className="data-list">
<label htmlFor="location_name">Building name</label>
<input className="form-control" type="text"
id="location_name" name="location_name"
value={this.state.location_name}
onChange={this.handleChange}
/>
<label htmlFor="location_number">Building number</label>
<input className="form-control" type="text"
id="location_number" name="location_number"
value={this.state.location_number}
onChange={this.handleChange}
/>
<label htmlFor="location_street">Street</label>
<input className="form-control" type="text"
id="location_street" name="location_street"
value={this.state.location_street}
onChange={this.handleChange}
/>
<label htmlFor="location_line_two">Address line 2</label>
<input className="form-control" type="text"
id="location_line_two" name="location_line_two"
value={this.state.location_line_two}
onChange={this.handleChange}
/>
<label htmlFor="location_town">Town</label>
<input className="form-control" type="text"
id="location_town" name="location_town"
value={this.state.location_town}
onChange={this.handleChange}
/>
<label htmlFor="location_postcode">Postcode</label>
<input className="form-control" type="text"
id="location_postcode" name="location_postcode"
value={this.state.location_postcode}
onChange={this.handleChange}
/>
</div>
</fieldset>
<fieldset className="data-section">
<legend className="h3 bullet-prefix age">Age</legend>
<div id="data-list-age" className="data-list">
<label htmlFor="date_year">Year built (best estimate)</label>
<input className="form-control" type="number" step="1"
id="date_year" name="date_year"
value={this.state.date_year}
onChange={this.handleChange}
/>
<label htmlFor="date_upper">Year built (upper estimate)</label>
<input className="form-control" type="number" step="1"
id="date_upper" name="date_upper"
value={this.state.date_upper}
onChange={this.handleChange}
/>
<label htmlFor="date_lower">Year built (lower estimate)</label>
<input className="form-control" type="number" step="1"
id="date_lower" name="date_lower"
value={this.state.date_lower}
onChange={this.handleChange}
/>
<label htmlFor="date_facade">Facade date</label>
<input className="form-control" type="number" step="1"
id="date_facade" name="date_facade"
value={this.state.date_facade}
onChange={this.handleChange}
/>
<label htmlFor="date_source">Source</label>
<input className="form-control" type="text"
id="date_source" name="date_source"
value={this.state.date_source}
onChange={this.handleChange}
/>
</div>
</fieldset>
<fieldset className="data-section">
<legend className="h3 bullet-prefix size">Size</legend>
<div id="data-list-size" className="data-list">
<label htmlFor="size_attic">Attic storeys</label>
<input className="form-control" type="number" step="1"
id="size_attic" name="size_attic"
value={this.state.size_attic}
onChange={this.handleChange}
/>
<label htmlFor="size_core">Core storeys</label>
<input className="form-control" type="number" step="1"
id="size_core" name="size_core"
value={this.state.size_core}
onChange={this.handleChange}
/>
<label htmlFor="size_basement">Basement storeys</label>
<input className="form-control" type="number" step="1"
id="size_basement" name="size_basement"
value={this.state.size_basement}
onChange={this.handleChange}
/>
</div>
</fieldset>
<fieldset className="data-section">
<legend className="h3 bullet-prefix like">Like Me!</legend>
<div id="data-list-like" className="data-list">
<label htmlFor="likes">Like this building?</label>
<div className="form-check">
<input className="form-check-input position-static"
type="checkbox"
checked={this.state.liked}
onChange={this.handleLike}
/>
</div>
</div>
</fieldset>
<div className="buttons-container">
<Link to={`/building/${this.props.id}.html`} className="btn btn-secondary">Cancel</Link>
<button type="submit" className="btn btn-primary">Save</button>
</div>
</form>
</Sidebar>
);
}
}
export default BuildingEdit;