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-779] Clear selected filters code #2725

Merged
merged 7 commits into from
Nov 15, 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
29 changes: 17 additions & 12 deletions src/components/data_search/DatasetFilterList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import ListItemButton from '@mui/material/ListItemButton';
import ListItemIcon from '@mui/material/ListItemIcon';
import ListItemText from '@mui/material/ListItemText';
import Divider from '@mui/material/Divider';
import { Typography } from '@mui/material';
import { Button, Typography } from '@mui/material';
import { Checkbox } from '@mui/material';
import { flatten, uniq, compact, capitalize, orderBy } from 'lodash';

export const FilterItemHeader = (props) => {
const { title, headerStyle = { fontFamily: 'Montserrat', fontWeight: '600', marginTop: '1em' } } = props;
return (
<Typography variant="h6" gutterBottom component="div" sx={headerStyle}>
<Typography variant='h6' gutterBottom component='div' sx={headerStyle}>
{title}
</Typography>
);
}
};

export const FilterItemList = (props) => {
const { category, datasets, filter, filterHandler, isFiltered, filterNameFn } = props;
Expand All @@ -43,29 +43,34 @@ export const FilterItemList = (props) => {
};

export const DatasetFilterList = (props) => {
const { datasets, filters, filterHandler, isFiltered } = props;
const { datasets, filters, filterHandler, isFiltered, onClear } = 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));
const dacFilters = orderBy(uniq(compact(datasets.map((dataset) => dataset.dac?.dacName))), (dac) => dac.toLowerCase(), 'asc');

return (
<Box sx={{ bgcolor: 'background.paper' }}>
<Typography variant="h5" gutterBottom component="div" sx={{ fontFamily: 'Montserrat', fontWeight: '600' }}>
Filters
</Typography>
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
<Typography variant='h5' gutterBottom component='div' sx={{ fontFamily: 'Montserrat', fontWeight: '600' }}>
Filters
</Typography>
<Button onClick={onClear}>
Clear Filters
</Button>
</Box>
<Divider />
<FilterItemHeader title="Access Type" />
<FilterItemHeader title='Access Type' />
<FilterItemList
category="accessManagement"
category='accessManagement'
datasets={datasets}
filter={accessManagementFilters}
filterHandler={filterHandler}
isFiltered={isFiltered}
filterNameFn={capitalize} />
<FilterItemHeader title="Data Use" />
<FilterItemHeader title='Data Use' />
<FilterItemList
category="dataUse"
category='dataUse'
datasets={datasets}
filter={dataUseFilters}
filterHandler={filterHandler}
Expand All @@ -77,7 +82,7 @@ export const DatasetFilterList = (props) => {
headerStyle={{ fontFamily: 'Montserrat', marginTop: '1em' }}
/>
<FilterItemList
category="dac"
category='dac'
datasets={datasets}
filter={dacFilters}
filterHandler={filterHandler}
Expand Down
56 changes: 24 additions & 32 deletions src/components/data_search/DatasetSearchTable.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Tab from '@mui/material/Tab';
import Tabs from '@mui/material/Tabs';
import useOnMount from '@mui/utils/useOnMount'
import useOnMount from '@mui/utils/useOnMount';
import * as React from 'react';
import { Box, Button } from '@mui/material';
import { useEffect, useState } from 'react';
Expand Down Expand Up @@ -35,15 +35,14 @@ const styles = {
const defaultFilters = {
accessManagement: [],
dataUse: [],
dac: [],
search: []
dac: []
};

export const DatasetSearchTable = (props) => {
const { datasets, history, icon, title } = props;
const [exportableDatasets, setExportableDatasets] = useState({});
const [filters, setFilters] = useState(defaultFilters);
const [filtered, setFiltered] = useState([]);
const [filters, setFilters] = useState(_.clone(defaultFilters));
const [filtered, setFiltered] = useState(datasets);
const [selected, setSelected] = useState([]);
const [selectedTable, setSelectedTable] = useState(datasetSearchTableTabs.study);
const [searchTerm, setSearchTerm] = useState('');
Expand All @@ -65,7 +64,7 @@ export const DatasetSearchTable = (props) => {
}
};

const assembleFullQuery = (filters) => {
const assembleFullQuery = () => {
const queryChunks = [
{
'match': {
Expand All @@ -80,11 +79,11 @@ export const DatasetSearchTable = (props) => {
];

// do not apply search modifier if there is no search term
if (filters.search.length > 0) {
if (searchTerm.length > 0) {
const searchModifier = [
{
'multi_match': {
'query': filters.search[filters.search.length - 1],
'query': searchTerm,
'type':'phrase_prefix',
'fields': [
'datasetName',
Expand Down Expand Up @@ -140,7 +139,7 @@ export const DatasetSearchTable = (props) => {
'dac.dacName': term
}
}))
}
}
});

if (filterTerms.length > 0) {
Expand Down Expand Up @@ -168,31 +167,15 @@ export const DatasetSearchTable = (props) => {
};

const filterHandler = (event, data, category, filter) => {
var newFilters = defaultFilters;
if (category === 'search') {
newFilters[category] = [];
setSearchTerm(filter);
}
var newFilters = _.clone(filters);
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(newFilters);
const doSearch = async () => {
try {
await DataSet.searchDatasetIndex(fullQuery).then((filteredDatasets) => {
var newFiltered = datasets.filter(value => filteredDatasets.some(item => _.isEqual(item, value)));
setFiltered(newFiltered);
});
} catch (error) {
Notifications.showError({ text: 'Failed to load Elasticsearch index' });
}
};
doSearch();
};

const applyForAccess = async () => {
try {
const draftResponse = await DAR.postDarDraft({ datasetId: selected });
Expand All @@ -216,8 +199,15 @@ export const DatasetSearchTable = (props) => {
});

useEffect(() => {
setFiltered(datasets);
}, [datasets]);
const fullQuery = assembleFullQuery();
try {
DataSet.searchDatasetIndex(fullQuery).then((filteredDatasets) => {
const newFiltered = datasets.filter(value => filteredDatasets.some(item => _.isEqual(item, value)));
setFiltered(newFiltered);
});
} catch (error) {
Notifications.showError({ text: 'Failed to load Elasticsearch index' });
} }, [filters, searchTerm]); // eslint-disable-line

return (
<>
Expand All @@ -240,11 +230,13 @@ export const DatasetSearchTable = (props) => {
fontSize: '1.5rem'
}}
value={searchTerm}
onChange={() => filterHandler(null, datasets, 'search', event.target.value)}
onChange={(event) => {
setSearchTerm(event.target.value);
}}
/>
<div/>
<Box sx={{ display: 'flex', flexDirection: 'row', justifyContent: 'flex-end', paddingLeft: '1em', height: '4rem' }}>
<Button variant='contained' onClick={() => {filterHandler(null, datasets, 'search', '');}} sx={{ width: '100px' }}>
<Button variant='contained' onClick={() => setSearchTerm('')} sx={{ width: '100px' }}>
Clear Search
</Button>
</Box>
Expand All @@ -270,7 +262,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}/>
<DatasetFilterList datasets={datasets} filters={filters} filterHandler={filterHandler} isFiltered={isFiltered} onClear={() => setFilters(defaultFilters)}/>
</Box>
<Box sx={{width: '85%', padding: '0 1em'}}>
{(() => {
Expand Down
Loading