Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DT-900] Get search working again #2703

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/components/data_search/DatasetFilterList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -32,7 +32,7 @@ export const DatasetFilterList = (props) => {
const category = 'accessManagement';
return (
<ListItem disablePadding key={filter}>
<ListItemButton sx={{ padding: '0' }} onClick={(event) => filterHandler(event, datasets, category, filter, searchRef.current.value)}>
<ListItemButton sx={{ padding: '0' }} onClick={(event) => filterHandler(event, datasets, category, filter)}>
<ListItemIcon>
<Checkbox checked={isFiltered(filter, category)} />
</ListItemIcon>
Expand All @@ -53,7 +53,7 @@ export const DatasetFilterList = (props) => {
const category = 'dataUse';
return (
<ListItem disablePadding key={filter}>
<ListItemButton sx={{ padding: '0' }} onClick={(event) => filterHandler(event, datasets, category, filter, searchRef.current.value)}>
<ListItemButton sx={{ padding: '0' }} onClick={(event) => filterHandler(event, datasets, category, filter)}>
<ListItemIcon>
<Checkbox checked={isFiltered(filter, category)} />
</ListItemIcon>
Expand Down
36 changes: 18 additions & 18 deletions src/components/data_search/DatasetSearchTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const styles = {
const defaultFilters = {
accessManagement: [],
dataUse: [],
search: []
};

export const DatasetSearchTable = (props) => {
Expand All @@ -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);
Expand All @@ -62,7 +63,7 @@ export const DatasetSearchTable = (props) => {
}
};

const assembleFullQuery = (searchTerm, filters) => {
const assembleFullQuery = (filters) => {
const queryChunks = [
{
'match': {
Expand All @@ -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],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change if searchTerm is being set in filterHandler?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reasons I don't completely understand searchTerm at this line reflects the previous searchTerm state (for example if your current input was hello, searchTerm would be hell)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if something similar could be done everywhere searchTerm is used to eliminate the need for that variable at all - not sure if that would work right though

'type':'phrase_prefix',
'fields': [
'datasetName',
Expand Down Expand Up @@ -159,17 +160,21 @@ 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)));
Expand All @@ -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;
Expand Down Expand Up @@ -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)}
/>
<div/>
<Box sx={{ display: 'flex', flexDirection: 'row', justifyContent: 'flex-end', paddingLeft: '1em', height: '4rem' }}>
<Button variant="contained" onClick={clearSearchRef} sx={{ width: '100px' }}>
<Button variant="contained" onClick={() => {filterHandler(null, datasets, 'search', '')}} sx={{ width: '100px' }}>
Clear Search
</Button>
</Box>
Expand All @@ -253,7 +253,7 @@ export const DatasetSearchTable = (props) => {
</Box>
<Box sx={{display: 'flex', flexDirection: 'row', paddingTop: '2em'}}>
<Box sx={{width: '14%', padding: '0 1em'}}>
<DatasetFilterList datasets={datasets} filters={filters} filterHandler={filterHandler} isFiltered={isFiltered} searchRef={searchRef}/>
<DatasetFilterList datasets={datasets} filters={filters} filterHandler={filterHandler} isFiltered={isFiltered}/>
</Box>
<Box sx={{width: '85%', padding: '0 1em'}}>
{(() => {
Expand Down
Loading