diff --git a/src/components/data_search/DatasetFilterList.jsx b/src/components/data_search/DatasetFilterList.jsx index 73551e620..f9f9392b8 100644 --- a/src/components/data_search/DatasetFilterList.jsx +++ b/src/components/data_search/DatasetFilterList.jsx @@ -11,7 +11,7 @@ import { Checkbox } from '@mui/material'; import { flatten, uniq, compact, capitalize } from 'lodash'; export const DatasetFilterList = (props) => { - const { datasets, filters, filterHandler, isFiltered, searchRef } = props; + const { datasets, filters, filterHandler, isFiltered } = props; const accessManagementFilters = uniq(compact(datasets.map((dataset) => dataset.accessManagement))); const dataUseFilters = uniq(compact(flatten(datasets.map((dataset) => dataset.dataUse?.primary))).map((dataUse) => dataUse.code)); @@ -32,7 +32,7 @@ export const DatasetFilterList = (props) => { const category = 'accessManagement'; return ( - filterHandler(event, datasets, category, filter, searchRef.current.value)}> + filterHandler(event, datasets, category, filter)}> @@ -53,7 +53,7 @@ export const DatasetFilterList = (props) => { const category = 'dataUse'; return ( - filterHandler(event, datasets, category, filter, searchRef.current.value)}> + filterHandler(event, datasets, category, filter)}> diff --git a/src/components/data_search/DatasetSearchTable.jsx b/src/components/data_search/DatasetSearchTable.jsx index 6d53d55c7..834bf4e58 100644 --- a/src/components/data_search/DatasetSearchTable.jsx +++ b/src/components/data_search/DatasetSearchTable.jsx @@ -34,6 +34,7 @@ const styles = { const defaultFilters = { accessManagement: [], dataUse: [], + search: [] }; export const DatasetSearchTable = (props) => { @@ -43,7 +44,7 @@ export const DatasetSearchTable = (props) => { const [filtered, setFiltered] = useState([]); const [selected, setSelected] = useState([]); const [selectedTable, setSelectedTable] = useState(datasetSearchTableTabs.study); - const searchRef = useRef(''); + const [searchTerm, setSearchTerm] = useState(''); const isFiltered = (filter, category) => (filters[category]).indexOf(filter) > -1; const numSelectedFilters = (filters) => Object.values(filters).reduce((sum, array) => sum + array.length, 0); @@ -62,7 +63,7 @@ export const DatasetSearchTable = (props) => { } }; - const assembleFullQuery = (searchTerm, filters) => { + const assembleFullQuery = (filters) => { const queryChunks = [ { 'match': { @@ -77,11 +78,11 @@ export const DatasetSearchTable = (props) => { ]; // do not apply search modifier if there is no searchTerm - if (searchTerm !== '') { + if (filters.search.length > 0) { const searchModifier = [ { 'multi_match': { - 'query': searchTerm, + 'query': filters.search[filters.search.length - 1], 'type':'phrase_prefix', 'fields': [ 'datasetName', @@ -159,17 +160,20 @@ export const DatasetSearchTable = (props) => { } }; - const filterHandler = (event, data, category, filter, searchTerm) => { + const filterHandler = (event, data, category, filter) => { var newFilters = defaultFilters; + if (category === 'search') { + newFilters[category] = []; + setSearchTerm(filter); + } if (!isFiltered(filter, category) && filter !== '') { newFilters[category] = filters[category].concat(filter); } else { newFilters[category] = filters[category].filter((f) => f !== filter); } setFilters(newFilters); - - const fullQuery = assembleFullQuery(searchTerm, newFilters); - const search = async () => { + const fullQuery = assembleFullQuery(newFilters); + const doSearch = async () => { try { await DataSet.searchDatasetIndex(fullQuery).then((filteredDatasets) => { var newFiltered = datasets.filter(value => filteredDatasets.some(item => _.isEqual(item, value))); @@ -179,18 +183,13 @@ export const DatasetSearchTable = (props) => { Notifications.showError({ text: 'Failed to load Elasticsearch index' }); } }; - search(); + doSearch(); }; const applyForAccess = async () => { const darDraft = await DAR.postDarDraft({ datasetId: selected }); history.push(`/dar_application/${darDraft.referenceId}`); }; - const clearSearchRef = () => { - searchRef.current.value = ''; - filterHandler(null, datasets, '', ''); - }; - useEffect(() => { if (isEmpty(filtered)) { return; @@ -222,12 +221,12 @@ export const DatasetSearchTable = (props) => { fontFamily: 'Montserrat', fontSize: '1.5rem' }} - onChange={() => filterHandler(null, datasets, '', searchRef.current.value)} - ref={searchRef} + value={searchTerm} + onChange={() => filterHandler(null, datasets, 'search', event.target.value)} />
- @@ -253,7 +252,7 @@ export const DatasetSearchTable = (props) => { - + {(() => {