import urlapi from 'url';
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 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 => (
{
section.fields.map(field => {
switch (field.type) {
case 'uprn_list':
return
case 'text_multi':
return
case 'like':
return
default:
return
}
})
}
))
}
);
}
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
}
const DataSection = (props) => {
const match = props.cat === props.slug;
return (
match}>
{props.title}
{
props.help?
Info
: null
}
{
!props.inactive?
Edit
: null
}
{
match?
!props.inactive?
{props.children}
: {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) => (
{ props.title }
{ props.tooltip? : null }
{ (props.cat && props.slug && !props.disabled)?
Copy
: 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) => (
{ 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 }
{ content }
);
}
MultiDataEntry.propTypes = {
title: PropTypes.string,
tooltip: PropTypes.string,
value: PropTypes.arrayOf(PropTypes.string)
}
function sanitiseURL(string){
let url_
// http or https
if (!(string.substring(0, 7) === 'http://' || string.substring(0, 8) === 'https://')){
return null
}
try {
url_ = document.createElement('a')
url_.href = string
} catch (error) {
try {
url_ = urlapi.parse(string)
} catch (error) {
return null
}
}
// required (www.example.com)
if (!url_.hostname || url_.hostname === '' || url_.hostname === 'localhost'){
return null
}
// optional (/some/path)
// url_.pathname;
// optional (?name=value)
// url_.search;
// optional (#anchor)
// url_.hash;
return `${url_.protocol}//${url_.hostname}${url_.pathname || ''}${url_.search || ''}${url_.hash || ''}`
}
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;