@open-tender/store
Version:
A library of hooks, reducers, utility functions, and types for use with Open Tender applications that utilize our in-store POS API
296 lines (295 loc) • 12.8 kB
JavaScript
var _a;
import { __awaiter, __generator, __spreadArray } from "tslib";
import { addItem, calcCartCounts, dateToIso, decrementItem, getUserTimezone, incrementItem, makeMenuItemLookup, makeRandomNumberString, makeSimpleCart, orderTypeNamesMap, rehydrateCart, removeItem, serviceTypeNamesMap } from '@open-tender/utils';
import { createAsyncThunk, createSelector, createSlice } from '@reduxjs/toolkit';
import { ReducerType } from './types';
var initialState = {
orderId: null,
orderType: null,
serviceType: null,
revenueCenterId: null,
table: null,
prepType: null,
requestedAt: 'asap',
currentVendor: null,
currentCategory: null,
currentItem: null,
currentOption: null,
currentSection: null,
cart: [],
cartCounts: {},
messages: [],
alert: null,
error: null,
loading: 'idle',
deviceType: 'KIOSK'
};
export var OrderActionType;
(function (OrderActionType) {
OrderActionType["Reorder"] = "orders/reorder";
})(OrderActionType || (OrderActionType = {}));
export var reorder = createAsyncThunk(OrderActionType.Reorder, function (orderCart_1, _a) { return __awaiter(void 0, [orderCart_1, _a], void 0, function (orderCart, _b) {
var _c, config, order, revenueCenterId, serviceType, requestedAt, categories, menuItems, simpleCart, _d, cart, cartCounts, err_1;
var getState = _b.getState, rejectWithValue = _b.rejectWithValue;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
_e.trys.push([0, 2, , 3]);
_c = getState(), config = _c.config, order = _c.order;
revenueCenterId = order.revenueCenterId || 0;
serviceType = order.serviceType || 'WALKIN';
requestedAt = dateToIso(new Date(), getUserTimezone());
return [4 /*yield*/, config.api.getMenu(revenueCenterId, serviceType, requestedAt)];
case 1:
categories = (_e.sent()).menu;
menuItems = Object.values(makeMenuItemLookup(categories));
simpleCart = makeSimpleCart(orderCart);
_d = rehydrateCart(menuItems, simpleCart), cart = _d.cart, cartCounts = _d.cartCounts;
return [2 /*return*/, { cart: cart, cartCounts: cartCounts }];
case 2:
err_1 = _e.sent();
return [2 /*return*/, rejectWithValue(err_1)];
case 3: return [2 /*return*/];
}
});
}); });
var orderSlice = createSlice({
name: ReducerType.Orders,
initialState: initialState,
reducers: {
resetOrder: function () { return initialState; },
resetOrderType: function (state) {
state.orderId = null;
state.orderType = null;
state.serviceType = null;
state.prepType = null;
state.revenueCenterId = null;
state.table = null;
state.requestedAt = 'asap';
},
resetRevenueCenter: function (state) {
state.revenueCenterId = null;
},
resetLocation: function (state) {
state.revenueCenterId = null;
},
resetCart: function (state) {
state.cart = [];
state.cartCounts = {};
},
resetMessages: function (state) {
state.messages = [];
},
resetAlert: function (state) {
state.alert = null;
},
setAlert: function (state, action) {
state.alert = action.payload;
},
setOrderId: function (state, action) {
state.orderId = action.payload;
},
setOrderType: function (state, action) {
state.orderType = action.payload;
},
setServiceType: function (state, action) {
state.serviceType = action.payload;
},
setRequestedAt: function (state, action) {
state.requestedAt = action.payload;
},
setRevenueCenterId: function (state, action) {
state.revenueCenterId = action.payload;
},
setTable: function (state, action) {
state.table = action.payload;
},
setPrepType: function (state, action) {
state.prepType = action.payload;
},
setOrderServiceType: function (state, action) {
var _a = action.payload, orderType = _a.orderType, serviceType = _a.serviceType;
state.orderType = orderType;
state.serviceType = serviceType;
},
setMenuVars: function (state, action) {
var _a = action.payload, revenueCenterId = _a.revenueCenterId, serviceType = _a.serviceType, requestedAt = _a.requestedAt;
state.revenueCenterId = revenueCenterId;
state.serviceType = serviceType;
state.requestedAt = requestedAt;
},
setCart: function (state, action) {
state.cart = action.payload;
state.cartCounts = calcCartCounts(action.payload);
},
setCurrentVendor: function (state, action) {
state.currentVendor = action.payload;
},
setCurrentCategory: function (state, action) {
state.currentCategory = action.payload;
},
setCurrentItem: function (state, action) {
state.currentItem = action.payload;
},
setCurrentOption: function (state, action) {
state.currentOption = action.payload;
},
setCurrentSection: function (state, action) {
state.currentSection = action.payload;
},
setOrderDeviceType: function (state, action) {
state.deviceType = action.payload;
},
addItemToCart: function (state, action) {
var _a = addItem(__spreadArray([], state.cart, true), action.payload), cart = _a.cart, cartCounts = _a.cartCounts;
state.cart = cart;
state.cartCounts = cartCounts;
},
removeItemFromCart: function (state, action) {
var index = action.payload.index;
var _a = removeItem(__spreadArray([], state.cart, true), index), cart = _a.cart, cartCounts = _a.cartCounts;
state.cart = cart;
state.cartCounts = cartCounts;
},
incrementItemInCart: function (state, action) {
var index = action.payload.index;
var _a = incrementItem(__spreadArray([], state.cart, true), index), cart = _a.cart, cartCounts = _a.cartCounts;
state.cart = cart;
state.cartCounts = cartCounts;
},
decrementItemInCart: function (state, action) {
var index = action.payload.index;
var _a = decrementItem(__spreadArray([], state.cart, true), index), cart = _a.cart, cartCounts = _a.cartCounts;
state.cart = cart;
state.cartCounts = cartCounts;
},
addMessage: function (state, action) {
var existing = state.messages.map(function (i) { return i.message; });
if (!existing.includes(action.payload)) {
state.messages = __spreadArray(__spreadArray([], state.messages, true), [
{
message: action.payload,
id: makeRandomNumberString()
}
], false);
}
},
removeMessage: function (state, action) {
state.messages = state.messages.filter(function (i) { return i.id !== action.payload; });
}
},
extraReducers: function (builder) {
builder
.addCase(reorder.fulfilled, function (state, action) {
state.cart = action.payload.cart;
state.cartCounts = action.payload.cartCounts;
state.serviceType = 'WALKIN';
state.orderType = 'OLO';
state.prepType = 'EAT_HERE';
state.loading = 'idle';
state.error = null;
})
.addCase(reorder.pending, function (state) {
state.loading = 'pending';
})
.addCase(reorder.rejected, function (state, action) {
state.error = action.payload;
state.loading = 'idle';
});
}
});
export var resetOrder = (_a = orderSlice.actions, _a.resetOrder), resetOrderType = _a.resetOrderType, resetRevenueCenter = _a.resetRevenueCenter, resetLocation = _a.resetLocation, resetCart = _a.resetCart, resetMessages = _a.resetMessages, resetAlert = _a.resetAlert, setAlert = _a.setAlert, setOrderId = _a.setOrderId, setOrderType = _a.setOrderType, setServiceType = _a.setServiceType, setRequestedAt = _a.setRequestedAt, setRevenueCenterId = _a.setRevenueCenterId, setTable = _a.setTable, setPrepType = _a.setPrepType, setOrderServiceType = _a.setOrderServiceType, setMenuVars = _a.setMenuVars, setCart = _a.setCart, setCurrentVendor = _a.setCurrentVendor, setCurrentCategory = _a.setCurrentCategory, setCurrentItem = _a.setCurrentItem, setCurrentOption = _a.setCurrentOption, setCurrentSection = _a.setCurrentSection, addItemToCart = _a.addItemToCart, removeItemFromCart = _a.removeItemFromCart, incrementItemInCart = _a.incrementItemInCart, decrementItemInCart = _a.decrementItemInCart, addMessage = _a.addMessage, removeMessage = _a.removeMessage, setOrderDeviceType = _a.setOrderDeviceType;
export var selectOrder = function (state) { return state.order; };
export var selectAlert = function (state) { return state.order.alert; };
export var selectMessages = function (state) { return state.order.messages; };
export var selectOrderType = function (state) { return state.order.orderType; };
export var selectOrderTypeName = function (state) {
return orderTypeNamesMap[state.order.orderType];
};
export var selectServiceType = function (state) { return state.order.serviceType; };
export var selectServiceTypeName = function (state) {
return serviceTypeNamesMap[state.order.serviceType];
};
export var selectRequestedAt = function (state) {
return state.order.requestedAt === 'asap' ? 'ASAP' : state.order.requestedAt;
};
export var selectRevenueCenterId = function (state) {
return state.order.revenueCenterId;
};
export var selectMenuSlug = function (state) {
var slug = (state.config.store || {}).slug;
return slug ? "/menu/".concat(slug) : '/';
};
export var selectMenuVars = function (state) {
return {
revenueCenterId: state.order.revenueCenterId,
serviceType: state.order.serviceType,
requestedAt: state.order.requestedAt
};
};
export var selectCurrentVendor = function (state) {
return state.order.currentVendor;
};
export var selectCurrentCategory = function (state) {
return state.order.currentCategory;
};
export var selectCurrentItem = function (state) { return state.order.currentItem; };
export var selectCurrentOption = function (state) {
return state.order.currentOption;
};
export var selectCurrentSection = function (state) {
return state.order.currentSection;
};
export var selectCart = function (state) { return state.order.cart; };
export var selectCartIds = createSelector(function (state) {
var cart = state.order.cart;
return cart;
}, function (cart) {
if (!cart)
return [];
return cart.map(function (i) { return i.id; });
});
export var selectCartQuantity = createSelector(function (state) {
var cart = state.order.cart;
return cart;
}, function (cart) {
if (!cart)
return 0;
return cart.reduce(function (t, i) { return (t += i.quantity); }, 0);
});
export var selectCartTotal = createSelector(function (state) {
var cart = state.order.cart;
return cart;
}, function (cart) {
if (!cart)
return 0.0;
return cart.reduce(function (t, i) { return (t += i.totalPrice || 0); }, 0.0);
});
export var selectCartTotals = createSelector(function (state) {
var cart = state.order.cart;
return cart;
}, function (cart) {
if (!cart)
return { count: 0, total: 0.0 };
var count = cart.reduce(function (t, i) { return (t += i.quantity); }, 0);
var total = cart.reduce(function (t, i) { return (t += i.totalPrice || 0); }, 0.0);
return { count: count, total: total };
});
export var selectCartCounts = function (state) {
return state.order.cartCounts || {};
};
export var selectCanOrder = function (state) {
return state.order.revenueCenterId &&
state.order.serviceType &&
state.order.requestedAt;
};
export var selectCartValidate = function (state) {
var _a = state.order || {}, revenueCenterId = _a.revenueCenterId, serviceType = _a.serviceType, requestedAt = _a.requestedAt, cart = _a.cart;
return {
revenueCenterId: revenueCenterId,
serviceType: serviceType,
requestedAt: requestedAt,
cart: cart
};
};
export var orderReducer = orderSlice.reducer;