UNPKG

@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

140 lines (139 loc) 7.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.customerReducer = exports.selectPointsProgram = exports.selectCustomerPoints = exports.selectCustomerPointsProgram = exports.selectCustomerLoyaltyProgram = exports.selectCustomerLoyalty = exports.selectCustomerLoyaltySummary = exports.selectCustomerRewards = exports.selectCustomerOrders = exports.selectCustomerGiftCards = exports.selectCustomerFavorites = exports.selectCustomerDeals = exports.selectCustomerAccount = exports.selectCustomer = exports.resetCustomer = exports.fetchCustomer = exports.CustomerActionType = void 0; var tslib_1 = require("tslib"); var toolkit_1 = require("@reduxjs/toolkit"); var types_1 = require("./types"); var initialState = { account: null, deals: [], allergens: [], favorites: [], giftCards: [], loyalty: [], orders: [], rewards: [], loading: 'idle', error: null }; var CustomerActionType; (function (CustomerActionType) { CustomerActionType["FetchCustomer"] = "customer/fetchCustomer"; })(CustomerActionType || (exports.CustomerActionType = CustomerActionType = {})); exports.fetchCustomer = (0, toolkit_1.createAsyncThunk)(CustomerActionType.FetchCustomer, function (_a, _b) { return tslib_1.__awaiter(void 0, [_a, _b], void 0, function (_c, _d) { var api, err_1; var customerId = _c.customerId, endpoints = _c.endpoints; var getState = _d.getState, rejectWithValue = _d.rejectWithValue; return tslib_1.__generator(this, function (_e) { switch (_e.label) { case 0: _e.trys.push([0, 2, , 3]); api = getState().config.api; return [4 /*yield*/, api.getCustomer(customerId, endpoints)]; case 1: return [2 /*return*/, _e.sent()]; case 2: err_1 = _e.sent(); return [2 /*return*/, rejectWithValue(err_1)]; case 3: return [2 /*return*/]; } }); }); }); var CustomerSlice = (0, toolkit_1.createSlice)({ name: types_1.ReducerType.Customer, initialState: initialState, reducers: { resetCustomer: function () { return initialState; } }, extraReducers: function (builder) { builder .addCase(exports.fetchCustomer.fulfilled, function (state, action) { var _a = action.payload, _b = _a.ACCOUNT, ACCOUNT = _b === void 0 ? null : _b, _c = _a.DEALS, DEALS = _c === void 0 ? [] : _c, _d = _a.FAVORITES, FAVORITES = _d === void 0 ? [] : _d, _e = _a.GIFT_CARDS, GIFT_CARDS = _e === void 0 ? [] : _e, _f = _a.LOYALTY, LOYALTY = _f === void 0 ? [] : _f, _g = _a.ORDERS, ORDERS = _g === void 0 ? [] : _g, _h = _a.REWARDS, REWARDS = _h === void 0 ? [] : _h, _j = _a.ALLERGENS, ALLERGENS = _j === void 0 ? [] : _j; state.account = ACCOUNT; state.deals = DEALS; state.favorites = FAVORITES; state.giftCards = GIFT_CARDS; state.loyalty = LOYALTY; state.orders = ORDERS; state.rewards = REWARDS; state.allergens = ALLERGENS; state.loading = 'idle'; state.error = null; }) .addCase(exports.fetchCustomer.pending, function (state) { state.loading = 'pending'; }) .addCase(exports.fetchCustomer.rejected, function (state, action) { state.error = action.payload; state.loading = 'idle'; }); } }); exports.resetCustomer = CustomerSlice.actions.resetCustomer; var selectCustomer = function (state) { return state.customer; }; exports.selectCustomer = selectCustomer; var selectCustomerAccount = function (state) { return state.customer.account; }; exports.selectCustomerAccount = selectCustomerAccount; var selectCustomerDeals = function (state) { return state.customer.deals; }; exports.selectCustomerDeals = selectCustomerDeals; var selectCustomerFavorites = function (state) { return state.customer.favorites; }; exports.selectCustomerFavorites = selectCustomerFavorites; var selectCustomerGiftCards = function (state) { return state.customer.giftCards; }; exports.selectCustomerGiftCards = selectCustomerGiftCards; var selectCustomerOrders = function (state) { return state.customer.orders; }; exports.selectCustomerOrders = selectCustomerOrders; var selectCustomerRewards = function (state) { return state.customer.rewards; }; exports.selectCustomerRewards = selectCustomerRewards; exports.selectCustomerLoyaltySummary = (0, toolkit_1.createSelector)(function (state) { var loyalty = state.customer.loyalty; return loyalty[0] || null; }, function (loyaltyProgram) { if (!loyaltyProgram) return null; var name = loyaltyProgram.name, perk = loyaltyProgram.perk, status = loyaltyProgram.status; var _a = perk || {}, _b = _a.loyalty_perk, loyalty_perk = _b === void 0 ? null : _b, _c = _a.credit, credit = _c === void 0 ? null : _c, _d = _a.thresholds, thresholds = _d === void 0 ? [] : _d; var isPoints = loyalty_perk === 'BANKABLE_POINTS'; var points = isPoints && credit ? parseInt(credit) : null; var _e = status || {}, _f = _e.progress, progress = _f === void 0 ? null : _f, _g = _e.tiers, tiers = _g === void 0 ? [] : _g; var tier = tiers.find(function (i) { return i.is_current_tier; }) || null; return { name: name, perkType: loyalty_perk, points: points, credit: isPoints ? null : credit, thresholds: thresholds, progress: isPoints && progress ? parseInt(progress) : progress, tier: tier }; }); var selectCustomerLoyalty = function (state) { return state.customer.loyalty; }; exports.selectCustomerLoyalty = selectCustomerLoyalty; exports.selectCustomerLoyaltyProgram = (0, toolkit_1.createSelector)(function (state) { return state.customer; }, function (_a) { var entities = _a.loyalty, loading = _a.loading, error = _a.error; var programs = entities.filter(function (i) { return i.earn_order_type === null || i.earn_order_type === 'OLO'; }); var program = programs.length ? programs[0] : null; return { program: program, loading: loading, error: error }; }); exports.selectCustomerPointsProgram = (0, toolkit_1.createSelector)(function (state) { return state.customer.loyalty; }, function (state) { return state.order.orderType; }, function (loyaltyPrograms, orderType) { if (!loyaltyPrograms) return null; var programs = loyaltyPrograms.filter(function (i) { var _a; return ((_a = i.perk) === null || _a === void 0 ? void 0 : _a.loyalty_perk) === 'BANKABLE_POINTS' && (i.earn_order_type === null || i.earn_order_type === orderType); }); return programs.length ? programs[0] : null; }); exports.selectCustomerPoints = (0, toolkit_1.createSelector)(exports.selectCustomerPointsProgram, function (program) { if (!program || !program.perk) return null; var spend_name = program.spend_name, perk = program.perk; return { name: spend_name, points: perk.credit }; }); exports.selectPointsProgram = (0, toolkit_1.createSelector)(function (state) { return state.order.orderType; }, exports.selectCustomerPointsProgram, function (orderType, program) { return orderType ? program : null; }); exports.customerReducer = CustomerSlice.reducer;