{"size": 1380, "ext": "js", "max_stars_repo_path": "libraries/commerce/category/actions/fetchCategoryProducts.js", "max_stars_repo_name": "shopg8felix/pwa", "max_stars_repo_stars_event_min_datetime": "2018-03-07T20:08:18.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-01T10:12:27.000Z", "max_issues_repo_path": "libraries/commerce/category/actions/fetchCategoryProducts.js", "max_issues_repo_name": "shopg8felix/pwa", "max_issues_repo_issues_event_min_datetime": "2018-04-18T10:35:12.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-14T07:41:23.000Z", "max_forks_repo_path": "libraries/commerce/category/actions/fetchCategoryProducts.js", "max_forks_repo_name": "shopg8felix/pwa", "max_forks_repo_forks_event_min_datetime": "2018-05-15T12:53:47.000Z", "max_forks_repo_forks_event_max_datetime": "2021-04-21T06:30:04.000Z"}
{"max_stars_count": 35, "max_issues_count": 435, "max_forks_count": 10, "avg_line_length": 29.3617021277, "max_line_length": 79, "alphanum_fraction": 0.6731884058}
import { ITEMS_PER_LOAD } from '@shopgate/pwa-common/constants/DisplayOptions';
import { getSortOrder } from '@shopgate/pwa-common/selectors/history';
import { mutable } from '@shopgate/pwa-common/helpers/redux';
import fetchProducts from '../../product/actions/fetchProducts';

/**
 * Retrieves category products by a category id.
 * @param {string} categoryId The category id to requests products for.
 * @param {Object} [filters=null] The filters.
 * @param {number} offset The offset for the products to request.
 * @param {number} limit The number of products to request.
 * @param {string} sort The sort order of the products.
 * @param {boolean} [cached=true] The cache strategy.
 * @param {null|number} [cachedTime=null] The cache TTL.
 * @param {Object} params additional params.
 * @return {Function} The dispatched action.
 */
const fetchCategoryProducts = ({
  categoryId,
  filters = null,
  offset = 0,
  limit = ITEMS_PER_LOAD,
  sort,
  cached = true,
  cachedTime = null,
  params,
}) =>
  (dispatch, getState) => {
    const sortOrder = sort || getSortOrder(getState());

    dispatch(fetchProducts({
      cached,
      cachedTime,
      params: {
        categoryId,
        offset,
        limit,
        sort: sortOrder,
        ...params,
      },
      filters,
    }));
  };

/** @mixes {MutableFunction} */
export default mutable(fetchCategoryProducts);
