2019-08-06 17:13:12 -04:00
|
|
|
import React, { Fragment } from 'react';
|
2019-05-10 11:10:16 -04:00
|
|
|
import { Link, Redirect } from 'react-router-dom';
|
|
|
|
import { parse } from 'query-string';
|
2019-05-27 15:13:43 -04:00
|
|
|
import PropTypes from 'prop-types';
|
2019-05-09 04:16:36 -04:00
|
|
|
|
|
|
|
import Sidebar from './sidebar';
|
2019-05-10 11:10:16 -04:00
|
|
|
import CONFIG from './fields-config.json';
|
2019-08-14 03:45:00 -04:00
|
|
|
import InfoBox from './components/info-box';
|
2019-08-06 17:13:12 -04:00
|
|
|
import { sanitiseURL } from './helpers';
|
2019-05-09 04:16:36 -04:00
|
|
|
|
|
|
|
const MultiEdit = (props) => {
|
|
|
|
if (!props.user){
|
|
|
|
return <Redirect to="/sign-up.html" />
|
|
|
|
}
|
2019-05-10 09:00:20 -04:00
|
|
|
const cat = props.match.params.cat;
|
2019-05-10 11:10:16 -04:00
|
|
|
if (cat === 'like') {
|
|
|
|
// special case for likes
|
|
|
|
return (
|
|
|
|
<Sidebar
|
2019-05-27 16:28:47 -04:00
|
|
|
title='Quick edit'
|
2019-05-27 15:13:43 -04:00
|
|
|
back={`/edit/${cat}.html`}>
|
|
|
|
<section className='data-section'>
|
2019-05-27 16:28:47 -04:00
|
|
|
<header className={`section-header view ${cat} active`}>
|
|
|
|
<a><h3 className="h3">Like me!</h3></a>
|
|
|
|
</header>
|
|
|
|
<form className='buttons-container'>
|
|
|
|
<InfoBox msg='Click all the buildings that you like and think contribute to the city!' />
|
2019-05-10 11:10:16 -04:00
|
|
|
|
2019-05-27 15:13:43 -04:00
|
|
|
<Link to='/view/like.html' className='btn btn-secondary'>Back to view</Link>
|
|
|
|
<Link to='/edit/like.html' className='btn btn-secondary'>Back to edit</Link>
|
2019-05-27 16:28:47 -04:00
|
|
|
</form>
|
2019-05-10 11:10:16 -04:00
|
|
|
</section>
|
|
|
|
</Sidebar>
|
|
|
|
);
|
|
|
|
}
|
2019-05-10 09:00:20 -04:00
|
|
|
|
2019-05-10 11:10:16 -04:00
|
|
|
const q = parse(props.location.search);
|
2019-08-09 13:49:43 -04:00
|
|
|
const data = JSON.parse(q.data as string) // TODO: verify what happens when data is string[]
|
2019-05-27 16:28:47 -04:00
|
|
|
const title = sectionTitleFromCat(cat);
|
2019-05-09 04:16:36 -04:00
|
|
|
return (
|
|
|
|
<Sidebar
|
2019-05-27 15:13:43 -04:00
|
|
|
title='Quick edit'
|
2019-05-09 04:16:36 -04:00
|
|
|
back={`/edit/${cat}.html`}>
|
2019-05-27 15:13:43 -04:00
|
|
|
<section className='data-section'>
|
2019-05-27 16:28:47 -04:00
|
|
|
<header className={`section-header view ${cat} active`}>
|
|
|
|
<a><h3 className="h3">{title}</h3></a>
|
|
|
|
</header>
|
2019-08-06 17:13:12 -04:00
|
|
|
<dl className='data-list'>
|
2019-08-01 05:29:32 -04:00
|
|
|
{
|
|
|
|
Object.keys(data).map((key => {
|
|
|
|
const label = fieldTitleFromSlug(key);
|
2019-08-06 17:13:12 -04:00
|
|
|
return <DataEntry key={key} label={label} value={data[key]}/>
|
2019-08-01 05:29:32 -04:00
|
|
|
}))
|
|
|
|
}
|
2019-08-06 17:13:12 -04:00
|
|
|
</dl>
|
2019-05-27 16:28:47 -04:00
|
|
|
<form className='buttons-container'>
|
2019-08-06 17:13:12 -04:00
|
|
|
<InfoBox msg='Click buildings to colour using the data above' />
|
2019-05-10 11:10:16 -04:00
|
|
|
|
2019-05-27 15:13:43 -04:00
|
|
|
<Link to={`/view/${cat}.html`} className='btn btn-secondary'>Back to view</Link>
|
|
|
|
<Link to={`/edit/${cat}.html`} className='btn btn-secondary'>Back to edit</Link>
|
2019-05-27 16:28:47 -04:00
|
|
|
</form>
|
2019-05-09 04:16:36 -04:00
|
|
|
</section>
|
|
|
|
</Sidebar>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-27 15:13:43 -04:00
|
|
|
MultiEdit.propTypes = {
|
|
|
|
user: PropTypes.object,
|
|
|
|
match: PropTypes.object,
|
|
|
|
location: PropTypes.object
|
|
|
|
}
|
|
|
|
|
2019-08-06 17:13:12 -04:00
|
|
|
const DataEntry = (props) => {
|
|
|
|
let content;
|
|
|
|
|
|
|
|
if (props.value != null && props.value !== '') {
|
|
|
|
if (typeof(props.value) === 'boolean') {
|
|
|
|
content = (props.value)? 'Yes' : 'No'
|
|
|
|
} else if (Array.isArray(props.value)) {
|
|
|
|
if (props.value.length) {
|
|
|
|
content = <ul>{
|
|
|
|
props.value.map((item, index) => {
|
|
|
|
return <li key={index}><a href={sanitiseURL(item)}>{item}</a></li>
|
|
|
|
})
|
|
|
|
}</ul>
|
|
|
|
} else {
|
|
|
|
content = '\u00A0'
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
content = props.value
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
content = '\u00A0'
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<dt>{props.label}</dt>
|
|
|
|
<dd>{content}</dd>
|
|
|
|
</Fragment>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:28:47 -04:00
|
|
|
function sectionTitleFromCat(cat) {
|
|
|
|
for (let index = 0; index < CONFIG.length; index++) {
|
|
|
|
const section = CONFIG[index];
|
|
|
|
if (section.slug === cat) {
|
|
|
|
return section.title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2019-05-27 15:13:43 -04:00
|
|
|
function fieldTitleFromSlug(slug) {
|
2019-05-10 11:10:16 -04:00
|
|
|
const fields = CONFIG.reduce(
|
|
|
|
(prev, section) => {
|
|
|
|
const el = prev.concat(
|
|
|
|
section.fields.filter(
|
2019-08-09 13:49:43 -04:00
|
|
|
(field: any) => field.slug === slug // TODO: remove any
|
2019-05-10 11:10:16 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
return el
|
|
|
|
}, []
|
|
|
|
)
|
|
|
|
if (fields.length === 1 && fields[0].title) {
|
|
|
|
return fields[0].title
|
|
|
|
} else {
|
|
|
|
console.error('Expected single match, got', fields)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 04:16:36 -04:00
|
|
|
export default MultiEdit;
|