2018-10-01 07:45:33 -04:00
|
|
|
import React, { Fragment } from 'react';
|
2018-09-30 19:13:01 -04:00
|
|
|
import { Link, NavLink } from 'react-router-dom';
|
2019-05-27 13:26:29 -04:00
|
|
|
import PropTypes from 'prop-types';
|
2018-09-09 17:22:44 -04:00
|
|
|
|
2019-08-14 11:03:47 -04:00
|
|
|
import BuildingNotFound from './building-not-found';
|
2018-09-11 15:59:59 -04:00
|
|
|
import Sidebar from './sidebar';
|
2019-08-14 04:21:42 -04:00
|
|
|
import Tooltip from '../components/tooltip';
|
2019-08-14 06:36:38 -04:00
|
|
|
import { BackIcon, EditIcon } from '../components/icons';
|
2019-08-14 04:21:42 -04:00
|
|
|
import { sanitiseURL } from '../helpers';
|
2018-09-11 15:59:59 -04:00
|
|
|
|
2018-10-03 16:47:49 -04:00
|
|
|
import CONFIG from './fields-config.json';
|
2018-09-30 19:32:24 -04:00
|
|
|
|
2018-10-03 16:47:49 -04:00
|
|
|
const BuildingView = (props) => {
|
2019-08-14 06:36:38 -04:00
|
|
|
const cat = props.match.params.cat;
|
|
|
|
const sections = CONFIG.filter((d) => d.slug === cat)
|
|
|
|
|
|
|
|
if (!props.building_id || sections.length !== 1){
|
2019-08-14 11:03:47 -04:00
|
|
|
return (<BuildingNotFound mode="view" />);
|
2018-10-03 16:47:49 -04:00
|
|
|
}
|
2019-08-14 06:36:38 -04:00
|
|
|
|
|
|
|
const section = sections[0];
|
2018-10-03 16:47:49 -04:00
|
|
|
return (
|
2019-08-14 06:36:38 -04:00
|
|
|
<Sidebar>
|
|
|
|
<DataSection
|
|
|
|
cat={cat}
|
|
|
|
{...section}
|
|
|
|
{...props}
|
|
|
|
/>
|
2018-10-03 16:47:49 -04:00
|
|
|
</Sidebar>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:26:29 -04:00
|
|
|
BuildingView.propTypes = {
|
2019-05-27 15:28:28 -04:00
|
|
|
building_id: PropTypes.number,
|
2019-05-27 13:26:29 -04:00
|
|
|
match: PropTypes.object,
|
|
|
|
uprns: PropTypes.arrayOf(PropTypes.shape({
|
|
|
|
uprn: PropTypes.string.isRequired,
|
|
|
|
parent_uprn: PropTypes.string
|
|
|
|
})),
|
|
|
|
building_like: PropTypes.bool
|
|
|
|
}
|
2018-10-03 16:47:49 -04:00
|
|
|
|
2019-08-09 13:49:43 -04:00
|
|
|
class DataSection extends React.Component<any, any> { // TODO: add proper types
|
|
|
|
static propTypes = { // TODO: generate propTypes from TS
|
|
|
|
title: PropTypes.string,
|
|
|
|
cat: PropTypes.string,
|
|
|
|
slug: PropTypes.string,
|
|
|
|
intro: PropTypes.string,
|
|
|
|
help: PropTypes.string,
|
|
|
|
inactive: PropTypes.bool,
|
|
|
|
building_id: PropTypes.number,
|
|
|
|
children: PropTypes.node
|
|
|
|
};
|
|
|
|
|
2019-08-01 06:19:04 -04:00
|
|
|
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 props = this.props;
|
|
|
|
const match = props.cat === props.slug;
|
|
|
|
const data_string = JSON.stringify(this.state.values_to_copy);
|
|
|
|
return (
|
2019-08-14 08:32:08 -04:00
|
|
|
<section id={props.slug} className="data-section">
|
|
|
|
<header className={`section-header view ${props.slug} background-${props.slug}`}>
|
2019-08-14 06:36:38 -04:00
|
|
|
<Link className="icon-button back" to="/view/categories.html">
|
|
|
|
<BackIcon />
|
|
|
|
</Link>
|
2019-08-14 08:32:08 -04:00
|
|
|
<h2 className="h2">{props.title}</h2>
|
2019-08-01 06:19:04 -04:00
|
|
|
<nav className="icon-buttons">
|
|
|
|
{
|
|
|
|
(match && !props.inactive)?
|
|
|
|
this.state.copying?
|
|
|
|
<Fragment>
|
|
|
|
<NavLink
|
|
|
|
to={`/multi-edit/${props.cat}.html?data=${data_string}`}
|
|
|
|
className="icon-button copy">
|
|
|
|
Copy selected
|
|
|
|
</NavLink>
|
|
|
|
<a className="icon-button copy" onClick={this.toggleCopying}>Cancel</a>
|
|
|
|
</Fragment>
|
|
|
|
:
|
|
|
|
<a className="icon-button copy" onClick={this.toggleCopying}>Copy</a>
|
2019-05-27 11:39:16 -04:00
|
|
|
: null
|
2019-08-01 06:19:04 -04:00
|
|
|
}
|
|
|
|
{
|
|
|
|
props.help && !this.state.copying?
|
|
|
|
<a className="icon-button help" title="Find out more" href={props.help}>
|
|
|
|
Info
|
|
|
|
</a>
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
{
|
|
|
|
!props.inactive && !this.state.copying?
|
|
|
|
<NavLink className="icon-button edit" title="Edit data"
|
|
|
|
to={`/edit/${props.slug}/building/${props.building_id}.html`}>
|
|
|
|
Edit
|
|
|
|
<EditIcon />
|
|
|
|
</NavLink>
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
</nav>
|
|
|
|
</header>
|
|
|
|
{
|
|
|
|
match?
|
2019-05-27 11:39:16 -04:00
|
|
|
!props.inactive?
|
2019-08-01 06:19:04 -04:00
|
|
|
<dl className="data-list">
|
|
|
|
{
|
|
|
|
props.fields.map(field => {
|
|
|
|
|
|
|
|
switch (field.type) {
|
|
|
|
case 'uprn_list':
|
|
|
|
return <UPRNsDataEntry
|
|
|
|
key={field.slug}
|
|
|
|
title={field.title}
|
|
|
|
value={props.uprns}
|
|
|
|
tooltip={field.tooltip} />
|
|
|
|
case 'text_multi':
|
|
|
|
return <MultiDataEntry
|
|
|
|
key={field.slug}
|
2019-08-06 17:11:17 -04:00
|
|
|
slug={field.slug}
|
|
|
|
disabled={field.disabled}
|
|
|
|
cat={props.cat}
|
2019-08-01 06:19:04 -04:00
|
|
|
title={field.title}
|
|
|
|
value={props[field.slug]}
|
2019-08-06 17:11:17 -04:00
|
|
|
|
|
|
|
copying={this.state.copying}
|
|
|
|
toggleCopyAttribute={this.toggleCopyAttribute}
|
|
|
|
copy={Object.keys(this.state.values_to_copy).includes(field.slug)}
|
|
|
|
|
2019-08-01 06:19:04 -04:00
|
|
|
tooltip={field.tooltip} />
|
|
|
|
case 'like':
|
|
|
|
return <LikeDataEntry
|
|
|
|
key={field.slug}
|
|
|
|
title={field.title}
|
|
|
|
value={props[field.slug]}
|
|
|
|
user_building_like={props.building_like}
|
|
|
|
tooltip={field.tooltip} />
|
|
|
|
default:
|
|
|
|
return <DataEntry
|
|
|
|
key={field.slug}
|
|
|
|
slug={field.slug}
|
|
|
|
disabled={field.disabled}
|
|
|
|
cat={props.cat}
|
|
|
|
title={field.title}
|
|
|
|
value={props[field.slug]}
|
|
|
|
|
|
|
|
copying={this.state.copying}
|
|
|
|
toggleCopyAttribute={this.toggleCopyAttribute}
|
|
|
|
copy={Object.keys(this.state.values_to_copy).includes(field.slug)}
|
|
|
|
|
|
|
|
tooltip={field.tooltip} />
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
</dl>
|
|
|
|
: <p className="data-intro">{props.intro}</p>
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
</section>
|
|
|
|
);
|
|
|
|
}
|
2018-09-30 19:32:24 -04:00
|
|
|
}
|
|
|
|
|
2019-08-14 03:28:15 -04:00
|
|
|
const DataEntry: React.FunctionComponent<any> = (props) => { // TODO: remove any
|
2019-08-01 05:29:32 -04:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<dt>
|
|
|
|
{ props.title }
|
|
|
|
{ props.tooltip? <Tooltip text={ props.tooltip } /> : null }
|
2019-08-01 06:19:04 -04:00
|
|
|
{ (props.copying && props.cat && props.slug && !props.disabled)?
|
2019-08-01 05:29:32 -04:00
|
|
|
<div className="icon-buttons">
|
2019-08-01 06:19:04 -04:00
|
|
|
<label className="icon-button copy">
|
2019-08-01 05:29:32 -04:00
|
|
|
Copy
|
2019-08-01 06:19:04 -04:00
|
|
|
<input type="checkbox" checked={props.copy}
|
|
|
|
onChange={() => props.toggleCopyAttribute(props.slug)}/>
|
|
|
|
</label>
|
2019-08-01 05:29:32 -04:00
|
|
|
</div>
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
</dt>
|
|
|
|
<dd>{
|
|
|
|
(props.value != null && props.value !== '')?
|
|
|
|
(typeof(props.value) === 'boolean')?
|
|
|
|
(props.value)? 'Yes' : 'No'
|
|
|
|
: props.value
|
|
|
|
: '\u00A0'}</dd>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
2018-09-09 17:22:44 -04:00
|
|
|
|
2019-05-27 13:26:29 -04:00
|
|
|
DataEntry.propTypes = {
|
|
|
|
title: PropTypes.string,
|
2019-05-27 15:13:43 -04:00
|
|
|
cat: PropTypes.string,
|
|
|
|
slug: PropTypes.string,
|
2019-05-27 13:26:29 -04:00
|
|
|
tooltip: PropTypes.string,
|
2019-05-27 15:54:43 -04:00
|
|
|
disabled: PropTypes.bool,
|
2019-05-27 13:26:29 -04:00
|
|
|
value: PropTypes.any
|
|
|
|
}
|
|
|
|
|
2019-08-14 03:28:15 -04:00
|
|
|
const LikeDataEntry: React.FunctionComponent<any> = (props) => { // TODO: remove any
|
2019-08-01 05:29:32 -04:00
|
|
|
const data_string = JSON.stringify({like: true});
|
2019-08-09 13:49:43 -04:00
|
|
|
return (
|
2019-08-01 05:29:32 -04:00
|
|
|
<Fragment>
|
|
|
|
<dt>
|
|
|
|
{ props.title }
|
|
|
|
{ props.tooltip? <Tooltip text={ props.tooltip } /> : null }
|
|
|
|
<div className="icon-buttons">
|
|
|
|
<NavLink
|
|
|
|
to={`/multi-edit/${props.cat}.html?data=${data_string}`}
|
|
|
|
className="icon-button copy">
|
|
|
|
Copy
|
|
|
|
</NavLink>
|
|
|
|
</div>
|
|
|
|
</dt>
|
|
|
|
<dd>
|
|
|
|
{
|
|
|
|
(props.value != null)?
|
|
|
|
(props.value === 1)?
|
|
|
|
`${props.value} person likes this building`
|
|
|
|
: `${props.value} people like this building`
|
|
|
|
: '\u00A0'
|
|
|
|
}
|
|
|
|
</dd>
|
2019-05-27 11:39:16 -04:00
|
|
|
{
|
2019-08-12 16:59:24 -04:00
|
|
|
(props.user_building_like)? <dd>…including you!</dd> : ''
|
2019-05-27 11:39:16 -04:00
|
|
|
}
|
2019-08-01 05:29:32 -04:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
2019-01-22 12:21:44 -05:00
|
|
|
|
2019-05-27 13:26:29 -04:00
|
|
|
LikeDataEntry.propTypes = {
|
|
|
|
title: PropTypes.string,
|
2019-05-27 15:13:43 -04:00
|
|
|
cat: PropTypes.string,
|
2019-05-27 13:26:29 -04:00
|
|
|
tooltip: PropTypes.string,
|
|
|
|
value: PropTypes.any,
|
|
|
|
user_building_like: PropTypes.bool
|
|
|
|
}
|
|
|
|
|
2019-08-14 03:28:15 -04:00
|
|
|
const MultiDataEntry: React.FunctionComponent<any> = (props) => { // TODO: remove any
|
2019-01-19 13:47:08 -05:00
|
|
|
let content;
|
|
|
|
|
|
|
|
if (props.value && props.value.length) {
|
|
|
|
content = <ul>{
|
|
|
|
props.value.map((item, index) => {
|
2019-05-27 13:26:29 -04:00
|
|
|
return <li key={index}><a href={sanitiseURL(item)}>{item}</a></li>
|
2019-01-19 13:47:08 -05:00
|
|
|
})
|
|
|
|
}</ul>
|
|
|
|
} else {
|
|
|
|
content = '\u00A0'
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<dt>
|
|
|
|
{ props.title }
|
|
|
|
{ props.tooltip? <Tooltip text={ props.tooltip } /> : null }
|
2019-08-06 17:11:17 -04:00
|
|
|
{ (props.copying && props.cat && props.slug && !props.disabled)?
|
|
|
|
<div className="icon-buttons">
|
|
|
|
<label className="icon-button copy">
|
|
|
|
Copy
|
|
|
|
<input type="checkbox" checked={props.copy}
|
|
|
|
onChange={() => props.toggleCopyAttribute(props.slug)}/>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
: null
|
|
|
|
}
|
2019-01-19 13:47:08 -05:00
|
|
|
</dt>
|
|
|
|
<dd>{ content }</dd>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:26:29 -04:00
|
|
|
MultiDataEntry.propTypes = {
|
|
|
|
title: PropTypes.string,
|
|
|
|
tooltip: PropTypes.string,
|
|
|
|
value: PropTypes.arrayOf(PropTypes.string)
|
|
|
|
}
|
|
|
|
|
2018-10-25 09:36:52 -04:00
|
|
|
const UPRNsDataEntry = (props) => {
|
|
|
|
const uprns = props.value || [];
|
2019-05-27 13:26:29 -04:00
|
|
|
const noParent = uprns.filter(uprn => uprn.parent_uprn == null);
|
|
|
|
const withParent = uprns.filter(uprn => uprn.parent_uprn != null);
|
2018-10-25 09:36:52 -04:00
|
|
|
|
|
|
|
return (
|
2019-05-27 11:39:16 -04:00
|
|
|
<Fragment>
|
|
|
|
<dt>
|
|
|
|
{ props.title }
|
|
|
|
{ props.tooltip? <Tooltip text={ props.tooltip } /> : null }
|
|
|
|
</dt>
|
|
|
|
<dd><ul className="uprn-list">
|
|
|
|
<Fragment>{
|
2019-05-27 13:26:29 -04:00
|
|
|
noParent.length?
|
|
|
|
noParent.map(uprn => (
|
2019-05-27 11:39:16 -04:00
|
|
|
<li key={uprn.uprn}>{uprn.uprn}</li>
|
2018-10-25 09:36:52 -04:00
|
|
|
))
|
2019-05-27 11:39:16 -04:00
|
|
|
: '\u00A0'
|
|
|
|
}</Fragment>
|
|
|
|
{
|
2019-05-27 13:26:29 -04:00
|
|
|
withParent.length?
|
2019-05-27 11:39:16 -04:00
|
|
|
<details>
|
|
|
|
<summary>Children</summary>
|
|
|
|
{
|
2019-05-27 13:26:29 -04:00
|
|
|
withParent.map(uprn => (
|
2019-05-27 11:39:16 -04:00
|
|
|
<li key={uprn.uprn}>{uprn.uprn} (child of {uprn.parent_uprn})</li>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</details>
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
</ul></dd>
|
|
|
|
</Fragment>
|
2018-10-25 09:36:52 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:26:29 -04:00
|
|
|
UPRNsDataEntry.propTypes = {
|
|
|
|
title: PropTypes.string,
|
|
|
|
tooltip: PropTypes.string,
|
|
|
|
value: PropTypes.arrayOf(PropTypes.shape({
|
|
|
|
uprn: PropTypes.string.isRequired,
|
|
|
|
parent_uprn: PropTypes.string
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2018-09-09 17:22:44 -04:00
|
|
|
export default BuildingView;
|