1
- import React , { memo , useState } from 'react' ;
1
+ import React , { memo , useState , useEffect , useCallback } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import { Popper , Tooltip , IconButton } from '@material-ui/core' ;
4
4
import { Close , Delete } from '@material-ui/icons' ;
5
5
import Grid from '@material-ui/core/Grid' ;
6
6
import MoleculeListSortFilterItem from './moleculeListSortFilterItem' ;
7
7
import WarningIcon from '@material-ui/icons/Warning' ;
8
8
import { makeStyles } from '@material-ui/styles' ;
9
- import { useDispatch , useSelector } from 'react-redux' ;
9
+ import { useDispatch } from 'react-redux' ;
10
10
import { MOL_ATTRIBUTES } from './redux/constants' ;
11
11
import { setFilter } from '../../../reducers/selection/actions' ;
12
12
import { Panel } from '../../common/Surfaces/Panel' ;
@@ -62,6 +62,7 @@ export const getFilteredMoleculesCount = (molecules, filter) => {
62
62
for ( let molecule of molecules ) {
63
63
let add = true ; // By default molecule passes filter
64
64
for ( let attr of MOL_ATTRIBUTES ) {
65
+ if ( ! attr . filter ) continue ;
65
66
const lowAttr = attr . key . toLowerCase ( ) ;
66
67
const attrValue = molecule [ lowAttr ] ;
67
68
if ( attrValue < filter . filter [ attr . key ] . minValue || attrValue > filter . filter [ attr . key ] . maxValue ) {
@@ -129,35 +130,18 @@ export const filterMolecules = (molecules, filter) => {
129
130
130
131
export const MoleculeListSortFilterDialog = memo (
131
132
( {
132
- molGroupSelection,
133
- cachedMolList,
134
133
filter,
135
134
anchorEl,
136
135
open,
137
136
parentID = 'default' ,
138
137
placement = 'right-start' ,
139
- setSortDialogAnchorEl
138
+ setSortDialogAnchorEl,
139
+ joinedMoleculeLists
140
140
} ) => {
141
141
let classes = useStyles ( ) ;
142
142
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
- }
156
143
157
- return molecules ;
158
- } ;
159
-
160
- const initialize = ( ) => {
144
+ const initialize = useCallback ( ( ) => {
161
145
let initObject = {
162
146
active : false ,
163
147
predefined : 'none' ,
@@ -169,9 +153,8 @@ export const MoleculeListSortFilterDialog = memo(
169
153
const lowAttr = attr . key . toLowerCase ( ) ;
170
154
let minValue = - 999999 ;
171
155
let maxValue = 0 ;
172
- const moleculeList = getListedMolecules ( ) ;
173
156
174
- moleculeList . forEach ( molecule => {
157
+ joinedMoleculeLists . forEach ( molecule => {
175
158
const attrValue = molecule [ lowAttr ] ;
176
159
if ( attrValue > maxValue ) maxValue = attrValue ;
177
160
if ( minValue === - 999999 ) minValue = maxValue ;
@@ -187,31 +170,70 @@ export const MoleculeListSortFilterDialog = memo(
187
170
} ;
188
171
}
189
172
return initObject ;
190
- } ;
191
-
192
- const [ initState ] = useState ( initialize ( ) ) ;
173
+ } , [ joinedMoleculeLists ] ) ;
193
174
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 ) ) ;
197
177
const [ predefinedFilter , setPredefinedFilter ] = useState ( filter . predefined ) ;
198
178
199
- const handleFilterChange = filter => {
179
+ const handleFilterChange = useCallback ( filter => {
200
180
const filterSet = Object . assign ( { } , filter ) ;
201
181
for ( let attr of MOL_ATTRIBUTES ) {
202
182
if ( filterSet . filter [ attr . key ] . priority === undefined || filterSet . filter [ attr . key ] . priority === '' ) {
203
183
filterSet . filter [ attr . key ] . priority = 0 ;
204
184
}
205
185
}
206
186
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 ] ) ;
208
231
209
232
const handleItemChange = key => setting => {
210
233
let newFilter = Object . assign ( { } , filter ) ;
211
234
newFilter . filter [ key ] = setting ;
212
235
newFilter . active = true ;
213
236
dispatch ( setFilter ( newFilter ) ) ;
214
- setFilteredCount ( getFilteredMoleculesCount ( getListedMolecules ( ) , newFilter ) ) ;
215
237
handleFilterChange ( newFilter ) ;
216
238
} ;
217
239
@@ -235,7 +257,6 @@ export const MoleculeListSortFilterDialog = memo(
235
257
const resetFilter = initialize ( ) ;
236
258
setPredefinedFilter ( 'none' ) ;
237
259
dispatch ( setFilter ( resetFilter ) ) ;
238
- setFilteredCount ( getFilteredMoleculesCount ( getListedMolecules ( ) , resetFilter ) ) ;
239
260
handleFilterChange ( resetFilter ) ;
240
261
} ;
241
262
@@ -338,8 +359,6 @@ export const MoleculeListSortFilterDialog = memo(
338
359
) ;
339
360
340
361
MoleculeListSortFilterDialog . propTypes = {
341
- molGroupSelection : PropTypes . arrayOf ( PropTypes . number ) . isRequired ,
342
- cachedMolList : PropTypes . object . isRequired ,
343
362
filter : PropTypes . object ,
344
363
setFilter : PropTypes . func ,
345
364
anchorEl : PropTypes . object ,
0 commit comments