};
const handleFavorites = (dispatch, projects) => {
- const favorites = projects.filter(project => project.isFavorite);
- if (favorites.length) {
- dispatch(receiveFavorites(favorites));
+ const toAdd = projects.filter(project => project.isFavorite);
+ const toRemove = projects.filter(project => project.isFavorite === false);
+ if (toAdd.length || toRemove.length) {
+ dispatch(receiveFavorites(toAdd, toRemove));
}
};
REMOVE_FAVORITE: 'REMOVE_FAVORITE'
};
-export const receiveFavorites = favorites => ({
+export const receiveFavorites = (favorites, notFavorites = []) => ({
type: actions.RECEIVE_FAVORITES,
- favorites
+ favorites,
+ notFavorites
});
export const addFavorite = componentKey => ({
export default (state = [], action = {}) => {
if (action.type === actions.RECEIVE_FAVORITES) {
- return uniq([...state, ...action.favorites.map(f => f.key)]);
+ const toAdd = action.favorites.map(f => f.key);
+ const toRemove = action.notFavorites.map(f => f.key);
+ return without(uniq([...state, ...toAdd]), ...toRemove);
}
if (action.type === actions.ADD_FAVORITE) {