@@ -42,13 +42,19 @@ function useStacSearch(stacApi: StacApi): StacSearchHook {
4242 const [ nextPageConfig , setNextPageConfig ] = useState < Link > ( ) ;
4343 const [ previousPageConfig , setPreviousPageConfig ] = useState < Link > ( ) ;
4444
45+ /**
46+ * Extracts the pagination config from the the links array of the items response
47+ */
4548 const setPaginationConfig = useCallback (
4649 ( links : Link [ ] ) => {
4750 setNextPageConfig ( links . find ( ( { rel } ) => rel === 'next' ) ) ;
4851 setPreviousPageConfig ( links . find ( ( { rel } ) => [ 'prev' , 'previous' ] . includes ( rel ) ) ) ;
4952 } , [ ]
5053 ) ;
5154
55+ /**
56+ * Returns the search payload based on the current application state
57+ */
5258 const getSearchPayload = useCallback (
5359 ( ) => ( {
5460 bbox,
@@ -58,40 +64,65 @@ function useStacSearch(stacApi: StacApi): StacSearchHook {
5864 [ bbox , collections , dateRangeFrom , dateRangeTo ]
5965 ) ;
6066
67+ /**
68+ * Resets the state and processes the results from the provided request
69+ */
70+ const processRequest = useCallback ( ( request : Promise < Response > ) => {
71+ setResults ( undefined ) ;
72+ setState ( 'LOADING' ) ;
73+ setError ( undefined ) ;
74+ setNextPageConfig ( undefined ) ;
75+ setPreviousPageConfig ( undefined ) ;
76+
77+ request
78+ . then ( response => response . json ( ) )
79+ . then ( data => {
80+ setResults ( data ) ;
81+ setPaginationConfig ( data . links ) ;
82+ } )
83+ . catch ( ( err ) => setError ( err ) )
84+ . finally ( ( ) => setState ( 'IDLE' ) ) ;
85+ } , [ setPaginationConfig ] ) ;
86+
87+ /**
88+ * Executes a POST request against the `search` endpoint using the provided payload and headers
89+ */
6190 const executeSearch = useCallback (
62- ( payload : SearchPayload , headers = { } ) => {
63- setResults ( undefined ) ;
64- setState ( 'LOADING' ) ;
65- setError ( undefined ) ;
66- setNextPageConfig ( undefined ) ;
67- setPreviousPageConfig ( undefined ) ;
91+ ( payload : SearchPayload , headers = { } ) => processRequest ( stacApi . search ( payload , headers ) ) ,
92+ [ stacApi , processRequest ]
93+ ) ;
6894
69- stacApi . search ( payload , headers )
70- . then ( response => response . json ( ) )
71- . then ( data => {
72- setResults ( data ) ;
73- setPaginationConfig ( data . links ) ;
74- } )
75- . catch ( ( err ) => setError ( err ) )
76- . finally ( ( ) => setState ( 'IDLE' ) ) ;
77- } ,
78- [ stacApi , setPaginationConfig ]
95+ /**
96+ * Execute a GET request against the provided URL
97+ */
98+ const getItems = useCallback (
99+ ( url : string ) => processRequest ( stacApi . get ( url ) ) ,
100+ [ stacApi , processRequest ]
79101 ) ;
80102
103+ /**
104+ * Retreives a page from a paginatied item set using the provided link config.
105+ * Executes a POST request against the `search` endpoint if pagination uses POST
106+ * or retrieves the page items using GET against the link href
107+ */
81108 const flipPage = useCallback (
82109 ( link ?: Link ) => {
83110 if ( link ) {
84111 let payload = link . body as LinkBody ;
85- if ( payload . merge ) {
86- payload = {
87- ...payload ,
88- ...getSearchPayload ( )
89- } ;
112+ if ( payload ) {
113+ if ( payload . merge ) {
114+ payload = {
115+ ...payload ,
116+ ...getSearchPayload ( )
117+ } ;
118+ }
119+ executeSearch ( payload , link . headers ) ;
120+ } else {
121+ getItems ( link . href ) ;
90122 }
91- executeSearch ( payload , link . headers ) ;
92123 }
93124 } ,
94- [ executeSearch , getSearchPayload ]
125+ [ executeSearch , getItems , getSearchPayload ]
95126 ) ;
96127
97128 const nextPageFn = useCallback (
0 commit comments