Skip to content

Commit eb1edbd

Browse files
Merge pull request #224 from m2ms/#456
#456
2 parents 0e50fed + fa8ee46 commit eb1edbd

File tree

2 files changed

+68
-46
lines changed

2 files changed

+68
-46
lines changed

js/components/preview/molecule/moleculeList.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,18 @@ export const MoleculeList = memo(({ height, setFilterItemsHeight, filterItemsHei
277277
// setCurrentPage(0);
278278
}, [object_selection]);*/
279279

280-
let joinedMoleculeLists = [];
281-
if (searchString !== null) {
282-
joinedMoleculeLists = getAllMoleculeList.filter(molecule =>
283-
molecule.protein_code.toLowerCase().includes(searchString.toLowerCase())
284-
);
285-
} else {
286-
joinedMoleculeLists = getJoinedMoleculeList;
287-
}
280+
let joinedMoleculeLists = useMemo(() => {
281+
if (searchString) {
282+
return getAllMoleculeList.filter(molecule =>
283+
molecule.protein_code.toLowerCase().includes(searchString.toLowerCase())
284+
);
285+
} else {
286+
return getJoinedMoleculeList;
287+
}
288+
}, [getJoinedMoleculeList, getAllMoleculeList, searchString]);
289+
290+
// Used for MoleculeListSortFilterDialog when using textSearch
291+
const joinedMoleculeListsCopy = useMemo(() => [...joinedMoleculeLists], [joinedMoleculeLists]);
288292

289293
if (isActiveFilter) {
290294
joinedMoleculeLists = filterMolecules(joinedMoleculeLists, filter);
@@ -652,10 +656,9 @@ export const MoleculeList = memo(({ height, setFilterItemsHeight, filterItemsHei
652656
<MoleculeListSortFilterDialog
653657
open={sortDialogOpen}
654658
anchorEl={sortDialogAnchorEl}
655-
molGroupSelection={object_selection}
656-
cachedMolList={all_mol_lists}
657659
filter={filter}
658660
setSortDialogAnchorEl={setSortDialogAnchorEl}
661+
joinedMoleculeLists={joinedMoleculeListsCopy}
659662
/>
660663
)}
661664
<div ref={filterRef}>

js/components/preview/molecule/moleculeListSortFilterDialog.js

+55-36
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import React, { memo, useState } from 'react';
1+
import React, { memo, useState, useEffect, useCallback } from 'react';
22
import PropTypes from 'prop-types';
33
import { Popper, Tooltip, IconButton } from '@material-ui/core';
44
import { Close, Delete } from '@material-ui/icons';
55
import Grid from '@material-ui/core/Grid';
66
import MoleculeListSortFilterItem from './moleculeListSortFilterItem';
77
import WarningIcon from '@material-ui/icons/Warning';
88
import { makeStyles } from '@material-ui/styles';
9-
import { useDispatch, useSelector } from 'react-redux';
9+
import { useDispatch } from 'react-redux';
1010
import { MOL_ATTRIBUTES } from './redux/constants';
1111
import { setFilter } from '../../../reducers/selection/actions';
1212
import { Panel } from '../../common/Surfaces/Panel';
@@ -62,6 +62,7 @@ export const getFilteredMoleculesCount = (molecules, filter) => {
6262
for (let molecule of molecules) {
6363
let add = true; // By default molecule passes filter
6464
for (let attr of MOL_ATTRIBUTES) {
65+
if (!attr.filter) continue;
6566
const lowAttr = attr.key.toLowerCase();
6667
const attrValue = molecule[lowAttr];
6768
if (attrValue < filter.filter[attr.key].minValue || attrValue > filter.filter[attr.key].maxValue) {
@@ -129,35 +130,18 @@ export const filterMolecules = (molecules, filter) => {
129130

130131
export const MoleculeListSortFilterDialog = memo(
131132
({
132-
molGroupSelection,
133-
cachedMolList,
134133
filter,
135134
anchorEl,
136135
open,
137136
parentID = 'default',
138137
placement = 'right-start',
139-
setSortDialogAnchorEl
138+
setSortDialogAnchorEl,
139+
joinedMoleculeLists
140140
}) => {
141141
let classes = useStyles();
142142
const dispatch = useDispatch();
143-
const moleculeGroupList = useSelector(state => state.apiReducers.mol_group_list);
144-
145-
const getListedMolecules = () => {
146-
let molecules = [];
147-
for (let molGroupId of molGroupSelection) {
148-
// Selected molecule groups
149-
const molGroup = cachedMolList[molGroupId];
150-
if (molGroup) {
151-
molecules = molecules.concat(molGroup);
152-
} else {
153-
console.log(`Molecule group ${molGroupId} not found in cached list`);
154-
}
155-
}
156143

157-
return molecules;
158-
};
159-
160-
const initialize = () => {
144+
const initialize = useCallback(() => {
161145
let initObject = {
162146
active: false,
163147
predefined: 'none',
@@ -169,9 +153,8 @@ export const MoleculeListSortFilterDialog = memo(
169153
const lowAttr = attr.key.toLowerCase();
170154
let minValue = -999999;
171155
let maxValue = 0;
172-
const moleculeList = getListedMolecules();
173156

174-
moleculeList.forEach(molecule => {
157+
joinedMoleculeLists.forEach(molecule => {
175158
const attrValue = molecule[lowAttr];
176159
if (attrValue > maxValue) maxValue = attrValue;
177160
if (minValue === -999999) minValue = maxValue;
@@ -187,31 +170,70 @@ export const MoleculeListSortFilterDialog = memo(
187170
};
188171
}
189172
return initObject;
190-
};
191-
192-
const [initState] = useState(initialize());
173+
}, [joinedMoleculeLists]);
193174

194-
filter = filter || initState;
195-
196-
const [filteredCount, setFilteredCount] = useState(getFilteredMoleculesCount(getListedMolecules(), filter));
175+
const [initState, setInitState] = useState(initialize());
176+
const [filteredCount, setFilteredCount] = useState(getFilteredMoleculesCount(joinedMoleculeLists, filter));
197177
const [predefinedFilter, setPredefinedFilter] = useState(filter.predefined);
198178

199-
const handleFilterChange = filter => {
179+
const handleFilterChange = useCallback(filter => {
200180
const filterSet = Object.assign({}, filter);
201181
for (let attr of MOL_ATTRIBUTES) {
202182
if (filterSet.filter[attr.key].priority === undefined || filterSet.filter[attr.key].priority === '') {
203183
filterSet.filter[attr.key].priority = 0;
204184
}
205185
}
206186
dispatch(setFilter(filterSet));
207-
};
187+
}, [dispatch]);
188+
189+
useEffect(() => {
190+
const init = initialize();
191+
192+
setInitState(init);
193+
194+
if (!filter.active) {
195+
const initCopy = { ...init };
196+
dispatch(setFilter(initCopy));
197+
handleFilterChange(initCopy);
198+
}
199+
}, [initialize, dispatch, joinedMoleculeLists, handleFilterChange, filter.active]);
200+
201+
useEffect(() => {
202+
setFilteredCount(getFilteredMoleculesCount(joinedMoleculeLists, filter));
203+
}, [joinedMoleculeLists, filter]);
204+
205+
useEffect(() => {
206+
let changed = false;
207+
const newFilter = { ...filter };
208+
209+
for (let attr of MOL_ATTRIBUTES) {
210+
if (!attr.filter) continue;
211+
const key = attr.key;
212+
const filterValue = newFilter.filter[key];
213+
const initValue = initState.filter[key];
214+
215+
if (filterValue.minValue < initValue.minValue || filterValue.minValue > initValue.maxValue) {
216+
filterValue.minValue = initValue.minValue;
217+
changed = true;
218+
}
219+
220+
if (filterValue.maxValue > initValue.maxValue || filterValue.maxValue < initValue.minValue) {
221+
filterValue.maxValue = initValue.maxValue;
222+
changed = true;
223+
}
224+
}
225+
226+
if (changed) {
227+
dispatch(setFilter(newFilter));
228+
handleFilterChange(newFilter);
229+
}
230+
}, [initState, filter, dispatch, handleFilterChange]);
208231

209232
const handleItemChange = key => setting => {
210233
let newFilter = Object.assign({}, filter);
211234
newFilter.filter[key] = setting;
212235
newFilter.active = true;
213236
dispatch(setFilter(newFilter));
214-
setFilteredCount(getFilteredMoleculesCount(getListedMolecules(), newFilter));
215237
handleFilterChange(newFilter);
216238
};
217239

@@ -235,7 +257,6 @@ export const MoleculeListSortFilterDialog = memo(
235257
const resetFilter = initialize();
236258
setPredefinedFilter('none');
237259
dispatch(setFilter(resetFilter));
238-
setFilteredCount(getFilteredMoleculesCount(getListedMolecules(), resetFilter));
239260
handleFilterChange(resetFilter);
240261
};
241262

@@ -338,8 +359,6 @@ export const MoleculeListSortFilterDialog = memo(
338359
);
339360

340361
MoleculeListSortFilterDialog.propTypes = {
341-
molGroupSelection: PropTypes.arrayOf(PropTypes.number).isRequired,
342-
cachedMolList: PropTypes.object.isRequired,
343362
filter: PropTypes.object,
344363
setFilter: PropTypes.func,
345364
anchorEl: PropTypes.object,

0 commit comments

Comments
 (0)