import fetch from 'isomorphic-fetch';


export function invalidateReddit({ reddit }) {}

export function requestPosts({ reddit }) {}

export function receivePosts({ reddit, json }) {
  return {
    reddit,
    posts: json.data.children.map(child => child.data),
    receivedAt: Date.now()
  };
}

function fetchPosts({ reddit }, { currentActionSet }) {
  currentActionSet.requestPosts({ reddit });

  fetch(`http://www.reddit.com/r/${reddit}.json`)
    .then(response => response.json())
    .then(json => currentActionSet.receivePosts({ reddit, json }))
  ;
}

export const introspection = {
  shouldFetchPosts({ reddit }) {}
}

export function fetchPostsIfNeeded({ reddit }, { currentActionSet, getConsensus }) {
  if (currentActionSet.getConsensus.shouldFetchPosts({ reddit }).some()) {
    fetchPosts({ reddit }, { currentActionSet });
  }
}

import fetch from 'isomorphic-fetch';


export function invalidateReddit({ reddit }) {}

export function requestPosts({ reddit }) {}

export function receivePosts({ reddit, json }) {
  return {
    reddit,
    posts: json.data.children.map(child => child.data),
    receivedAt: Date.now()
  };
}

function fetchPosts({ reddit }, { currentActionSet }) {
  currentActionSet.requestPosts({ reddit });

  fetch(`http://www.reddit.com/r/${reddit}.json`)
    .then(response => response.json())
    .then(json => currentActionSet.receivePosts({ reddit, json }))
  ;
}

export const introspection = {
  shouldFetchPosts({ reddit }) {}
}

export function fetchPostsIfNeeded({ reddit }, { currentActionSet }) {
  if (currentActionSet.consensus.shouldFetchPosts({ reddit }).some()) {
    fetchPosts({ reddit }, { currentActionSet });
  }
}

Update async-redux example to getConsensus -> consensus
Update async-redux example to getConsensus -> consensus
