import React, { Component, Fragment } from 'react'; import { Link, NavLink, Redirect } from 'react-router-dom'; import queryString from 'query-string'; import ErrorBox from './error-box'; import InfoBox from './info-box'; import Sidebar from './sidebar'; import Tooltip from './tooltip'; import { CloseIcon, SaveIcon } from './icons'; import CONFIG from './fields-config.json'; const BuildingEdit = (props) => { if (!props.user){ return } if (!props.building_id){ return (
Back to maps
); } const search = (props.location && props.location.search)? queryString.parse(props.location.search): {}; return ( { CONFIG.map((conf_props) => { return }) } ); } class EditForm extends Component { constructor(props) { super(props); this.state = {} for (let field of props.fields) { this.state[field.slug] = props[field.slug] } this.state.error = this.props.error || undefined; this.state.like = this.props.like || undefined; this.handleChange = this.handleChange.bind(this); this.handleLike = this.handleLike.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleChange(event) { const target = event.target; let value = (target.value === '')? null : target.value; const name = target.name; // special transform - consider something data driven before adding 'else if's if (name === 'location_postcode' && value !== null) { value = value.toUpperCase(); } this.setState({ [name]: value }); } handleLike(event) { event.preventDefault(); fetch(`/building/like/${this.props.building_id}`, { method: 'POST', headers:{ 'Content-Type': 'application/json' }, credentials: 'same-origin' }).then( res => res.json() ).then(function(res){ if (res.error) { this.setState({error: res.error}) } else { this.props.selectBuilding(res); this.setState({ likes_total: res.likes_total }) } }.bind(this)).catch( (err) => this.setState({error: err}) ); } handleSubmit(event) { event.preventDefault(); this.setState({error: undefined}) fetch(`/building/${this.props.building_id}.json`, { method: 'POST', body: JSON.stringify(this.state), headers:{ 'Content-Type': 'application/json' }, credentials: 'same-origin' }).then( res => res.json() ).then(function(res){ if (res.error) { this.setState({error: res.error}) } else { this.props.selectBuilding(res); const new_cat = this.props.search.cat; this.props.history.push(`/building/${res.building_id}.html?cat=${new_cat}`); } }.bind(this)).catch( (err) => this.setState({error: err}) ); } render() { const match = this.props.search.cat === this.props.slug; if (!match) { return null } return (

{this.props.title}

{ this.props.fields.map((props) => { var el; switch (props.type) { case "text": el = break; case "text_list": el = break; case "number": el = break; case "like": el = break; default: el = null break; } return el }) }

Colouring may take a few seconds - try zooming the map or hitting refresh after saving (we're working on making this smoother).

{ (this.props.slug === 'like')? // special-case for likes
Done
:
Cancel
}
) } } const TextInput = (props) => ( ); const TextListInput = (props) => ( ) const NumberInput = (props) => ( ); const LikeButton = (props) => (

{(props.value)? props.value : 0} likes

); const Label = (props) => ( ) export default BuildingEdit;