Skip to content

Commit

Permalink
feat(export): Add JSONL as export format
Browse files Browse the repository at this point in the history
  • Loading branch information
annelhote committed Jan 30, 2024
1 parent cd4815c commit 74b9277
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
54 changes: 41 additions & 13 deletions client/src/pages/actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useSearchParams } from 'react-router-dom';
import { Tooltip } from 'react-tooltip';

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

export default function Actions({
Expand Down Expand Up @@ -51,28 +51,52 @@ export default function Actions({
Restore affiliations from saved file
</Tooltip>
<Button
data-tooltip-id="export-datasets-button"
data-tooltip-id="export-datasets-csv-button"
disabled={!allDatasets.length}
icon="ri-save-line"
onClick={() => export2Csv(allDatasets, 'datasets')}
onClick={() => export2Csv({ data: allDatasets, label: 'datasets' })}
size="sm"
>
Export datasets
Export datasets (minimal data)
</Button>
<Tooltip id="export-datasets-button" hidden={!allDatasets.length}>
Export all the datasets in CSV
<Tooltip id="export-datasets-csv-button" hidden={!allDatasets.length}>
Export all datasets in CSV
</Tooltip>
<Button
data-tooltip-id="export-publications-button"
data-tooltip-id="export-datasets-jsonl-button"
disabled={!allDatasets.length}
icon="ri-save-line"
onClick={() => export2jsonl({ data: allDatasets, label: 'datasets' })}
size="sm"
>
Export datasets (complete data)
</Button>
<Tooltip id="export-datasets-jsonl-button" hidden={!allDatasets.length}>
Export all datasets in JSONL
</Tooltip>
<Button
data-tooltip-id="export-publications-csv-button"
disabled={!allPublications.length}
icon="ri-save-line"
onClick={() => export2Csv({ data: allPublications, label: 'publications' })}
size="sm"
>
Export publications (minimal data)
</Button>
<Tooltip id="export-publications-csv-button" hidden={!allPublications.length}>
Export all publications in CSV
</Tooltip>
<Button
data-tooltip-id="export-publications-jsonl-button"
disabled={!allPublications.length}
icon="ri-save-line"
onClick={() => export2Csv(allPublications, 'publications')}
onClick={() => export2jsonl({ data: allPublications, label: 'publications' })}
size="sm"
>
Export publications
Export publications (complete data)
</Button>
<Tooltip id="export-publications-button" hidden={!allPublications.length}>
Export all the publications in CSV
<Tooltip id="export-publications-jsonl-button" hidden={!allPublications.length}>
Export all publications in JSONL
</Tooltip>
<Button
data-tooltip-id="export-fosm-button"
Expand All @@ -81,10 +105,14 @@ export default function Actions({
onClick={() => export2FosmCsv(allPublications)}
size="sm"
>
Export French OSM
Custom export for French OSM
</Button>
<Tooltip id="export-fosm-button" hidden={!allPublications.length}>
Export the validated publications in the format needed to build a local French OSM
Export the
{' '}
<b>validated</b>
{' '}
publications in the format needed to build a local French OSM in CSV
</Tooltip>
</Col>
</Row>
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function Home() {
});

useWebSocket(`${VITE_WS_HOST}:${VITE_WS_PORT}`, {
onMessage: (message) => setCurrent(Number(message.data) + 1),
onMessage: (message) => setCurrent(Number(message.data)),
share: true,
});

Expand Down
28 changes: 20 additions & 8 deletions client/src/utils/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const export2FosmCsv = (allPublications) => {
document.body.removeChild(link);
};

const export2Csv = (allWorks, label) => {
allWorks.forEach((work) => {
const export2Csv = ({ data, label }) => {
data.forEach((work) => {
work.allIds?.forEach((id) => {
work[id.id_type] = id.id_value;
});
Expand All @@ -43,24 +43,35 @@ const export2Csv = (allWorks, label) => {
delete work.id;
return work;
});
const headers = Object.keys(allWorks?.[0] ?? {});
const headers = Object.keys(data?.[0] ?? {});
const csvFile = [
headers,
...allWorks.map((item) => Object.values(item).map((cell) => JSON.stringify(cell))),
...data.map((item) => Object.values(item).map((cell) => JSON.stringify(cell))),
].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-${label}.csv`);
const fileName = label ? `works-finder-${label}.csv` : 'works-finder.csv';
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};

const export2json = (data) => {
const export2json = ({ data, label }) => {
const link = document.createElement('a');
link.href = URL.createObjectURL(new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' }));
link.setAttribute('download', 'works-finder.json');
const fileName = label ? `works-finder-${label}.json` : 'works-finder.json';
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};

const export2jsonl = ({ data, label }) => {
const link = document.createElement('a');
link.href = URL.createObjectURL(new Blob([data.map(JSON.stringify).join('\n')], { type: 'application/jsonl+json' }));
const fileName = label ? `works-finder-${label}.jsonl` : 'works-finder.jsonl';
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
Expand Down Expand Up @@ -92,5 +103,6 @@ export {
export2Csv,
export2FosmCsv,
export2json,
export2jsonl,
importJson,
};

0 comments on commit 74b9277

Please sign in to comment.