export function getSuggestedContent()

in public/js/actions/AtomActions/getSuggestedContent.js [38:72]


export function getSuggestedContent(atomId, atomType) {
  const path = `/atom/${atomType.toLowerCase()}/${atomId}`;
  const pluralType = `${atomType}s`;

  return dispatch => {
    dispatch(requestSuggestedContent(atomType, atomId));
    return fetchTargetsForAtomPath(path)
      .then((targets) => {
        return targets.map((target) => {
          return target.tagPaths;
        });
      })
      .then(tags => getContentByTags(tags, pluralType))
      .then(content => {
        //Exclude any content with this atom
        const suggested = content.filter(c => {
          if (c.atoms && c.atoms[pluralType]) {
            const atomIdx = c.atoms[pluralType].find(a => {
              return a.id === atomId;
            });
            return atomIdx === -1;
          } else {
            return true;
          }
        });

        suggested.sort((first, second) => new Date(second.fields.creationDate) - new Date(first.fields.creationDate));

        dispatch(receiveSuggestedContent(suggested));
      })
      .catch(error => {
        dispatch(errorReceivingSuggestedContent(error));
      });
  };
}