Skip to content

Commit

Permalink
feat(actions): Export validated datasets in CSV, see #55
Browse files Browse the repository at this point in the history
  • Loading branch information
annelhote committed Jan 18, 2024
1 parent f261578 commit 6415693
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
25 changes: 23 additions & 2 deletions client/src/pages/actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { useSearchParams } from 'react-router-dom';
import { Tooltip } from 'react-tooltip';

import Button from '../components/button';
import { export2FosmCsv, export2json, importJson } from '../utils/file';
import { export2Csv, export2FosmCsv, export2json, importJson } from '../utils/file';
import { status } from '../config';

export default function Actions({
allAffiliations,
allDatasets,
allPublications,
options,
setAllAffiliations,
Expand Down Expand Up @@ -49,6 +50,18 @@ export default function Actions({
<Tooltip id="restore-affiliations-button">
Restore affiliations from saved file
</Tooltip>
<Button
data-tooltip-id="export-datasets-button"
disabled={!allDatasets.length}
icon="ri-save-line"
onClick={() => export2Csv(allDatasets)}
size="sm"
>
Export datasets
</Button>
<Tooltip id="export-datasets-button" hidden={!allPublications.length}>
Export the validated datasets in CSV
</Tooltip>
<Button
data-tooltip-id="export-fosm-button"
disabled={!allPublications.length}
Expand All @@ -59,7 +72,7 @@ export default function Actions({
Export French OSM
</Button>
<Tooltip id="export-fosm-button" hidden={!allPublications.length}>
Export the selected publications in the format needed to build a local French OSM
Export the validated publications in the format needed to build a local French OSM
</Tooltip>
</Col>
</Row>
Expand Down Expand Up @@ -87,6 +100,14 @@ Actions.propTypes = {
works: PropTypes.arrayOf(PropTypes.string).isRequired,
worksNumber: PropTypes.number.isRequired,
})).isRequired,
allDatasets: PropTypes.arrayOf(PropTypes.shape({
affiliations: PropTypes.arrayOf(PropTypes.string).isRequired,
allIds: PropTypes.arrayOf(PropTypes.object).isRequired,
datasource: PropTypes.arrayOf(PropTypes.string).isRequired,
id: PropTypes.string.isRequired,
status: PropTypes.string.isRequired,
type: PropTypes.string.isRequired,
})).isRequired,
allPublications: PropTypes.arrayOf(PropTypes.shape({
affiliations: PropTypes.arrayOf(PropTypes.string).isRequired,
allIds: PropTypes.arrayOf(PropTypes.object).isRequired,
Expand Down
1 change: 1 addition & 0 deletions client/src/pages/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export default function Home() {
<Container className="fr-mx-5w" as="section" fluid>
<Actions
allAffiliations={allAffiliations}
allDatasets={allDatasets}
allPublications={allPublications}
options={options}
setAllAffiliations={setAllAffiliations}
Expand Down
17 changes: 17 additions & 0 deletions client/src/utils/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ const export2FosmCsv = (allPublications) => {
document.body.removeChild(link);
};

const export2Csv = (allWorks) => {
const validatedWorks = allWorks.filter((publication) => publication.status === status.validated.id);
const headers = Object.keys(validatedWorks?.[0] ?? {});
const csvFile = [
headers,
...validatedWorks.map((item) => Object.values(item)),
].map((e) => e.join(',')).join('\n');

const link = document.createElement('a');
link.href = URL.createObjectURL(new Blob([csvFile], { type: 'text/csv;charset=utf-8' }));
link.setAttribute('download', 'works-finder-datasets.csv');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};

const export2json = (data) => {
const link = document.createElement('a');
link.href = URL.createObjectURL(new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' }));
Expand Down Expand Up @@ -61,6 +77,7 @@ const importJson = (e, optionsInit, setAllAffiliations, setAllPublications, setS
};

export {
export2Csv,
export2FosmCsv,
export2json,
importJson,
Expand Down

0 comments on commit 6415693

Please sign in to comment.