From 517c1722a2557b656d5544b487b351ed042ecbc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anne=20L=27H=C3=B4te?= Date: Fri, 28 Jul 2023 11:14:53 +0200 Subject: [PATCH] fix(file): Restore filters from file --- client/src/pages/home/actions.jsx | 5 ++++- client/src/pages/home/filters.jsx | 6 +++--- client/src/utils/file.js | 5 +++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/client/src/pages/home/actions.jsx b/client/src/pages/home/actions.jsx index 5b5175fd..783733be 100644 --- a/client/src/pages/home/actions.jsx +++ b/client/src/pages/home/actions.jsx @@ -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'; @@ -13,6 +15,7 @@ export default function Actions({ setOptions, setPublicationsDataTable, }) { + const [, setSearchParams] = useSearchParams(); const [displayFileUpload, setDisplayFileUpload] = useState(false); return ( @@ -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)} /> diff --git a/client/src/pages/home/filters.jsx b/client/src/pages/home/filters.jsx index 1ae16d63..1bd7b802 100644 --- a/client/src/pages/home/filters.jsx +++ b/client/src/pages/home/filters.jsx @@ -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({ diff --git a/client/src/utils/file.js b/client/src/utils/file.js index 562f996b..be6153bc 100644 --- a/client/src/utils/file.js +++ b/client/src/utils/file.js @@ -7,7 +7,7 @@ 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) => { @@ -15,7 +15,8 @@ const importJson = (e, setAffiliationsDataTable, setPublicationsDataTable, setOp options.restoreFromFile = true; setAffiliationsDataTable(affiliationsDataTable); setPublicationsDataTable(publicationsDataTable); - setOptions(options); + // setOptions(options); + setSearchParams(options); }; };