Skip to content

Commit

Permalink
feat(export): New files name
Browse files Browse the repository at this point in the history
  • Loading branch information
annelhote committed Feb 2, 2024
1 parent cb702b9 commit 3054021
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
9 changes: 5 additions & 4 deletions client/src/components/button-dropdown/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { export2Csv, export2FosmCsv, export2jsonl } from '../../utils/file';

import './index.scss';

export default function ButtonDropdown({ data, label }) {
export default function ButtonDropdown({ data, label, searchParams }) {
return (
<div className={`dropdown ${data.length > 0 ? 'enabled' : 'disabled'}`}>
<Button
Expand All @@ -20,20 +20,20 @@ export default function ButtonDropdown({ data, label }) {
</Button>
<div className="dropdown-content">
<Button
onClick={() => export2Csv({ data, label })}
onClick={() => export2Csv({ data, label, searchParams })}
size="sm"
>
Export in CSV (minimal data)
</Button>
<Button
onClick={() => export2jsonl({ data, label })}
onClick={() => export2jsonl({ data, label, searchParams })}
size="sm"
>
Export in JSONL (complete data)
</Button>
{label === 'publications' && (
<Button
onClick={() => export2FosmCsv(data)}
onClick={() => export2FosmCsv({ data })}
size="sm"
>
Custom export for French OSM
Expand All @@ -47,4 +47,5 @@ export default function ButtonDropdown({ data, label }) {
ButtonDropdown.propTypes = {
data: PropTypes.array.isRequired,
label: PropTypes.string.isRequired,
searchParams: PropTypes.object.isRequired,
};
3 changes: 2 additions & 1 deletion client/src/components/button-dropdown/index.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.dropdown {
display: inline-block;
position: relative;
z-index: 2;

.dropdown-content {
display: none;
Expand All @@ -10,6 +11,6 @@
}
}

.dropdown:hover .dropdown-content {
.dropdown.enabled:hover .dropdown-content {
display: block;
}
6 changes: 3 additions & 3 deletions client/src/pages/actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function Actions({
setAllPublications,
tagAffiliations,
}) {
const [, setSearchParams] = useSearchParams();
const [searchParams, setSearchParams] = useSearchParams();
const [displayFileUpload, setDisplayFileUpload] = useState(false);

const decidedAffiliations = allAffiliations.filter((affiliation) => affiliation.status !== status.tobedecided.id);
Expand Down Expand Up @@ -51,8 +51,8 @@ export default function Actions({
<Tooltip id="restore-affiliations-button">
Restore affiliations from saved file
</Tooltip>
<ButtonDropdown data={allDatasets} label="datasets" />
<ButtonDropdown data={allPublications} label="publications" />
<ButtonDropdown data={allDatasets} label="datasets" searchParams={searchParams} />
<ButtonDropdown data={allPublications} label="publications" searchParams={searchParams} />
</Col>
</Row>
{displayFileUpload && (
Expand Down
40 changes: 32 additions & 8 deletions client/src/utils/file.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
import { status } from '../config';

const export2FosmCsv = (allPublications) => {
const hashCode = (str) => {
let hash = 0;
if (str.length === 0) return hash;
for (let i = 0; i < str.length; i += 1) {
const chr = str.charCodeAt(i);
// eslint-disable-next-line no-bitwise
hash = ((hash << 5) - hash) + chr;
// eslint-disable-next-line no-bitwise
hash |= 0; // Convert to 32bit integer
}
// return positive numbers only
return hash + 2147483647 + 1;
};

const getFileName = ({ extension, label, searchParams }) => {
let fileName = 'works_finder';
fileName += label ? `_${label}` : '';
fileName += `_${hashCode(searchParams.get('affiliations'))}`;
fileName += `_${searchParams.get('startYear')}`;
fileName += `_${searchParams.get('endYear')}`;
fileName += `_${Date.now()}`;
fileName += `.${extension}`;
return fileName;
};

const export2FosmCsv = ({ data: allPublications }) => {
const csvHeader = ['doi', 'hal_id', 'nnt_id'].join(';');
const validatedPublications = allPublications.filter((publication) => publication.status === status.validated.id);
const getValue = (row, idType) => {
Expand Down Expand Up @@ -29,7 +54,7 @@ const export2FosmCsv = (allPublications) => {
document.body.removeChild(link);
};

const export2Csv = ({ data, label }) => {
const export2Csv = ({ data, label, searchParams }) => {
data.forEach((work) => {
work.allIds?.forEach((id) => {
work[id.id_type] = id.id_value;
Expand All @@ -41,7 +66,6 @@ const export2Csv = ({ data, label }) => {
delete work.authors;
delete work.datasource;
delete work.id;
return work;
});
const headers = Object.keys(data?.[0] ?? {});
const csvFile = [
Expand All @@ -50,27 +74,27 @@ const export2Csv = ({ data, label }) => {
].map((e) => e.join(',')).join('\n');
const link = document.createElement('a');
link.href = URL.createObjectURL(new Blob([csvFile], { type: 'text/csv;charset=utf-8' }));
const fileName = label ? `works-finder-${label}.csv` : 'works-finder.csv';
const fileName = getFileName({ extension: 'csv', label, searchParams });
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};

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

const export2jsonl = ({ data, label }) => {
const export2jsonl = ({ data, label, searchParams }) => {
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';
const fileName = getFileName({ extension: 'jsonl', label, searchParams });
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
Expand Down

0 comments on commit 3054021

Please sign in to comment.