Skip to content

Commit

Permalink
fix(publications): Restore export in different formats
Browse files Browse the repository at this point in the history
  • Loading branch information
annelhote committed Feb 7, 2025
1 parent 17c969d commit 15c60d7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
4 changes: 0 additions & 4 deletions client/src/components/button-dropdown/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,19 @@ export default function ButtonDropdown({ className, data, label, searchParams })
color="blue-ecume"
disabled={!data.length}
icon="save-line"
size="sm"
>
{`Export ${label} (${data.length})`}
</Button>
<div className="dropdown-content">
<Button
color="blue-ecume"
onClick={() => { export2Csv({ data, label, searchParams }); toastExport(); }}
size="sm"
>
Export in CSV (minimal data)
</Button>
<Button
color="blue-ecume"
onClick={() => { export2jsonl({ data, label, searchParams }); toastExport(); }}
size="sm"
>
Export in JSONL (complete data)
</Button>
Expand All @@ -60,7 +57,6 @@ export default function ButtonDropdown({ className, data, label, searchParams })
const numberOfLines = export2FosmCsv({ data, label, searchParams });
toastExport(numberOfLines);
}}
size="sm"
>
Custom export for French OSM
</Button>
Expand Down
22 changes: 15 additions & 7 deletions client/src/utils/files.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ const export2FosmCsv = ({ data, label, searchParams }) => {
};

const export2Csv = ({ data, label, searchParams }) => {
// Deep copy of data
const dataCopy = JSON.parse(JSON.stringify(data));
const deletedFields = ['affiliations', 'affiliationsHtml', 'affiliationsTooltip', 'allIds', 'allInfos', 'authors', 'datasource', 'id', 'hasCorrection', 'key', 'nameHtml', 'selected'];
const stringifiedFields = ['addList', 'fr_authors_orcid', 'fr_publications_linked', 'removeList', 'rors', 'rorsInOpenAlex', 'rorsToCorrect', 'worksExample'];
data.forEach((work) => {
dataCopy.forEach((work) => {
work.allIds?.forEach((id) => {
work[id.id_type] = id.id_value;
});
Expand All @@ -73,7 +75,7 @@ const export2Csv = ({ data, label, searchParams }) => {
}
});
});
const csvFile = Papa.unparse(data, { skipEmptyLines: 'greedy' });
const csvFile = Papa.unparse(dataCopy, { skipEmptyLines: 'greedy' });
const link = document.createElement('a');
link.href = URL.createObjectURL(new Blob([csvFile], { type: 'text/csv;charset=utf-8' }));
const fileName = getFileName({ extension: 'csv', label, searchParams });
Expand All @@ -84,12 +86,13 @@ const export2Csv = ({ data, label, searchParams }) => {
};

const export2json = ({ data, label, searchParams }) => {
const link = document.createElement('a');
const exportedData = data.map((work) => {
// Deep copy of data
const dataCopy = JSON.parse(JSON.stringify(data));
dataCopy.forEach((work) => {
delete work.allInfos;
return work;
});
link.href = URL.createObjectURL(new Blob([JSON.stringify(exportedData, null, 2)], { type: 'application/json' }));
const link = document.createElement('a');
link.href = URL.createObjectURL(new Blob([JSON.stringify(dataCopy, null, 2)], { type: 'application/json' }));
const fileName = getFileName({ extension: 'json', label, searchParams });
link.setAttribute('download', fileName);
document.body.appendChild(link);
Expand All @@ -98,8 +101,13 @@ const export2json = ({ data, label, searchParams }) => {
};

const export2jsonl = ({ data, label, searchParams }) => {
// Deep copy of data
const dataCopy = JSON.parse(JSON.stringify(data));
dataCopy.forEach((work) => {
delete work.allInfos;
});
const link = document.createElement('a');
link.href = URL.createObjectURL(new Blob([data.map(JSON.stringify).join('\n')], { type: 'application/jsonl+json' }));
link.href = URL.createObjectURL(new Blob([dataCopy.map(JSON.stringify).join('\n')], { type: 'application/jsonl+json' }));
const fileName = getFileName({ extension: 'jsonl', label, searchParams });
link.setAttribute('download', fileName);
document.body.appendChild(link);
Expand Down

1 comment on commit 15c60d7

@annelhote
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

close #92

Please sign in to comment.