import React, { Fragment } from 'react';
import { Link, NavLink } from 'react-router-dom';
import PropTypes from 'prop-types';
import Sidebar from './sidebar';
import Tooltip from './tooltip';
import InfoBox from './info-box';
import { EditIcon } from './icons';
import { sanitiseURL } from './helpers';
import CONFIG from './fields-config.json';
const BuildingView = (props) => {
if (!props.building_id){
return (
Back to maps
);
}
const cat = props.match.params.cat;
return (
{
CONFIG.map(section => (
))
}
);
}
BuildingView.propTypes = {
building_id: PropTypes.number,
match: PropTypes.object,
uprns: PropTypes.arrayOf(PropTypes.shape({
uprn: PropTypes.string.isRequired,
parent_uprn: PropTypes.string
})),
building_like: PropTypes.bool
}
class DataSection extends React.Component {
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 (
match}>
{props.title}
{
match?
!props.inactive?
{
props.fields.map(field => {
switch (field.type) {
case 'uprn_list':
return
case 'text_multi':
return
case 'like':
return
default:
return
}
})
}
: {props.intro}
: null
}
);
}
}
DataSection.propTypes = {
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
}
const DataEntry = (props) => {
return (
{ props.title }
{ props.tooltip? : null }
{ (props.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 = (props) => {
const data_string = JSON.stringify({like: true});
(
{ 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! : null
}
);
}
LikeDataEntry.propTypes = {
title: PropTypes.string,
cat: PropTypes.string,
tooltip: PropTypes.string,
value: PropTypes.any,
user_building_like: PropTypes.bool
}
const MultiDataEntry = (props) => {
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;