import { models } from '@r/api-client';
import * as platformActions from '@r/platform/actions';
import { METHODS } from '@r/platform/router';

import * as subredditActions from './subreddits';

const { Subreddit } = models;

const gotoSubredditPage = (subredditName, dispatch) => {
  dispatch(platformActions.navigateToUrl(METHODS.GET, `/r/${subredditName}`, {}));
};

const subredditExists = (subredditName, state) => {
  return !!state.subreddits[subredditName];
};

export const gotoSubreddit = (queriedName, waitForAction) => async (dispatch, getState) => {
  const name = Subreddit.cleanName(queriedName);

  if (subredditExists(name, getState())) {
    gotoSubredditPage(name, dispatch);
    return;
  }

  dispatch(subredditActions.fetchSubreddit(name));

  await waitForAction(
    subredditActions.receivedSubreddit(),
    () => {}, // do nothing in a callback, do it here,
    () => {}, // do nothing when state fails, wait for it to fetch
  );

  if (subredditExists(name, getState())) {
    gotoSubredditPage(name, dispatch);
    return;
  }

  let query = queriedName;

  if (query.indexOf('/') !== -1) {
    query = query.location.split('/')[1];
  }

  const queryParams = { q: query };

  dispatch(platformActions.navigateToUrl(METHODS.GET, '/search', { queryParams }));
};

@@ -787,167 +787,97 @@
     
-subredditActions.receivedSubreddit(),%0A    () =%3E %7B%7D, // do nothing in a callback, do it here,%0A    () =%3E %7B%7D, // do nothing when state fails, wait for it to fetch
+(action) =%3E action.type === subredditActions.RECEIEVED_SUBREDDIT && action.name === name,
 %0A  )

Update node-middleware