Skip to content

Commit f1bc57a

Browse files
committed
feat: Add reload item
1 parent 5db4b2b commit f1bc57a

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

src/context/index.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type StacApiContextType = {
1212
setCollections: (collections?: CollectionsResponse) => void;
1313
getItem: (id: string) => Item | undefined;
1414
addItem: (id: string, item: Item) => void;
15+
deleteItem: (id: string) => void;
1516
}
1617

1718
type StacApiProviderType = {
@@ -33,13 +34,20 @@ export function StacApiProvider({ children, apiUrl, options }: StacApiProviderTy
3334
setItems(new Map(items.set(itemPath, item)));
3435
}, [items]);
3536

37+
const deleteItem = useCallback((itemPath: string) => {
38+
const tempItems = new Map(items);
39+
items.delete(itemPath);
40+
setItems(tempItems);
41+
}, [items]);
42+
3643
const contextValue = useMemo(() => ({
3744
stacApi,
3845
collections,
3946
setCollections,
4047
getItem,
41-
addItem
42-
}), [addItem, collections, getItem, stacApi]);
48+
addItem,
49+
deleteItem
50+
}), [addItem, collections, deleteItem, getItem, stacApi]);
4351

4452
return (
4553
<StacApiContext.Provider value={contextValue}>
@@ -54,14 +62,16 @@ export function useStacApiContext() {
5462
collections,
5563
setCollections,
5664
getItem,
57-
addItem
65+
addItem,
66+
deleteItem
5867
} = useContext(StacApiContext);
5968

6069
return {
6170
stacApi,
6271
collections,
6372
setCollections,
6473
getItem,
65-
addItem
74+
addItem,
75+
deleteItem
6676
};
6777
}

src/hooks/useItem.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fetch from 'jest-fetch-mock';
2-
import { renderHook } from '@testing-library/react-hooks';
2+
import { renderHook, act } from '@testing-library/react-hooks';
33
import useItem from './useItem';
44
import wrapper from './wrapper';
55

@@ -57,4 +57,23 @@ describe('useItem', () => {
5757
detail: 'Wrong query'
5858
});
5959
});
60+
61+
it('reloads item', async () => {
62+
fetch
63+
.mockResponseOnce(JSON.stringify({ id: 'abc', links: [] }))
64+
.mockResponseOnce(JSON.stringify({ id: 'abc' }))
65+
.mockResponseOnce(JSON.stringify({ id: 'abc', description: 'Updated' }));
66+
67+
const { result, waitForNextUpdate } = renderHook(
68+
() => useItem('https://fake-stac-api.net/items/abc'),
69+
{ wrapper }
70+
);
71+
await waitForNextUpdate();
72+
expect(result.current.item).toEqual({ id: 'abc' });
73+
74+
act(() => result.current.reload());
75+
76+
await waitForNextUpdate();
77+
expect(result.current.item).toEqual({ id: 'abc', description: 'Updated' });
78+
});
6079
});

src/hooks/useItem.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import { useState, useEffect} from 'react';
1+
import { useState, useEffect, useCallback} from 'react';
22
import { Item } from '../types/stac';
33
import { ApiError, LoadingState } from '../types';
44
import { useStacApiContext } from '../context';
55

66
type ItemHook = {
7-
item?: Item
8-
state: LoadingState
9-
error?: ApiError
7+
item?: Item;
8+
state: LoadingState;
9+
error?: ApiError;
10+
reload: () => void;
1011
}
1112

1213
function useItem(url: string): ItemHook {
13-
const { stacApi, getItem, addItem } = useStacApiContext();
14+
const { stacApi, getItem, addItem, deleteItem } = useStacApiContext();
1415
const [ state, setState ] = useState<LoadingState>('IDLE');
1516
const [ item, setItem ] = useState<Item>();
1617
const [ error, setError ] = useState<ApiError>();
@@ -38,10 +39,39 @@ function useItem(url: string): ItemHook {
3839
.finally(() => setState('IDLE'));
3940
}, [stacApi, addItem, getItem, url]);
4041

42+
const fetchItem = useCallback(() => {
43+
if (!stacApi) return;
44+
45+
setState('LOADING');
46+
new Promise((resolve, reject) => {
47+
const i = getItem(url);
48+
if (i) {
49+
resolve(i);
50+
} else {
51+
stacApi.fetch(url)
52+
.then(r => r.json())
53+
.then(r => {
54+
addItem(url, r);
55+
resolve(r);
56+
})
57+
.catch((err) => reject(err));
58+
}
59+
})
60+
.then(setItem)
61+
.catch((err) => setError(err))
62+
.finally(() => setState('IDLE'));
63+
}, [addItem, getItem, stacApi, url]);
64+
65+
const reload = useCallback(() => {
66+
deleteItem(url);
67+
fetchItem();
68+
}, [deleteItem, fetchItem, url]);
69+
4170
return {
4271
item,
4372
state,
44-
error
73+
error,
74+
reload
4575
};
4676
}
4777

0 commit comments

Comments
 (0)