import {
  REMEMBER_ARTICLE_SUCCESS,
  UPDATE_ARTICLE_SUCCESS,
  LOAD_ARTICLE_SUCCESS,
  REMOVE_ARTICLE_SUCCESS,
} from './../../actions/article';


export default (state, action) => {
  switch (action.type) {
    case LOAD_ARTICLE_SUCCESS: {
      const articles = action.payload.getIn(['entities', 'article']);

      if (!articles) {
        return state;
      }

      return state.setIn(['article', action.meta.articleUrl], articles.first());
    }
    case REMEMBER_ARTICLE_SUCCESS: {
      const article = action.payload.getIn(['entities', 'article']).first();

      return state.setIn(['article', action.meta.articleUrl], article);
    }
    case UPDATE_ARTICLE_SUCCESS: {
      const article = action.payload.getIn(['entities', 'article']).first();
      const articleUrl = state.getIn(['article']).findKey((el) => el.get('id') === action.meta.articleId);

      return state.setIn(['article', articleUrl], article);
    }
    case REMOVE_ARTICLE_SUCCESS: {
      const articleUrl = state.getIn(['article']).findKey((el) => el.get('id') === action.meta.articleId);

      return state.deleteIn(['article', articleUrl]);
    }
    default: {
      return state;
    }
  }
};

import {
  REMEMBER_ARTICLE_SUCCESS,
  UPDATE_ARTICLE_SUCCESS,
  LOAD_ARTICLE_SUCCESS,
  REMOVE_ARTICLE_SUCCESS,
} from './../../actions/article';


export default (state, action) => {
  switch (action.type) {
    case LOAD_ARTICLE_SUCCESS: {
      const articles = action.payload.getIn(['entities', 'article']);

      if (!articles) {
        return state;
      }

      return state.setIn(['article', action.meta.articleUrl], articles.first());
    }
    case REMEMBER_ARTICLE_SUCCESS: {
      const article = action.payload.getIn(['entities', 'article']).first();

      return state.setIn(['article', action.meta.articleUrl], article);
    }
    case UPDATE_ARTICLE_SUCCESS: {
      const article = action.payload.getIn(['entities', 'article']).first();

      return state.update('article', (articles) =>
                          articles.map((v) =>
                                       (v.get('id') === action.meta.articleId ? article : v)));
    }
    case REMOVE_ARTICLE_SUCCESS: {
      return state.update('article', (articles) =>
                          articles.filter((v) => v.get('id') !== action.meta.articleId));
    }
    default: {
      return state;
    }
  }
};

Fix popup updates when url changes
Fix popup updates when url changes

Problem: when the article changes url once (medium.com), popup is not
able to report article status immediately.

Cause: the issue was caused by entities reducer to update only one
article (in non-deterministic fashion).

Solution: this commit makes entities reducer update all entries with
the matching id.
