File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import PropTypes from 'prop-types' ;
2- import { useState } from 'react' ;
2+ import { useMemo } from 'react' ;
33import { FormattedMessage } from 'react-intl' ;
44
55import {
@@ -16,7 +16,7 @@ const FilterFilters = ({
1616 activeFilters = { type : [ ] } ,
1717 filterHandlers,
1818} ) => {
19- const [ filterState ] = useState ( ( ) => buildFilterState ( filterConfig ) ) ;
19+ const filterState = useMemo ( ( ) => buildFilterState ( filterConfig ) , [ ] ) ;
2020
2121 const renderCheckboxFilter = ( key , props ) => {
2222 const groupFilters = activeFilters [ key ] || [ ] ;
Original file line number Diff line number Diff line change 11import PropTypes from 'prop-types' ;
2- import { useState } from 'react' ;
2+ import { useMemo } from 'react' ;
33import { FormattedMessage } from 'react-intl' ;
44
55import {
@@ -24,10 +24,11 @@ const CollectionFilters = ({
2424 filterData,
2525 ...props
2626} ) => {
27- const [ filterState ] = useState ( ( ) => buildFilterState (
27+ const filterState = useMemo (
2828 // skip for mdSource filter as it is dynamic and handled separately
29- filterConfig . filter ( f => f . name !== 'mdSource' )
30- ) ) ;
29+ ( ) => buildFilterState ( filterConfig . filter ( f => f . name !== 'mdSource' ) ) ,
30+ [ ]
31+ ) ;
3132
3233 const renderCheckboxFilter = ( key ) => {
3334 const groupFilters = activeFilters [ key ] || [ ] ;
Original file line number Diff line number Diff line change 11import PropTypes from 'prop-types' ;
2- import { useState } from 'react' ;
2+ import { useMemo } from 'react' ;
33import { FormattedMessage } from 'react-intl' ;
44
55import {
@@ -20,7 +20,7 @@ const SourceFilters = ({
2020 filterHandlers,
2121 ...props
2222} ) => {
23- const [ filterState ] = useState ( ( ) => buildFilterState ( filterConfig ) ) ;
23+ const filterState = useMemo ( ( ) => buildFilterState ( filterConfig ) , [ ] ) ;
2424
2525 const renderCheckboxFilter = ( key ) => {
2626 const groupFilters = activeFilters [ key ] || [ ] ;
Original file line number Diff line number Diff line change 1+ import { buildFilterState } from './filterUtils' ;
2+
3+ describe ( 'buildFilterState' , ( ) => {
4+ it ( 'should build filter state from configs' , ( ) => {
5+ const configs = [
6+ {
7+ name : 'filter1' ,
8+ values : [
9+ { cql : 'cql1' , name : 'name1' } ,
10+ { cql : 'cql2' , name : 'name2' } ,
11+ ] ,
12+ } ,
13+ {
14+ name : 'filter2' ,
15+ values : [ { cql : 'cql3' , name : 'name3' } ] ,
16+ } ,
17+ ] ;
18+
19+ const expected = {
20+ filter1 : [
21+ { value : 'cql1' , label : 'name1' } ,
22+ { value : 'cql2' , label : 'name2' } ,
23+ ] ,
24+ filter2 : [ { value : 'cql3' , label : 'name3' } ] ,
25+ } ;
26+
27+ expect ( buildFilterState ( configs ) ) . toEqual ( expected ) ;
28+ } ) ;
29+
30+ it ( 'should handle empty configs' , ( ) => {
31+ const configs = [ ] ;
32+ const expected = { } ;
33+ expect ( buildFilterState ( configs ) ) . toEqual ( expected ) ;
34+ } ) ;
35+
36+ it ( 'should handle configs with empty values' , ( ) => {
37+ const configs = [
38+ {
39+ name : 'filter1' ,
40+ values : [ ] ,
41+ } ,
42+ ] ;
43+
44+ const expected = {
45+ filter1 : [ ] ,
46+ } ;
47+
48+ expect ( buildFilterState ( configs ) ) . toEqual ( expected ) ;
49+ } ) ;
50+ } ) ;
You can’t perform that action at this time.
0 commit comments