From 455cfe91b8038c258b7aba81d4a78a3c3b698335 Mon Sep 17 00:00:00 2001 From: fboulnois Date: Fri, 1 Nov 2024 12:29:13 -0400 Subject: [PATCH] [DT-900] Get search working again (#2703) --- .../data_search/DatasetFilterList.jsx | 6 ++-- .../data_search/DatasetSearchTable.jsx | 36 +++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) 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..b83640dd7 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': { @@ -76,12 +77,12 @@ export const DatasetSearchTable = (props) => { } ]; - // do not apply search modifier if there is no searchTerm - if (searchTerm !== '') { + // do not apply search modifier if there is no search term + if (filters.search.length > 0) { const searchModifier = [ { 'multi_match': { - 'query': searchTerm, + 'query': filters.search[filters.search.length - 1], 'type':'phrase_prefix', 'fields': [ 'datasetName', @@ -159,8 +160,12 @@ 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 { @@ -168,8 +173,8 @@ export const DatasetSearchTable = (props) => { } 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 +184,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 +222,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 +253,7 @@ export const DatasetSearchTable = (props) => { - + {(() => {