colouring-montreal/app/src/frontend/pages/data-extracts.tsx

93 lines
3.4 KiB
TypeScript
Raw Normal View History

2019-08-30 08:45:55 -04:00
import React, { FunctionComponent } from 'react';
2019-11-07 02:39:26 -05:00
import { NavLink } from 'react-router-dom';
2019-08-30 08:45:55 -04:00
import { dateReviver } from '../../helpers';
2019-08-30 08:45:55 -04:00
interface ExtractViewModel {
extract_id: number;
extracted_on: Date;
download_path: string;
}
interface DataExtractsState {
2019-10-01 09:46:28 -04:00
extracts: ExtractViewModel[];
2019-08-30 08:45:55 -04:00
latestExtract: ExtractViewModel;
previousExtracts: ExtractViewModel[];
}
export default class DataExtracts extends React.Component<{}, DataExtractsState> {
constructor(props) {
super(props);
this.state = {
2019-10-01 09:46:28 -04:00
extracts: undefined,
2019-08-30 08:45:55 -04:00
latestExtract: undefined,
previousExtracts: undefined
};
}
async componentDidMount() {
const res = await fetch('/api/extracts');
const data = JSON.parse(await res.text(), dateReviver);
const extracts = (data.extracts as ExtractViewModel[])
.sort((a, b) => a.extracted_on.valueOf() - b.extracted_on.valueOf())
.reverse();
2019-10-01 09:46:28 -04:00
this.setState({ extracts: extracts, latestExtract: extracts[0], previousExtracts: extracts.slice(1) });
2019-08-30 08:45:55 -04:00
}
render() {
2019-10-01 09:46:28 -04:00
2019-08-30 08:45:55 -04:00
return (
<article>
<section className="main-col">
<h1 className="h2">Open data extracts</h1>
<p>Choose one of the links below to download an archive containing the open data collected on the Colouring London platform</p>
2019-10-03 09:55:54 -04:00
<p>By downloading data extracts from this site, you agree to the <NavLink to="/data-accuracy.html">data accuracy agreement </NavLink> and the <NavLink to="/ordnance-survey-uprn.html">Ordnance Survey terms of UPRN usage</NavLink>.</p>
2019-08-30 08:45:55 -04:00
{
2019-10-01 09:46:28 -04:00
this.state.extracts == undefined ?
2019-08-30 08:45:55 -04:00
<p>Loading extracts...</p> :
2019-10-01 09:46:28 -04:00
(
this.state.extracts.length === 0 ?
<p>No extracts available</p> :
null
)
}
{
this.state.latestExtract != undefined ?
2019-08-30 08:45:55 -04:00
<div>
<h1 className="h3">Latest extract</h1>
<ExtractDownloadLink {...this.state.latestExtract} />
2019-10-01 09:46:28 -04:00
</div> :
null
2019-08-30 08:45:55 -04:00
}
{
this.state.previousExtracts && this.state.previousExtracts.length > 0 ?
(<div>
<h1 className="h3">Older extracts</h1>
2019-09-09 08:26:24 -04:00
<ul>
{
this.state.previousExtracts.map(e =>
<li>
<ExtractDownloadLink {...e} />
</li>
)
}
</ul>
2019-08-30 08:45:55 -04:00
</div>) :
null
}
</section>
</article>
);
}
}
const ExtractDownloadLink: FunctionComponent<ExtractViewModel> = (props) => (
<p><a href={props.download_path}>Extracted on {props.extracted_on.toDateString()}</a></p>
);