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

140 lines
4.8 KiB
JavaScript
Raw Normal View History

import React, { Fragment } from 'react';
import { Link, NavLink } from 'react-router-dom';
2018-09-11 15:59:59 -04:00
import Sidebar from './sidebar';
import Tooltip from './tooltip';
2018-09-13 15:41:42 -04:00
import InfoBox from './info-box';
2018-11-13 05:45:35 -05:00
import { EditIcon } from './icons';
2018-11-30 04:26:28 -05:00
import { parseCategoryURL } from '../parse';
2018-09-11 15:59:59 -04:00
import CONFIG from './fields-config.json';
const BuildingView = (props) => {
if (!props.building_id){
return (
<Sidebar title="Building Not Found">
<InfoBox msg="We can't find that one anywhere - try the map again?" />
<div className="buttons-container with-space">
2018-11-29 17:00:53 -05:00
<Link to="/view/age.html" className="btn btn-secondary">Back to maps</Link>
</div>
</Sidebar>
);
}
2018-11-30 04:26:28 -05:00
const cat = parseCategoryURL(props.match.url);
return (
2018-11-29 17:00:53 -05:00
<Sidebar title={`Data available for this building`} back={`/view/${cat}.html`}>
{
CONFIG.map(section_props => (
<DataSection
2018-11-29 17:00:53 -05:00
key={section_props.slug} cat={cat}
building_id={props.building_id}
2018-10-04 17:50:33 -04:00
{...section_props}>
{
section_props.fields.map(field_props => {
return (field_props.slug === 'uprns')?
<UPRNsDataEntry
key={field_props.slug}
title={field_props.title}
value={props.uprns}
tooltip={field_props.tooltip} />
:
<DataEntry
2018-10-04 17:50:33 -04:00
key={field_props.slug}
title={field_props.title}
value={props[field_props.slug]}
tooltip={field_props.tooltip} />
})
}
</DataSection>
))
}
</Sidebar>
);
}
const DataSection = (props) => {
2018-11-29 17:00:53 -05:00
const match = props.cat === props.slug;
return (
<section id={props.slug} className={(props.inactive)? "data-section inactive": "data-section"}>
<header className={(match? "active " : "") + " section-header view"}>
2018-10-04 17:50:33 -04:00
<NavLink
2018-11-29 17:00:53 -05:00
to={`/view/${props.slug}/building/${props.building_id}.html`}
title={(props.inactive)? 'Coming soon… Click the ? for more info.' :
(match)? 'Hide details' : 'Show details'}
2018-10-04 17:50:33 -04:00
isActive={() => match}>
2018-10-01 11:59:39 -04:00
<h3 className="h3">{props.title}</h3>
</NavLink>
2018-10-05 04:10:20 -04:00
<nav className="icon-buttons">
2018-10-01 11:59:39 -04:00
{
2018-10-04 17:50:33 -04:00
props.help?
<a className="icon-button help" title="Find out more" href={props.help}>
2018-11-13 05:44:12 -05:00
More info
2018-10-01 11:59:39 -04:00
</a>
: null
}
{
!props.inactive?
2018-10-04 17:50:33 -04:00
<NavLink className="icon-button edit" title="Edit data"
2018-11-29 17:00:53 -05:00
to={`/edit/${props.slug}/building/${props.building_id}.html`}>
Edit
2018-10-01 11:59:39 -04:00
<EditIcon />
</NavLink>
: null
}
2018-10-05 04:10:20 -04:00
</nav>
2018-10-01 11:59:39 -04:00
</header>
{ match? <dl className="data-list">{props.children}</dl> : null }
</section>
);
}
const DataEntry = (props) => (
<Fragment>
<dt>
{ props.title }
{ props.tooltip? <Tooltip text={ props.tooltip } /> : null }
</dt>
2018-10-25 08:49:24 -04:00
<dd>{(props.value != null)? props.value : '\u00A0'}</dd>
</Fragment>
);
const UPRNsDataEntry = (props) => {
const uprns = props.value || [];
const no_parent = uprns.filter(uprn => uprn.parent_uprn == null);
const with_parent = uprns.filter(uprn => uprn.parent_uprn != null);
return (
<Fragment>
<dt>
{ props.title }
{ props.tooltip? <Tooltip text={ props.tooltip } /> : null }
</dt>
<dd><ul className="uprn-list">
<Fragment>{
no_parent.length?
no_parent.map(uprn => (
<li key={uprn.uprn}>{uprn.uprn}</li>
))
: '\u00A0'
}</Fragment>
{
with_parent.length?
<details>
<summary>Children</summary>
{
with_parent.map(uprn => (
<li key={uprn.uprn}>{uprn.uprn} (child of {uprn.parent_uprn})</li>
))
}
</details>
: null
}
</ul></dd>
</Fragment>
)
}
export default BuildingView;