UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

6 lines 1.7 kB
import{produce}from'immer';import{RECEIVE_FAVORITES_LISTS,SUCCESS_ADD_FAVORITES_LIST,SUCCESS_UPDATE_FAVORITES_LIST,SUCCESS_REMOVE_FAVORITES_LIST,OPEN_FAVORITE_LIST_CHOOSER,CLOSE_FAVORITE_LIST_CHOOSER,FAVORITES_LIFETIME,OPEN_FAVORITE_COMMENT_DIALOG,CLOSE_FAVORITE_COMMENT_DIALOG}from"../constants";/** * Favorites lists reducer. * @param {Object} state Current state. * @param {Object} action Dispatched action. * @returns {Object} New state. */var lists=function lists(){var state=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{expires:0,lists:[],chooser:null,commentDialog:null};var action=arguments.length>1?arguments[1]:undefined;/* eslint-disable no-param-reassign */var newState=produce(state,function(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:{var list=draft.lists.find(function(l){return l.id===action.listId;});list.name=action.name;break;}case SUCCESS_REMOVE_FAVORITES_LIST:{var listIndex=draft.lists.findIndex(function(l){return l.id===action.listId;});draft.lists.splice(listIndex,1);break;}default:break;}});/* eslint-enable no-param-reassign */return newState;};export default lists;