@empathyco/x-components
Version:
Empathy X Components
58 lines (55 loc) • 1.81 kB
JavaScript
import { createFetchAndSaveActions } from '../../../../store/utils/fetch-and-save-action.utils.js';
const { fetchAndSave, cancelPrevious } = createFetchAndSaveActions({
async fetch({ dispatch, state }, request) {
return request
? dispatch('fetchSearchResponse', enrichRequest(request, state))
: Promise.resolve(null);
},
onSuccess({ dispatch }, response) {
if (response !== null) {
void dispatch('saveSearchResponse', response);
}
},
});
/**
* Enriches the {@link SearchRequest} object with the origin and page properties taken from the
* {@link SearchState | search state}.
*
* @param request - The {@link InternalSearchRequest}.
* @param state - {@link SearchState}.
*
* @returns The search request.
* @internal
*/
function enrichRequest(request, state) {
const { page, ...restRequest } = request;
const { config: { pageSize, pageMode }, origin, results, } = state;
let start;
if (pageMode === 'infinite_scroll') {
start = page === 1 ? 0 : results.length;
}
else {
start = state.config.pageSize * (page - 1);
}
const rows = pageMode === 'infinite_scroll' ? pageSize * page - start : pageSize;
return {
...restRequest,
...(origin && { origin }),
start,
rows,
};
}
/**
* Default implementation for {@link SearchActions.fetchAndSaveSearchResponse} action.
*
* @public
*/
const fetchAndSaveSearchResponse = fetchAndSave;
/**
* Default implementation for {@link SearchActions.cancelFetchAndSaveSearchResponse} action.
*
* @public
*/
const cancelFetchAndSaveSearchResponse = cancelPrevious;
export { cancelFetchAndSaveSearchResponse, fetchAndSaveSearchResponse };
//# sourceMappingURL=fetch-and-save-search-response.action.js.map