@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
50 lines (48 loc) • 2.03 kB
JavaScript
import PipelineRequest from '@shopgate/pwa-core/classes/PipelineRequest';
import { logger } from '@shopgate/pwa-core/helpers';
import { generateResultHash, shouldFetchFilters } from '@shopgate/pwa-common/helpers/redux';
import { SHOPGATE_CATALOG_GET_FILTERS } from "../constants/Pipelines";
import requestFilters from "../action-creators/requestFilters";
import receiveFilters from "../action-creators/receiveFilters";
import errorFilters from "../action-creators/errorFilters";
import { getFilterResults } from "../selectors";
import buildFilterParams from "./helpers/buildFilterParams";
import processParams from "./helpers/processParams";
/**
* Retrieves the available filters for a list of products.
* @param {Object} options The options for the getProducts request.
* @param {Object} [options.filters = null] Filters object for the request
* @returns {Function} A redux thunk
*/
function fetchFilters({
filters = null
} = {}) {
return (dispatch, getState) => {
const state = getState();
const params = buildFilterParams(state, filters);
const hash = generateResultHash({
pipeline: SHOPGATE_CATALOG_GET_FILTERS,
...params
}, false, false);
const result = getFilterResults(state)[hash];
if (!shouldFetchFilters(result)) {
return Promise.resolve(null);
}
// We need to process the params to handle edge cases in the pipeline params.
const requestParams = processParams(params);
if (Object.keys(requestParams).length === 0) {
const error = `Attempt to call ${SHOPGATE_CATALOG_GET_FILTERS} pipeline without parameters - aborted`;
logger.error(error);
return Promise.reject(new Error(error));
}
dispatch(requestFilters(hash));
const request = new PipelineRequest(SHOPGATE_CATALOG_GET_FILTERS).setInput(requestParams).dispatch();
request.then(response => {
dispatch(receiveFilters(hash, response.filters));
}).catch(() => {
dispatch(errorFilters(hash));
});
return request;
};
}
export default fetchFilters;