export function getAtomUsages()

in public/js/actions/AtomActions/getAtomUsages.js [32:57]


export function getAtomUsages(atomType, atomId) {
  return dispatch => {
    dispatch(requestAtomUsages());
    return fetchAtomUsages(atomType, atomId)
    .then(res => {

      // the atom usage endpoint in capi only returns article paths,
      // lookup the articles in capi to get their fields
      Promise.all(res.map(getByPath))
        .then(capiResponse => {
          const usages = capiResponse.reduce((all, item) => {
            all.push(item);
            return all;
          }, []);

          // sort by article creation date DESC
          usages.sort((first, second) => new Date(second.fields.creationDate) - new Date(first.fields.creationDate));

          dispatch(receiveAtomUsages(usages));
        });
    })
    .catch(error => {
      dispatch(errorReceivingAtomUsages(error));
    });
  };
}