Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 8cabcb9

Browse files
Merge pull request #592 from topcoder-platform/issue-524
changes for #524
2 parents 9c405ca + e335881 commit 8cabcb9

File tree

1 file changed

+75
-69
lines changed

1 file changed

+75
-69
lines changed

client/src/pages/Search/Global.jsx

Lines changed: 75 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import api from "../../services/api";
1717
import staticData from "../../services/static-data";
1818

1919
import style from "./style.module.scss";
20+
import _ from "lodash";
2021

2122
const colorIterator = makeColorIterator(avatarColors);
2223

@@ -61,6 +62,7 @@ export default function SearchGlobal({ keyword }) {
6162
const dropdownRef = React.useRef(null);
6263

6364
const prevOrderBy = usePrevious(orderBy);
65+
const [prevCriteria, setPrevCriteria] = React.useState(null);
6466

6567
const usersPerPage = config.ITEMS_PER_PAGE;
6668

@@ -129,79 +131,88 @@ export default function SearchGlobal({ keyword }) {
129131
return;
130132
}
131133

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) {
155154
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)
158158
) {
159-
criteria.achievements = searchContext.selectedAchievements;
160-
}
161-
if (searchContext.filters[FILTERS.AVAILABILITY].active) {
159+
const availabilityFilter = searchContext.selectedAvailability;
162160
if (
163-
searchContext.selectedAvailability &&
164-
("isAvailableSelected" in searchContext.selectedAvailability ||
165-
"isUnavailableSelected" in searchContext.selectedAvailability)
161+
availabilityFilter.isAvailableSelected &&
162+
!availabilityFilter.isUnavailableSelected
166163
) {
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
187168
) {
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;
194170
}
195-
});
196-
197-
// reset first page when change orderBy
198-
if (prevOrderBy !== "undefined" && prevOrderBy !== orderBy) {
199-
searchContext.pagination.page = 1;
200171
}
172+
}
201173

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+
});
204186
}
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([]);
205216

206217
const { url, options, body } = helper.getSearchUsersRequestDetails({
207218
keyword,
@@ -245,11 +256,6 @@ export default function SearchGlobal({ keyword }) {
245256
setTotalPages(Number(headers["x-total-pages"]));
246257
}
247258
})();
248-
249-
return () => {
250-
isSubscribed = false;
251-
source.cancel("Cancelling in cleanup");
252-
};
253259
// eslint-disable-next-line react-hooks/exhaustive-deps
254260
}, [isLoading, isAuthenticated, keyword, orderBy, searchContext]);
255261

0 commit comments

Comments
 (0)