@@ -42,13 +42,19 @@ function useStacSearch(stacApi: StacApi): StacSearchHook {
42
42
const [ nextPageConfig , setNextPageConfig ] = useState < Link > ( ) ;
43
43
const [ previousPageConfig , setPreviousPageConfig ] = useState < Link > ( ) ;
44
44
45
+ /**
46
+ * Extracts the pagination config from the the links array of the items response
47
+ */
45
48
const setPaginationConfig = useCallback (
46
49
( links : Link [ ] ) => {
47
50
setNextPageConfig ( links . find ( ( { rel } ) => rel === 'next' ) ) ;
48
51
setPreviousPageConfig ( links . find ( ( { rel } ) => [ 'prev' , 'previous' ] . includes ( rel ) ) ) ;
49
52
} , [ ]
50
53
) ;
51
54
55
+ /**
56
+ * Returns the search payload based on the current application state
57
+ */
52
58
const getSearchPayload = useCallback (
53
59
( ) => ( {
54
60
bbox,
@@ -58,40 +64,65 @@ function useStacSearch(stacApi: StacApi): StacSearchHook {
58
64
[ bbox , collections , dateRangeFrom , dateRangeTo ]
59
65
) ;
60
66
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
+ */
61
90
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
+ ) ;
68
94
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 ]
79
101
) ;
80
102
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
+ */
81
108
const flipPage = useCallback (
82
109
( link ?: Link ) => {
83
110
if ( link ) {
84
111
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 ) ;
90
122
}
91
- executeSearch ( payload , link . headers ) ;
92
123
}
93
124
} ,
94
- [ executeSearch , getSearchPayload ]
125
+ [ executeSearch , getItems , getSearchPayload ]
95
126
) ;
96
127
97
128
const nextPageFn = useCallback (
0 commit comments