@@ -17,6 +17,7 @@ import api from "../../services/api";
17
17
import staticData from "../../services/static-data" ;
18
18
19
19
import style from "./style.module.scss" ;
20
+ import _ from "lodash" ;
20
21
21
22
const colorIterator = makeColorIterator ( avatarColors ) ;
22
23
@@ -61,6 +62,7 @@ export default function SearchGlobal({ keyword }) {
61
62
const dropdownRef = React . useRef ( null ) ;
62
63
63
64
const prevOrderBy = usePrevious ( orderBy ) ;
65
+ const [ prevCriteria , setPrevCriteria ] = React . useState ( null ) ;
64
66
65
67
const usersPerPage = config . ITEMS_PER_PAGE ;
66
68
@@ -129,79 +131,88 @@ export default function SearchGlobal({ keyword }) {
129
131
return ;
130
132
}
131
133
132
- let isSubscribed = true ;
133
- let source = axios . CancelToken . source ( ) ;
134
-
135
- ( async ( ) => {
136
- const criteria = { } ;
137
- let headers ;
138
- let data ;
139
-
140
- setIsSearching ( true ) ;
141
- setUsers ( [ ] ) ;
142
-
143
- if (
144
- searchContext . filters [ FILTERS . LOCATIONS ] . active &&
145
- searchContext . selectedLocations . length > 0
146
- ) {
147
- criteria . locations = searchContext . selectedLocations ;
148
- }
149
- if (
150
- searchContext . filters [ FILTERS . SKILLS ] . active &&
151
- searchContext . selectedSkills . length > 0
152
- ) {
153
- criteria . skills = searchContext . selectedSkills ;
154
- }
134
+ const criteria = { } ;
135
+ if (
136
+ searchContext . filters [ FILTERS . LOCATIONS ] . active &&
137
+ searchContext . selectedLocations . length > 0
138
+ ) {
139
+ criteria . locations = searchContext . selectedLocations ;
140
+ }
141
+ if (
142
+ searchContext . filters [ FILTERS . SKILLS ] . active &&
143
+ searchContext . selectedSkills . length > 0
144
+ ) {
145
+ criteria . skills = searchContext . selectedSkills ;
146
+ }
147
+ if (
148
+ searchContext . filters [ FILTERS . ACHIEVEMENTS ] . active &&
149
+ searchContext . selectedAchievements . length > 0
150
+ ) {
151
+ criteria . achievements = searchContext . selectedAchievements ;
152
+ }
153
+ if ( searchContext . filters [ FILTERS . AVAILABILITY ] . active ) {
155
154
if (
156
- searchContext . filters [ FILTERS . ACHIEVEMENTS ] . active &&
157
- searchContext . selectedAchievements . length > 0
155
+ searchContext . selectedAvailability &&
156
+ ( "isAvailableSelected" in searchContext . selectedAvailability ||
157
+ "isUnavailableSelected" in searchContext . selectedAvailability )
158
158
) {
159
- criteria . achievements = searchContext . selectedAchievements ;
160
- }
161
- if ( searchContext . filters [ FILTERS . AVAILABILITY ] . active ) {
159
+ const availabilityFilter = searchContext . selectedAvailability ;
162
160
if (
163
- searchContext . selectedAvailability &&
164
- ( "isAvailableSelected" in searchContext . selectedAvailability ||
165
- "isUnavailableSelected" in searchContext . selectedAvailability )
161
+ availabilityFilter . isAvailableSelected &&
162
+ ! availabilityFilter . isUnavailableSelected
166
163
) {
167
- const availabilityFilter = searchContext . selectedAvailability ;
168
- if (
169
- availabilityFilter . isAvailableSelected &&
170
- ! availabilityFilter . isUnavailableSelected
171
- ) {
172
- criteria . isAvailable = true ;
173
- } else if (
174
- ! availabilityFilter . isAvailableSelected &&
175
- availabilityFilter . isUnavailableSelected
176
- ) {
177
- criteria . isAvailable = false ;
178
- }
179
- }
180
- }
181
-
182
- criteria . attributes = [ ] ;
183
- searchContext . getCompanyAttrActiveFilter ( ) . forEach ( ( filter ) => {
184
- if (
185
- searchContext . selectedCompanyAttributes [ filter . id ] &&
186
- searchContext . selectedCompanyAttributes [ filter . id ] . length > 0
164
+ criteria . isAvailable = true ;
165
+ } else if (
166
+ ! availabilityFilter . isAvailableSelected &&
167
+ availabilityFilter . isUnavailableSelected
187
168
) {
188
- criteria . attributes . push ( {
189
- id : filter . id ,
190
- value : searchContext . selectedCompanyAttributes [ filter . id ] . map (
191
- ( data ) => data . value
192
- ) ,
193
- } ) ;
169
+ criteria . isAvailable = false ;
194
170
}
195
- } ) ;
196
-
197
- // reset first page when change orderBy
198
- if ( prevOrderBy !== "undefined" && prevOrderBy !== orderBy ) {
199
- searchContext . pagination . page = 1 ;
200
171
}
172
+ }
201
173
202
- if ( searchContext . pagination . page !== page ) {
203
- setPage ( searchContext . pagination . page ) ;
174
+ criteria . attributes = [ ] ;
175
+ searchContext . getCompanyAttrActiveFilter ( ) . forEach ( ( filter ) => {
176
+ if (
177
+ searchContext . selectedCompanyAttributes [ filter . id ] &&
178
+ searchContext . selectedCompanyAttributes [ filter . id ] . length > 0
179
+ ) {
180
+ criteria . attributes . push ( {
181
+ id : filter . id ,
182
+ value : searchContext . selectedCompanyAttributes [ filter . id ] . map (
183
+ ( data ) => data . value
184
+ ) ,
185
+ } ) ;
204
186
}
187
+ } ) ;
188
+
189
+ // reset first page when change orderBy or criteria
190
+ if ( ( prevOrderBy !== "undefined" && prevOrderBy !== orderBy )
191
+ || _ . isEqual ( prevCriteria , criteria ) === false ) {
192
+ searchContext . pagination . page = 1 ;
193
+ }
194
+
195
+ let pageChanged = false ;
196
+ if ( searchContext . pagination . page !== page ) {
197
+ setPage ( searchContext . pagination . page ) ;
198
+ pageChanged = true ;
199
+ }
200
+
201
+ if ( _ . isEqual ( prevCriteria , criteria ) && ! pageChanged ) {
202
+ return ;
203
+ } else {
204
+ setPrevCriteria ( criteria ) ;
205
+ }
206
+
207
+ let isSubscribed = true ;
208
+ let source = axios . CancelToken . source ( ) ;
209
+
210
+ ( async ( ) => {
211
+ let headers ;
212
+ let data ;
213
+
214
+ setIsSearching ( true ) ;
215
+ setUsers ( [ ] ) ;
205
216
206
217
const { url, options, body } = helper . getSearchUsersRequestDetails ( {
207
218
keyword,
@@ -245,11 +256,6 @@ export default function SearchGlobal({ keyword }) {
245
256
setTotalPages ( Number ( headers [ "x-total-pages" ] ) ) ;
246
257
}
247
258
} ) ( ) ;
248
-
249
- return ( ) => {
250
- isSubscribed = false ;
251
- source . cancel ( "Cancelling in cleanup" ) ;
252
- } ;
253
259
// eslint-disable-next-line react-hooks/exhaustive-deps
254
260
} , [ isLoading , isAuthenticated , keyword , orderBy , searchContext ] ) ;
255
261
0 commit comments