import React, { Fragment } from 'react'; import { NavLink } from 'react-router-dom'; import PropTypes from 'prop-types'; import Tooltip from '../components/tooltip'; import { sanitiseURL } from '../helpers'; import BuildingNotFound from './building-not-found'; import ContainerHeader from './container-header'; import Sidebar from './sidebar'; import LocationContainer from './data-containers/location'; import UseContainer from './data-containers/use'; import TypeContainer from './data-containers/type'; import AgeContainer from './data-containers/age'; import SizeContainer from './data-containers/size'; import ConstructionContainer from './data-containers/construction'; import TeamContainer from './data-containers/team'; import SustainabilityContainer from './data-containers/sustainability'; import GreeneryContainer from './data-containers/greenery'; import CommunityContainer from './data-containers/community'; import PlanningContainer from './data-containers/planning'; import LikeContainer from './data-containers/like'; /** * Top-level container for building view/edit form * * @param props */ const BuildingView = (props) => { switch (props.cat) { case 'location': return case 'use': return case 'type': return case 'age': return case 'size': return case 'construction': return case 'team': return case 'sustainability': return case 'greenery': return case 'community': return case 'planning': return case 'like': return default: return } } /** * Shared functionality for view/edit forms * * See React Higher-order-component docs for the pattern * - https://reactjs.org/docs/higher-order-components.html * * @param WrappedComponent */ function withCopyEdit(WrappedComponent) { return class extends React.Component { // TODO: add proper types static propTypes = { // TODO: generate propTypes from TS title: PropTypes.string, slug: PropTypes.string, intro: PropTypes.string, help: PropTypes.string, inactive: PropTypes.bool, building_id: PropTypes.number, children: PropTypes.node }; constructor(props) { super(props); this.state = { copying: false, values_to_copy: {} }; this.toggleCopying = this.toggleCopying.bind(this); this.toggleCopyAttribute = this.toggleCopyAttribute.bind(this); } /** * Enter or exit "copying" state - allow user to select attributes to copy */ toggleCopying() { this.setState({ copying: !this.state.copying }) } /** * Keep track of data to copy (accumulate while in "copying" state) * * @param {string} key */ toggleCopyAttribute(key) { const value = this.props[key]; const values = this.state.values_to_copy; if(Object.keys(this.state.values_to_copy).includes(key)){ delete values[key]; } else { values[key] = value; } this.setState({ values_to_copy: values }) } render() { const data_string = JSON.stringify(this.state.values_to_copy); const copy = { copying: this.state.copying, toggleCopyAttribute: this.toggleCopyAttribute, copyingKey: (key) => Object.keys(this.state.values_to_copy).includes(key) } return this.props.building?
: } } } const DataEntry: React.FunctionComponent = (props) => { // TODO: remove any return (
{ props.title } { props.tooltip? : null } { (props.copy.copying && props.cat && props.slug && !props.disabled)?
: null }
{ (props.value != null && props.value !== '')? (typeof(props.value) === 'boolean')? (props.value)? 'Yes' : 'No' : props.value : '\u00A0'}
); } DataEntry.propTypes = { title: PropTypes.string, cat: PropTypes.string, slug: PropTypes.string, tooltip: PropTypes.string, disabled: PropTypes.bool, value: PropTypes.any } const LikeDataEntry: React.FunctionComponent = (props) => { // TODO: remove any const data_string = JSON.stringify({like: true}); return (
{ props.title } { props.tooltip? : null }
Copy
{ (props.value != null)? (props.value === 1)? `${props.value} person likes this building` : `${props.value} people like this building` : '\u00A0' }
{ (props.user_building_like)?
…including you!
: '' }
); } LikeDataEntry.propTypes = { title: PropTypes.string, cat: PropTypes.string, tooltip: PropTypes.string, value: PropTypes.any, user_building_like: PropTypes.bool } const MultiDataEntry: React.FunctionComponent = (props) => { // TODO: remove any let content; if (props.value && props.value.length) { content =
    { props.value.map((item, index) => { return
  • {item}
  • }) }
} else { content = '\u00A0' } return (
{ props.title } { props.tooltip? : null } { (props.copying && props.cat && props.slug && !props.disabled)?
: null }
{ content }
); } MultiDataEntry.propTypes = { title: PropTypes.string, tooltip: PropTypes.string, value: PropTypes.arrayOf(PropTypes.string) } const UPRNsDataEntry = (props) => { const uprns = props.value || []; const noParent = uprns.filter(uprn => uprn.parent_uprn == null); const withParent = uprns.filter(uprn => uprn.parent_uprn != null); return (
{ props.title } { props.tooltip? : null }
    { noParent.length? noParent.map(uprn => (
  • {uprn.uprn}
  • )) : '\u00A0' }
    { withParent.length?
    Children { withParent.map(uprn => (
  • {uprn.uprn} (child of {uprn.parent_uprn})
  • )) }
    : null }
) } UPRNsDataEntry.propTypes = { title: PropTypes.string, tooltip: PropTypes.string, value: PropTypes.arrayOf(PropTypes.shape({ uprn: PropTypes.string.isRequired, parent_uprn: PropTypes.string })) } export default BuildingView; export { withCopyEdit };