UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

73 lines (72 loc) 1.96 kB
import { produce } from 'immer'; import { CLOSE_FAVORITE_COMMENT_DIALOG, CLOSE_FAVORITE_LIST_CHOOSER, FAVORITES_LIFETIME, OPEN_FAVORITE_COMMENT_DIALOG, OPEN_FAVORITE_LIST_CHOOSER, RECEIVE_FAVORITES_LISTS, SUCCESS_ADD_FAVORITES_LIST, SUCCESS_REMOVE_FAVORITES_LIST, SUCCESS_UPDATE_FAVORITES_LIST } from "../constants"; /** * Favorites lists reducer. * @param {Object} state Current state. * @param {Object} action Dispatched action. * @returns {Object} New state. */ const lists = (state = { expires: 0, lists: [], chooser: null, commentDialog: null }, action = {}) => produce(state, draft => { switch (action.type) { case OPEN_FAVORITE_LIST_CHOOSER: { draft.chooser = { productId: action.productId, withRelatives: action.withRelatives }; break; } case CLOSE_FAVORITE_LIST_CHOOSER: { draft.chooser = null; break; } case OPEN_FAVORITE_COMMENT_DIALOG: { draft.commentDialog = { productId: action.productId, listId: action.listId }; break; } case CLOSE_FAVORITE_COMMENT_DIALOG: { draft.commentDialog = null; break; } case RECEIVE_FAVORITES_LISTS: { draft.expires = Date.now() + FAVORITES_LIFETIME; draft.lists = action.favoritesLists; break; } case SUCCESS_ADD_FAVORITES_LIST: { draft.lists.push({ id: action.listId, name: action.name }); break; } case SUCCESS_UPDATE_FAVORITES_LIST: { const list = draft.lists.find(l => l.id === action.listId); list.name = action.name; break; } case SUCCESS_REMOVE_FAVORITES_LIST: { const listIndex = draft.lists.findIndex(l => l.id === action.listId); draft.lists.splice(listIndex, 1); break; } default: break; } }); export default lists;