Skip to content

Commit cf60bf6

Browse files
committed
feat: Add useCollections hook
1 parent 56a4b8e commit cf60bf6

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

src/hooks/useCollections.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import fetch from 'jest-fetch-mock';
2+
import { renderHook, act } from '@testing-library/react-hooks';
3+
import useCollections from './useCollections';
4+
import StacApi from '../stac-api';
5+
6+
describe('useCollections', () => {
7+
const stacApi = new StacApi('https://fake-stac-api.net');
8+
beforeEach(() => fetch.resetMocks());
9+
10+
it('queries collections', async () => {
11+
fetch.mockResponseOnce(JSON.stringify({ data: '12345' }));
12+
13+
const { result, waitForNextUpdate } = renderHook(
14+
() => useCollections(stacApi)
15+
);
16+
expect(result.current.state).toEqual('LOADING');
17+
await waitForNextUpdate();
18+
expect(fetch.mock.calls[0][0]).toEqual('https://fake-stac-api.net/collections');
19+
expect(result.current.collections).toEqual({ data: '12345' });
20+
expect(result.current.state).toEqual('IDLE');
21+
});
22+
23+
it('reloads collections', async () => {
24+
fetch.mockResponseOnce(JSON.stringify({ data: 'original' }));
25+
26+
const { result, waitForNextUpdate } = renderHook(
27+
() => useCollections(stacApi)
28+
);
29+
await waitForNextUpdate();
30+
expect(result.current.collections).toEqual({ data: 'original' });
31+
32+
expect(result.current.state).toEqual('IDLE');
33+
fetch.mockResponseOnce(JSON.stringify({ data: 'reloaded' }));
34+
act(() => result.current.reload());
35+
await waitForNextUpdate();
36+
expect(result.current.collections).toEqual({ data: 'reloaded' });
37+
});
38+
});

src/hooks/useCollections.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { useCallback, useEffect, useState } from 'react';
2+
import StacApi from '../stac-api';
3+
import type { GenericObject, LoadingState } from '../types';
4+
5+
type StacCollectionsHook = {
6+
collections?: GenericObject,
7+
reload: () => void,
8+
state: LoadingState
9+
};
10+
11+
function useCollections(stacApi: StacApi): StacCollectionsHook {
12+
const [ collections, setCollections ] = useState<GenericObject>();
13+
const [ state, setState ] = useState<LoadingState>('IDLE');
14+
15+
const getCollections = useCallback(
16+
() => {
17+
setState('LOADING');
18+
setCollections(undefined);
19+
20+
stacApi.getCollections()
21+
.then(response => response.json())
22+
.then(setCollections)
23+
.finally(() => setState('IDLE'));
24+
},
25+
[stacApi]
26+
);
27+
28+
useEffect(() => getCollections() ,[getCollections]);
29+
30+
return {
31+
collections,
32+
reload: getCollections,
33+
state
34+
};
35+
}
36+
37+
export default useCollections;

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import useStacSearch from './hooks/useStacSearch';
2+
import useCollections from './hooks/useCollections';
23
import StacApi from './stac-api';
34

45
export {
6+
useCollections,
57
useStacSearch,
68
StacApi
79
};

src/stac-api/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ class StacApi {
6868
}
6969

7070
fetch(url: string, options: Partial<FetchOptions> = {}): Promise<Response> {
71-
const { method = 'GET', payload = {}, headers = {} } = options;
71+
const { method = 'GET', payload, headers = {} } = options;
7272

7373
return fetch(url, {
7474
method,
7575
headers: {
7676
'Content-Type': 'application/json',
7777
...headers
7878
},
79-
body: JSON.stringify(payload)
79+
body: payload ? JSON.stringify(payload) : undefined
8080
}).then(async (response) => {
8181
if (response.ok) {
8282
return response;
@@ -99,6 +99,10 @@ class StacApi {
9999
{ method: 'POST', payload: requestPayload, headers }
100100
);
101101
}
102+
103+
getCollections(): Promise<Response> {
104+
return this.fetch(`${this.baseUrl}/collections`);
105+
}
102106
}
103107

104108
export default StacApi;

0 commit comments

Comments
 (0)