Skip to content

Commit 93ac27f

Browse files
committed
feat: Merge search payload with pagintion link body
1 parent 8ce2251 commit 93ac27f

File tree

3 files changed

+77
-16
lines changed

3 files changed

+77
-16
lines changed

src/hooks/useStacSearch.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,42 @@ describe('useStacSearch', () => {
259259
expect(result.current.results).toEqual({ data: '12345' });
260260
expect(postPayload).toEqual(response.links[0].body);
261261
});
262+
263+
it('merges pagination body', async () => {
264+
const response = {
265+
links: [{
266+
rel: 'previous',
267+
type: 'application/geo+json',
268+
method: 'POST',
269+
href: 'https://example.com/stac/search',
270+
body: {
271+
limit: 25,
272+
token: 'prev:abc123',
273+
merge: true
274+
}
275+
}]
276+
};
277+
fetch.mockResponseOnce(JSON.stringify(response));
278+
279+
const { result, waitForNextUpdate } = renderHook(
280+
() => useStacSearch(stacApi)
281+
);
282+
283+
act(() => result.current.setBbox([-0.59, 51.24, 0.30, 51.74]));
284+
act(() => result.current.submit());
285+
await waitForNextUpdate(); // wait to set results
286+
expect(result.current.results).toEqual(response);
287+
expect(result.current.previousPage).toBeDefined();
288+
289+
fetch.mockResponseOnce(JSON.stringify({ data: '12345' }));
290+
act(() => result.current.previousPage && result.current.previousPage());
291+
await waitForNextUpdate();
292+
293+
const postPayload = parseRequestPayload(fetch.mock.calls[1][1]);
294+
expect(result.current.results).toEqual({ data: '12345' });
295+
expect(postPayload).toEqual({
296+
bbox: [-0.59, 51.24, 0.30, 51.74],
297+
...response.links[0].body
298+
});
299+
});
262300
});

src/hooks/useStacSearch.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import { useCallback, useState } from 'react';
22
import StacApi from '../stac-api';
33

4-
import type { Link, Item, Bbox, CollectionIdList, ApiError, SearchPayload } from '../types';
4+
import type {
5+
Link,
6+
Item,
7+
Bbox,
8+
CollectionIdList,
9+
ApiError,
10+
SearchPayload,
11+
LinkBody
12+
} from '../types';
513

614
type SearchResponse = {
715
type: 'FeatureCollection'
@@ -49,6 +57,15 @@ function useStacSearch(stacApi: StacApi): StacSearchHook {
4957
}, []
5058
);
5159

60+
const getSearchPayload = useCallback(
61+
() => ({
62+
bbox,
63+
collections,
64+
dateRange: { from: dateRangeFrom, to: dateRangeTo }
65+
}),
66+
[ bbox, collections, dateRangeFrom, dateRangeTo ]
67+
);
68+
5269
const executeSearch = useCallback(
5370
(payload: SearchPayload) => {
5471
setResults(undefined);
@@ -72,11 +89,17 @@ function useStacSearch(stacApi: StacApi): StacSearchHook {
7289
const flipPage = useCallback(
7390
(link?: Link) => {
7491
if (link) {
75-
const payload = link.body as SearchPayload;
92+
let payload = link.body as LinkBody;
93+
if (payload.merge) {
94+
payload = {
95+
...payload,
96+
...getSearchPayload()
97+
};
98+
}
7699
executeSearch(payload);
77100
}
78101
},
79-
[executeSearch]
102+
[executeSearch, getSearchPayload]
80103
);
81104

82105
const nextPageFn = useCallback(
@@ -91,13 +114,9 @@ function useStacSearch(stacApi: StacApi): StacSearchHook {
91114

92115
const submit = useCallback(
93116
() => {
94-
const payload = {
95-
bbox,
96-
collections,
97-
dateRange: { from: dateRangeFrom, to: dateRangeTo }
98-
};
117+
const payload = getSearchPayload();
99118
executeSearch(payload);
100-
}, [executeSearch, bbox, collections, dateRangeFrom, dateRangeTo]
119+
}, [executeSearch, getSearchPayload]
101120
);
102121

103122
return {

src/types.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ export type DateRange = {
1111
to?: string
1212
}
1313

14+
export type SearchPayload = {
15+
bbox?: Bbox,
16+
collections?: CollectionIdList,
17+
dateRange?: DateRange,
18+
}
19+
20+
export type LinkBody = SearchPayload & {
21+
merge?: boolean
22+
}
23+
1424
export type Link = {
1525
href: string
1626
rel: string
@@ -20,7 +30,7 @@ export type Link = {
2030
length?: number
2131
method?: string
2232
headers?: GenericObject
23-
body?: SearchPayload
33+
body?: LinkBody
2434
merge?: boolean
2535
}
2636

@@ -47,9 +57,3 @@ export type ApiError = {
4757
status: number,
4858
statusText: string
4959
}
50-
51-
export type SearchPayload = {
52-
bbox?: Bbox,
53-
collections?: CollectionIdList,
54-
dateRange?: DateRange
55-
}

0 commit comments

Comments
 (0)