text: import fetch from 'isomorphic-fetch'
  import { REQUEST_POSTS, RECEIVE_POSTS, SELECT_REDDIT, INVALIDATE_REDDIT } from '../constants/ActionTypes'
  
  export function selectReddit(reddit) {
    return {
      type: SELECT_REDDIT,
      reddit
    }
  }
  
  export function invalidateReddit(reddit) {
    return {
      type: INVALIDATE_REDDIT,
      reddit
    }
  }
  
  function requestPosts(reddit) {
    return {
      type: REQUEST_POSTS,
      reddit
    }
  }
  
  function receivePosts(reddit, json) {
    return {
      type: RECEIVE_POSTS,
      reddit: reddit,
      posts: json.success,
      receivedAt: Date.now()
    }
  }
  
  function fetchPosts(reddit) {
    return dispatch => {
      dispatch(requestPosts(reddit))
      return fetch(`http://localhost:8011/api/price/getpurchase/${reddit}`)
        .then(response => response.json())
        .then(json => {
          dispatch(receivePosts(reddit, json))
        })
    }
  }
  
  function shouldFetchPosts(state, reddit) {
    const posts = state.postsByReddit[reddit]
    if (!posts) {
      return true
    }
    if (posts.isFetching) {
      return false
    }
    return posts.didInvalidate
  }
  
  export function fetchPostsIfNeeded(reddit) {
    return (dispatch, getState) => {
      if (shouldFetchPosts(getState(), reddit)) {
        return dispatch(fetchPosts(reddit))
      }
    }
  }
  