-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseLoadSearchResults.ts
More file actions
80 lines (64 loc) · 2.84 KB
/
useLoadSearchResults.ts
File metadata and controls
80 lines (64 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { useEffect, useRef } from 'react';
import { SearchQueryParams } from '@common/constants/routes.constants';
import { SEARCH_RESULTS_LIMIT, SearchIdentifiers } from '@common/constants/search.constants';
import { useLoadingState, useSearchState } from '@src/store';
import { useSearchContext } from './useSearchContext';
export const useLoadSearchResults = (
fetchData: ({ query, searchBy, offset, selectedSegment, baseQuerySelector }: FetchDataParams) => Promise<void>,
) => {
const { hasSearchParams, defaultSearchBy, defaultQuery, getSearchSourceData, getSearchFacetsData } =
useSearchContext();
const { setIsLoading } = useLoadingState();
const { setQuery, setData, setSearchBy, forceRefresh, setForceRefresh, resetFacetsData } = useSearchState();
const searchParams = new URLSearchParams(window.location.search);
const queryParam = searchParams.get(SearchQueryParams.Query);
const searchByParam = searchParams.get(SearchQueryParams.SearchBy);
const offsetParam = searchParams.get(SearchQueryParams.Offset);
const prevSearchParams = useRef<{ query: string | null; searchBy: string | null; offset: string | number | null }>({
query: null,
searchBy: null,
offset: null,
});
useEffect(() => {
if (!hasSearchParams) return;
async function makeSearch() {
const { query: prevQuery, searchBy: prevSearchBy, offset: prevOffset } = prevSearchParams.current;
if (prevQuery === queryParam && prevSearchBy === searchByParam && prevOffset === offsetParam && !forceRefresh)
return;
if (searchByParam && prevSearchBy !== searchByParam) {
setSearchBy(searchByParam as SearchIdentifiers);
}
if (!queryParam) {
setData(null);
return;
}
// Sets query's value for Basic search
if (searchByParam && prevQuery !== queryParam) {
setQuery(queryParam);
}
await fetchData({
query: queryParam,
searchBy: searchByParam as SearchIdentifiers,
offset: offsetParam ? parseInt(offsetParam) * SEARCH_RESULTS_LIMIT : 0,
});
setForceRefresh(false);
prevSearchParams.current = { query: queryParam, searchBy: searchByParam, offset: offsetParam };
}
makeSearch();
}, [hasSearchParams, queryParam, searchByParam, offsetParam, forceRefresh]);
// Load source, facets data and search results when the Search module is loaded with the default "Search By" and "Query" values.
// This is used for Complex Lookup field if it has the selected value.
useEffect(() => {
async function onLoad() {
setIsLoading(true);
await getSearchSourceData?.();
await getSearchFacetsData?.();
if (defaultSearchBy && defaultQuery) {
await fetchData({ query: defaultQuery, searchBy: defaultSearchBy, offset: 0 });
}
setIsLoading(false);
}
onLoad();
return resetFacetsData;
}, []);
};