@@ -85,6 +85,31 @@ function prepareSearchResults(listObjData, displayFieldMeta) {
8585 return searchResults ;
8686}
8787
88+ function handleGroupedDataSearch ( dataApiObj , searchText , clickedGroup , initialCaseClass , itemsTree ) {
89+ const localDataApiObj = cloneDeep ( dataApiObj ) ;
90+ localDataApiObj . fetchedNQData = false ;
91+ localDataApiObj . cache = { } ;
92+
93+ if ( ! searchText && ! clickedGroup ) {
94+ return { shouldReturn : true , value : itemsTree } ;
95+ }
96+
97+ localDataApiObj . parameters [ Object . keys ( localDataApiObj . parameters ) [ 1 ] ] = searchText ;
98+ localDataApiObj . parameters [ Object . keys ( localDataApiObj . parameters ) [ 0 ] ] = initialCaseClass ;
99+
100+ if ( clickedGroup ) {
101+ if ( ! searchText ) {
102+ const containsData = itemsTree . find ( item => item . id === clickedGroup ) ;
103+ if ( containsData ?. items ?. length ) {
104+ return { shouldReturn : true , value : itemsTree } ;
105+ }
106+ }
107+ localDataApiObj . parameters [ Object . keys ( localDataApiObj . parameters ) [ 0 ] ] = JSON . stringify ( [ clickedGroup ] ) ;
108+ }
109+
110+ return { shouldReturn : false , value : localDataApiObj } ;
111+ }
112+
88113async function doSearch (
89114 searchText ,
90115 clickedGroup ,
@@ -96,61 +121,44 @@ async function doSearch(
96121 showSecondaryInSearchOnly ,
97122 selected
98123) {
99- let searchTextForUngroupedData = '' ;
100- if ( dataApiObj ) {
101- // creating dataApiObject in grouped data cases
102- if ( isGroupData ) {
103- dataApiObj = cloneDeep ( dataApiObj ) ;
104- dataApiObj . fetchedNQData = false ;
105- dataApiObj . cache = { } ;
106-
107- // if we have no search text and no group selected, return the original tree
108- if ( searchText === '' && clickedGroup === '' ) {
109- return itemsTree ;
110- }
124+ if ( ! dataApiObj ) {
125+ return itemsTree ;
126+ }
111127
112- // setting the inital search text & search classes in ApiObject
113- dataApiObj . parameters [ Object . keys ( dataApiObj . parameters ) [ 1 ] ] = searchText ;
114- dataApiObj . parameters [ Object . keys ( dataApiObj . parameters ) [ 0 ] ] = initialCaseClass ;
115-
116- // if we have a selected group
117- if ( clickedGroup ) {
118- // check if the data for this group is already present and no search text
119- if ( searchText === '' ) {
120- const containsData = itemsTree . find ( item => item . id === clickedGroup ) ;
121- // do not make API call when items of respective group are already fetched
122- if ( containsData ?. items ?. length ) return itemsTree ;
123- }
128+ let searchTextForApi = '' ;
129+ let localDataApiObj = dataApiObj ;
124130
125- dataApiObj . parameters [ Object . keys ( dataApiObj . parameters ) [ 0 ] ] = JSON . stringify ( [ clickedGroup ] ) ;
126- }
127- } else {
128- searchTextForUngroupedData = searchText ;
131+ if ( isGroupData ) {
132+ const groupResult = handleGroupedDataSearch ( dataApiObj , searchText , clickedGroup , initialCaseClass , itemsTree ) ;
133+ if ( groupResult . shouldReturn ) {
134+ return groupResult . value ;
129135 }
136+ localDataApiObj = groupResult . value ;
137+ } else {
138+ searchTextForApi = searchText ;
139+ }
130140
131- // search API call
132- const response = await dataApiObj . fetchData ( searchTextForUngroupedData ) . catch ( ( ) => {
133- return itemsTree ;
134- } ) ;
141+ const response = await localDataApiObj . fetchData ( searchTextForApi ) . catch ( ( ) => ( { data : undefined } ) ) ;
135142
136- let listObjData = response . data ;
137- let newItemsTree = [ ] ;
138- if ( isGroupData ) {
139- if ( searchText ) {
140- listObjData = prepareSearchResults ( listObjData , displayFieldMeta ) ;
141- } else {
142- newItemsTree = putItemsDataInItemsTree ( listObjData , displayFieldMeta , itemsTree , showSecondaryInSearchOnly , selected ) ;
143- return newItemsTree ;
144- }
145- }
146- const showSecondaryData = showSecondaryInSearchOnly ? ! ! searchText : true ;
147- if ( listObjData !== undefined && listObjData . length > 0 ) {
148- newItemsTree = listObjData . map ( entry => createSingleTreeObejct ( entry , displayFieldMeta , showSecondaryData , selected ) ) ;
143+ let listObjData = response . data ;
144+ if ( ! listObjData ) {
145+ return itemsTree ;
146+ }
147+
148+ if ( isGroupData ) {
149+ if ( searchText ) {
150+ listObjData = prepareSearchResults ( listObjData , displayFieldMeta ) ;
151+ } else {
152+ return putItemsDataInItemsTree ( listObjData , displayFieldMeta , itemsTree , showSecondaryInSearchOnly , selected ) ;
149153 }
150- return newItemsTree ;
151154 }
152155
153- return itemsTree ;
156+ if ( listObjData . length === 0 ) {
157+ return [ ] ;
158+ }
159+
160+ const showSecondaryData = showSecondaryInSearchOnly ? ! ! searchText : true ;
161+ return listObjData . map ( entry => createSingleTreeObejct ( entry , displayFieldMeta , showSecondaryData , selected ) ) ;
154162}
155163
156164function setValuesToPropertyList ( searchText , assocProp , items , columns , actions , updatePropertyInRedux = true ) {
0 commit comments