From 30e4cf29121f8a018dc188f39c34b811d71fb58a Mon Sep 17 00:00:00 2001 From: Tom Russell Date: Tue, 11 Sep 2018 20:59:59 +0100 Subject: [PATCH] Data-driven building edit and view --- app/src/frontend/building-edit.js | 295 ++++++++++++++++++++++-------- app/src/frontend/building-view.js | 109 +++++------ 2 files changed, 264 insertions(+), 140 deletions(-) diff --git a/app/src/frontend/building-edit.js b/app/src/frontend/building-edit.js index c2cb5b48..9394dc56 100644 --- a/app/src/frontend/building-edit.js +++ b/app/src/frontend/building-edit.js @@ -1,80 +1,223 @@ -import React from 'react'; +import React, { Component } from 'react'; import { Link } from 'react-router-dom'; +import Sidebar from './sidebar'; -const BuildingEdit = (props) => ( -
-

Edit building data

-
-
- Location -
- - - - - - - - - - - - -
-
-
- Age -
- - - - - - - - - - -
-
-
- Size -
- - - - - - -
-
-
- Like Me! -
- -
- -
-
-
-
- Cancel - -
-
-
-); +class BuildingEdit extends Component { + constructor(props) { + super(props); + this.state = { + 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 || [], + liked: this.user_likes(props.user_id, props.likes) + }; + + 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({ + likes: likes.concat([this.props.user_id]), + liked: true + }); + } else { + this.setState({ + likes: likes.filter(id => id !== this.props.user_id), + liked: false + }); + } + } + + handleSubmit(event) { + event.preventDefault(); + fetch(`/building/${this.props.id}`, { + method: 'POST', + body: JSON.stringify(this.state), + headers:{ + 'Content-Type': 'application/json' + } + }).then( + res => res.json() + ).then(function(res){ + if (res.error) { + console.error(res.error); // tell user + } else { + console.log(res); // redirect back + } + }).catch( + err => console.error(err) + ); + } + + render() { + return ( + +
+
+ Location +
+ + + + + + + + + + + + + + + + + + + +
+
+
+ Age +
+ + + + + + + + + + + + + + + +
+
+
+ Size +
+ + + + + + + + + +
+
+
+ Like Me! +
+ +
+ +
+
+
+
+ Cancel + +
+
+
+ ); + } +} export default BuildingEdit; diff --git a/app/src/frontend/building-view.js b/app/src/frontend/building-view.js index e947b3aa..fceac3a9 100644 --- a/app/src/frontend/building-view.js +++ b/app/src/frontend/building-view.js @@ -1,15 +1,13 @@ import React from 'react'; import { Link } from 'react-router-dom'; +import Sidebar from './sidebar'; +import Tooltip from './tooltip'; + const BuildingView = (props) => ( -
-

Building data

+
-

- Location -

+

Location

Section introduction of up to roughly 100 characters will take @@ -18,78 +16,61 @@ const BuildingView = (props) => ( Read more.

-
- Building Name - - - ? -
-
-
- - Hint tooltip content should be ~40 chars. - -
-
-
-
-
no data
-
Building Number
-
no data
-
Street
-
no data
-
Address line 2
-
no data
-
Town
-
no data
-
Postcode
-
no data
+
+ Building Name + +
+
{props.location_name ? props.location_name : '-'}
+
Building Number
+
{props.location_number ? props.location_number : '-'}
+
Street
+
{props.location_street ? props.location_street : '-'}
+
Address line 2
+
{props.location_line_two ? props.location_line_two : '-'}
+
Town
+
{props.location_town ? props.location_town : '-'}
+
Postcode
+
{props.location_postcode ? props.location_postcode : '-'}
-

Age

-
-
Year built (best estimate)
-
2018
-
Year built (lower estimate)
-
2018
-
Year built (upper estimate)
-
2018
-
Date Source
-
Pevsner
-
Facade date
-
2018
+

Age

+
+
Year built (best estimate)
+
{props.date_year? props.date_year : '-'}
+
Year built (lower estimate)
+
{props.date_lower? props.date_lower : '-'}
+
Year built (upper estimate)
+
{props.date_upper? props.date_upper : '-'}
+
Date Source
+
{props.date_source? props.date_source : '-'}
+
Facade date
+
{props.date_facade? props.date_facade : '-'}
-

Size

-
-
Attic storeys
-
0
-
Core storeys
-
3
-
Basement storeys
-
1
+

Size

+
+
Attic storeys
+
{props.size_attic? props.size_attic : '-'}
+
Core storeys
+
{props.size_core? props.size_core : '-'}
+
Basement storeys
+
{props.size_basement? props.size_basement : '-'}
-

Like Me!

-
-
Likes
-
25
+

Like Me!

+
+
Likes
+
{props.likes ? props.likes.length : 0}
Back to maps Edit data
-
+ ); export default BuildingView;