Skip to content

Commit 8ce2251

Browse files
committed
refactor: Internal handling and execution of pagination
1 parent a6495d6 commit 8ce2251

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

src/hooks/useStacSearch.ts

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,43 +38,55 @@ function useStacSearch(stacApi: StacApi): StacSearchHook {
3838
const [ dateRangeTo, setDateRangeTo ] = useState<string>('');
3939
const [ state, setState ] = useState<LoadingState>('IDLE');
4040
const [ error, setError ] = useState<ApiError>();
41-
const [ nextPage, setNextPage ] = useState<PaginationHandler>();
42-
const [ previousPage, setPreviousPage ] = useState<PaginationHandler>();
4341

44-
const loadNewPage = useCallback(
45-
(link: Link) => {
46-
const payload = link.body as SearchPayload;
47-
42+
const [ nextPageConfig, setNextPageConfig ] = useState<Link>();
43+
const [ previousPageConfig, setPreviousPageConfig ] = useState<Link>();
44+
45+
const setPaginationConfig = useCallback(
46+
(links: Link[]) => {
47+
setNextPageConfig(links.find(({ rel }) => rel === 'next'));
48+
setPreviousPageConfig(links.find(({ rel }) => ['prev', 'previous'].includes(rel)));
49+
}, []
50+
);
51+
52+
const executeSearch = useCallback(
53+
(payload: SearchPayload) => {
4854
setResults(undefined);
4955
setState('LOADING');
5056
setError(undefined);
51-
setNextPage(undefined);
52-
setPreviousPage(undefined);
57+
setNextPageConfig(undefined);
58+
setPreviousPageConfig(undefined);
59+
5360
stacApi.search(payload)
5461
.then(response => response.json())
5562
.then(data => {
5663
setResults(data);
57-
parsePagination(data.links);
64+
setPaginationConfig(data.links);
5865
})
5966
.catch((err) => setError(err))
6067
.finally(() => setState('IDLE'));
6168
},
62-
[]
69+
[stacApi, setPaginationConfig]
6370
);
6471

65-
const parsePagination = useCallback(
66-
(links: Link[]) => {
67-
const nextPageLink = links.find(({ rel }) => rel === 'next');
68-
if (nextPageLink) {
69-
setNextPage(() => () => loadNewPage(nextPageLink));
72+
const flipPage = useCallback(
73+
(link?: Link) => {
74+
if (link) {
75+
const payload = link.body as SearchPayload;
76+
executeSearch(payload);
7077
}
78+
},
79+
[executeSearch]
80+
);
7181

72-
// Turns not all STAC APIs implement the spec correctly, some advertise the prev link as previous
73-
const previousPageLink = links.find(({ rel }) => ['prev', 'previous'].includes(rel));
74-
if (previousPageLink) {
75-
setPreviousPage(() => () => loadNewPage(previousPageLink));
76-
}
77-
}, [loadNewPage]
82+
const nextPageFn = useCallback(
83+
() => flipPage(nextPageConfig),
84+
[flipPage, nextPageConfig]
85+
);
86+
87+
const previousPageFn = useCallback(
88+
() => flipPage(previousPageConfig),
89+
[flipPage, previousPageConfig]
7890
);
7991

8092
const submit = useCallback(
@@ -84,21 +96,8 @@ function useStacSearch(stacApi: StacApi): StacSearchHook {
8496
collections,
8597
dateRange: { from: dateRangeFrom, to: dateRangeTo }
8698
};
87-
88-
setResults(undefined);
89-
setState('LOADING');
90-
setError(undefined);
91-
setNextPage(undefined);
92-
setPreviousPage(undefined);
93-
stacApi.search(payload)
94-
.then(response => response.json())
95-
.then(data => {
96-
setResults(data);
97-
parsePagination(data.links);
98-
})
99-
.catch((err) => setError(err))
100-
.finally(() => setState('IDLE'));
101-
}, [stacApi, bbox, collections, dateRangeFrom, dateRangeTo, parsePagination]
99+
executeSearch(payload);
100+
}, [executeSearch, bbox, collections, dateRangeFrom, dateRangeTo]
102101
);
103102

104103
return {
@@ -114,8 +113,8 @@ function useStacSearch(stacApi: StacApi): StacSearchHook {
114113
results,
115114
state,
116115
error,
117-
nextPage,
118-
previousPage
116+
nextPage: nextPageConfig ? nextPageFn : undefined,
117+
previousPage: previousPageConfig ? previousPageFn : undefined
119118
};
120119
}
121120

0 commit comments

Comments
 (0)