colouring-montreal/app/src/frontend/building/multi-edit.tsx

96 lines
3.2 KiB
TypeScript
Raw Normal View History

import React from 'react';
import { Link, Redirect, RouteComponentProps } from 'react-router-dom';
import { parse } from 'query-string';
2019-05-09 04:16:36 -04:00
import Sidebar from './sidebar';
import InfoBox from '../components/info-box';
import { BackIcon }from '../components/icons';
import DataEntry from './data-components/data-entry';
2019-10-25 13:07:24 -04:00
import { dataFields } from '../data_fields';
import { User } from '../models/user';
2019-05-09 04:16:36 -04:00
interface MultiEditRouteParams {
cat: string;
}
interface MultiEditProps extends RouteComponentProps<MultiEditRouteParams> {
user?: User;
}
const MultiEdit: React.FC<MultiEditProps> = (props) => {
2019-05-09 04:16:36 -04:00
if (!props.user){
return <Redirect to="/sign-up.html" />
}
2019-05-10 09:00:20 -04:00
const cat = props.match.params.cat;
if (cat === 'like') {
// special case for likes
return (
2019-08-14 14:27:28 -04:00
<Sidebar>
2019-05-27 15:13:43 -04:00
<section className='data-section'>
<header className={`section-header view ${cat} background-${cat}`}>
<h2 className="h2">Like me!</h2>
2019-05-27 16:28:47 -04:00
</header>
<form className='buttons-container'>
<InfoBox msg='Click all the buildings that you like and think contribute to the city!' />
2019-09-08 20:11:45 -04:00
<Link to='/view/like' className='btn btn-secondary'>Back to view</Link>
<Link to='/edit/like' className='btn btn-secondary'>Back to edit</Link>
2019-05-27 16:28:47 -04:00
</form>
</section>
</Sidebar>
);
}
2019-05-10 09:00:20 -04:00
const q = parse(props.location.search);
let data: object;
if (cat === 'like'){
data = { like: true }
} else {
try {
// TODO: verify what happens if data is string[]
data = JSON.parse(q.data as string);
} catch (error) {
console.error(error, q)
data = {}
}
}
2019-05-09 04:16:36 -04:00
return (
2019-08-14 14:27:28 -04:00
<Sidebar>
2019-05-27 15:13:43 -04:00
<section className='data-section'>
<header className={`section-header view ${cat} background-${cat}`}>
<Link
className="icon-button back"
to={`/edit/${cat}`}>
<BackIcon />
</Link>
<h2 className="h2">Copy {cat} data</h2>
2019-05-27 16:28:47 -04:00
</header>
<form>
<InfoBox msg='Click buildings one at a time to colour using the data below' />
{
Object.keys(data).map((key => {
2019-10-25 13:07:24 -04:00
const info = dataFields[key] || {};
return (
<DataEntry
2019-10-25 13:07:24 -04:00
title={info.title || `Unknown field (${key})`}
slug={key}
disabled={true}
value={data[key]}
/>
)
}))
}
</form>
2019-05-27 16:28:47 -04:00
<form className='buttons-container'>
2019-09-08 20:11:45 -04:00
<Link to={`/view/${cat}`} className='btn btn-secondary'>Back to view</Link>
<Link to={`/edit/${cat}`} 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>
);
}
export default MultiEdit;