import React, { Component, Fragment } from 'react'; import { Link, NavLink, Redirect } from 'react-router-dom'; import ErrorBox from './error-box'; import InfoBox from './info-box'; import Sidebar from './sidebar'; import Tooltip from './tooltip'; import { SaveIcon } from './icons'; import { parseCategoryURL } from '../parse'; import CONFIG from './fields-config.json'; const BuildingEdit = (props) => { if (!props.user){ return } const cat = parseCategoryURL(props.match.url); if (!props.building_id){ return (
Back to maps
); } 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); } }.bind(this)).catch( (err) => this.setState({error: err}) ); } render() { const match = this.props.cat === this.props.slug; return (
match}>

{this.props.title}

{ match? (
{ 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 null :
} ) : null }
) } } const TextInput = (props) => ( ); const TextListInput = (props) => ( ) const NumberInput = (props) => ( ); const LikeButton = (props) => (

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

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