Skip to content

Commit

Permalink
fix(file): Restore filters from file
Browse files Browse the repository at this point in the history
  • Loading branch information
annelhote committed Jul 28, 2023
1 parent a54a882 commit 517c172
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
5 changes: 4 additions & 1 deletion client/src/pages/home/actions.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Col, File, Row } from '@dataesr/react-dsfr';
import PropTypes from 'prop-types';
import { useState } from 'react';
import { useSearchParams } from 'react-router-dom';

import Button from '../../components/button';

import { export2BsoCsv, export2json, importJson } from '../../utils/file';
Expand All @@ -13,6 +15,7 @@ export default function Actions({
setOptions,
setPublicationsDataTable,
}) {
const [, setSearchParams] = useSearchParams();
const [displayFileUpload, setDisplayFileUpload] = useState(false);

return (
Expand Down Expand Up @@ -54,7 +57,7 @@ export default function Actions({
accept=".json"
hint="Select JSON file to restore from previous state"
label="JSON file"
onChange={(e) => importJson(e, setAffiliationsDataTable, setPublicationsDataTable, setOptions)}
onChange={(e) => importJson(e, setAffiliationsDataTable, setPublicationsDataTable, setOptions, setSearchParams)}
/>
</Col>
</Row>
Expand Down
6 changes: 3 additions & 3 deletions client/src/pages/home/filters.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export default function Filters({ sendQuery }) {
const [authorsToExclude, setAuthorsToExclude] = useState(searchParams.size ? searchParams.getAll('authorsToExclude') : []);
const [dataIdentifiers, setDataIdentifiers] = useState(searchParams.size ? searchParams.getAll('dataIdentifiers') : identifiers);
const [datasources, setDatasources] = useState(searchParams.size ? searchParams.getAll('datasources') : sources.map((source) => source.key));
const [endYear, setEndYear] = useState(searchParams.size ? searchParams.getAll('endYear')?.[0] : '2021');
const [moreOptions, setMoreOptions] = useState(searchParams.size ? searchParams.getAll('moreOptions')?.[0].toString() === 'true' : false);
const [startYear, setStartYear] = useState(searchParams.size ? searchParams.getAll('startYear')?.[0] : '2021');
const [endYear, setEndYear] = useState(searchParams.size ? searchParams.get('endYear') : '2021');
const [moreOptions, setMoreOptions] = useState(searchParams.size ? searchParams.get('moreOptions').toString() === 'true' : false);
const [startYear, setStartYear] = useState(searchParams.size ? searchParams.get('startYear') : '2021');

useEffect(() => {
setSearchParams({
Expand Down
5 changes: 3 additions & 2 deletions client/src/utils/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ const export2json = (affiliationsDataTable, options, publicationsDataTable) => {
document.body.removeChild(link);
};

const importJson = (e, setAffiliationsDataTable, setPublicationsDataTable, setOptions) => {
const importJson = (e, setAffiliationsDataTable, setPublicationsDataTable, setOptions, setSearchParams) => {
const fileReader = new FileReader();
fileReader.readAsText(e.target.files[0], 'UTF-8');
fileReader.onload = (f) => {
const { affiliationsDataTable, options, publicationsDataTable } = JSON.parse(f.target.result);
options.restoreFromFile = true;
setAffiliationsDataTable(affiliationsDataTable);
setPublicationsDataTable(publicationsDataTable);
setOptions(options);
// setOptions(options);
setSearchParams(options);
};
};

Expand Down

0 comments on commit 517c172

Please sign in to comment.