UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

28 lines 5.84 kB
import{produce}from'immer';import isNumber from'lodash/isNumber';import isString from'lodash/isString';import{REQUEST_ADD_FAVORITES,SUCCESS_ADD_FAVORITES,CANCEL_REQUEST_SYNC_FAVORITES,ERROR_ADD_FAVORITES,REQUEST_REMOVE_FAVORITES,SUCCESS_REMOVE_FAVORITES,ERROR_REMOVE_FAVORITES,REQUEST_FAVORITES,ERROR_FETCH_FAVORITES,SUCCESS_REMOVE_FAVORITES_LIST,SUCCESS_ADD_FAVORITES_LIST,RECEIVE_FAVORITES,FAVORITES_LIFETIME,SUCCESS_UPDATE_FAVORITES,REQUEST_UPDATE_FAVORITES,ERROR_UPDATE_FAVORITES,RECEIVE_FAVORITES_LISTS}from"../constants";/** * Favorites reducer. * @param {Object} state Current state. * @param {Object} action Dispatched action. * @returns {Object} New state. */var products=function products(){var state=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{byList:{}};var action=arguments.length>1?arguments[1]:undefined;/* eslint-disable no-param-reassign */var producedState=produce(state,function(draft){switch(action.type){// Handle a new favorites request. case REQUEST_FAVORITES:{var existingList=draft.byList[action.listId];if(!existingList){draft.byList[action.listId]={isFetching:true,lastChange:0,lastFetch:0,expires:0,items:[],syncCount:0};return;}existingList.isFetching=true;existingList.expires=0;break;}// Handle incoming favorite products. case RECEIVE_FAVORITES:{var list=draft.byList[action.listId];var isSynching=list.syncCount>0;var isOngoing=state.lastChange>action.requestTimestamp;/** * Note: When favorites are received, an add or remove request can be in progress. In this * case only fetching state will be updated and the received data will be discarded. * A new fetch request will be queued as soon as the sync is done, which will recover * discarded data. */list.isFetching=false;if(list.ready&&(isSynching||isOngoing)){return;}list.expires=Date.now()+FAVORITES_LIFETIME;list.items=action.items.map(function(_ref){var quantity=_ref.quantity,notes=_ref.notes,product=_ref.product;return{quantity:quantity,notes:notes,productId:product.id};});list.ready=true;// `syncCount` stays untouched because this is not considered to be a sync. break;}// Handle failed fetching case ERROR_FETCH_FAVORITES:{var _list=draft.byList[action.listId];_list.isFetching=false;_list.expires=0;_list.ready=true;// `syncCount` stays untouched because this is not considered to be a sync. break;}// Handle adding favorite list products. case REQUEST_ADD_FAVORITES:{var _list2=draft.byList[action.listId];var matchingItem=_list2.items.find(function(_ref2){var productId=_ref2.productId;return productId===action.productId;});if(matchingItem){matchingItem.notes=typeof action.notes==='string'?action.notes:matchingItem.notes;matchingItem.quantity=typeof action.quantity==='number'?matchingItem.quantity+action.quantity:matchingItem.quantity+1;}else{_list2.items.push({notes:action.notes||'',quantity:action.quantity||1,productId:action.productId});}_list2.lastChange=Date.now();_list2.syncCount+=1;break;}case REQUEST_UPDATE_FAVORITES:{var _list3=draft.byList[action.listId];var _matchingItem=_list3.items.find(function(_ref3){var productId=_ref3.productId;return productId===action.productId;});if(_matchingItem){if(isNumber(action.quantity)){_matchingItem.quantity=action.quantity;}if(isString(action.notes)){_matchingItem.notes=action.notes;}_list3.lastChange=Date.now();_list3.syncCount+=1;}break;}// Handle removing favorite list products. case REQUEST_REMOVE_FAVORITES:{var _list4=draft.byList[action.listId];var matchingItemIndex=_list4.items.findIndex(function(_ref4){var productId=_ref4.productId;return productId===action.productId;});if(matchingItemIndex>-1){_list4.items.splice(matchingItemIndex,1);}_list4.lastChange=Date.now();_list4.syncCount+=1;break;}// Handle cancellation of synchronization. // Sync count needs to be updated, when an add or a remove favorites action is cancelled // This recovers from invalid sync states when a backend call is detected to be redundant case CANCEL_REQUEST_SYNC_FAVORITES:{var _list5=draft.byList[action.listId];_list5.syncCount-=action.count;break;}// Handle success of adding favorite list products. case SUCCESS_ADD_FAVORITES:case SUCCESS_UPDATE_FAVORITES:case SUCCESS_REMOVE_FAVORITES:{var _list6=draft.byList[action.listId];_list6.lastChange=Date.now();_list6.syncCount-=1;break;}// Handle deletion failure by adding the product back in to the list. case ERROR_REMOVE_FAVORITES:{var _list7=draft.byList[action.listId];_list7.items.push({productId:action.productId,quantity:action.quantity||1,notes:action.notes||''});_list7.lastChange=Date.now();_list7.syncCount-=1;break;}// Handle adding failure by removing the product from the list. case ERROR_ADD_FAVORITES:{var _list8=draft.byList[action.listId];var _matchingItemIndex=_list8.items.findIndex(function(_ref5){var productId=_ref5.productId;return productId===action.productId;});if(_matchingItemIndex>-1){_list8.items.splice(_matchingItemIndex,1);}_list8.lastChange=Date.now();_list8.syncCount-=1;break;}case ERROR_UPDATE_FAVORITES:{var _list9=draft.byList[action.listId];_list9.lastChange=Date.now();_list9.syncCount-=1;break;}// Handle cleanup after deletion of a list. case SUCCESS_REMOVE_FAVORITES_LIST:{delete draft.byList[action.listId];break;}// Handle adding new lists. case SUCCESS_ADD_FAVORITES_LIST:{draft.byList[action.listId]={isFetching:true,lastChange:0,lastFetch:0,expires:0,items:[],syncCount:0,ready:true};break;}// Handle cleanup after lists are updated case RECEIVE_FAVORITES_LISTS:{var listIds=action.favoritesLists.map(function(_ref6){var id=_ref6.id;return id;});Object.keys(draft.byList).forEach(function(id){if(!listIds.includes(id)){// Remove list items that don't have a list anymore delete draft.byList[id];}});break;}default:break;}});/* eslint-enable no-param-reassign */return producedState;};export default products;