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

129 lines (128 loc) 6.22 kB
import { __awaiter, __generator } from "tslib"; import { createAsyncThunk, createSelector, createSlice } from '@reduxjs/toolkit'; import { ReducerType } from './types'; var initialState = { account: null, deals: [], allergens: [], favorites: [], giftCards: [], loyalty: [], orders: [], rewards: [], loading: 'idle', error: null }; export var CustomerActionType; (function (CustomerActionType) { CustomerActionType["FetchCustomer"] = "customer/fetchCustomer"; })(CustomerActionType || (CustomerActionType = {})); export var fetchCustomer = createAsyncThunk(CustomerActionType.FetchCustomer, function (_a, _b) { return __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 __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 = createSlice({ name: ReducerType.Customer, initialState: initialState, reducers: { resetCustomer: function () { return initialState; } }, extraReducers: function (builder) { builder .addCase(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(fetchCustomer.pending, function (state) { state.loading = 'pending'; }) .addCase(fetchCustomer.rejected, function (state, action) { state.error = action.payload; state.loading = 'idle'; }); } }); export var resetCustomer = CustomerSlice.actions.resetCustomer; export var selectCustomer = function (state) { return state.customer; }; export var selectCustomerAccount = function (state) { return state.customer.account; }; export var selectCustomerDeals = function (state) { return state.customer.deals; }; export var selectCustomerFavorites = function (state) { return state.customer.favorites; }; export var selectCustomerGiftCards = function (state) { return state.customer.giftCards; }; export var selectCustomerOrders = function (state) { return state.customer.orders; }; export var selectCustomerRewards = function (state) { return state.customer.rewards; }; export var selectCustomerLoyaltySummary = 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 }; }); export var selectCustomerLoyalty = function (state) { return state.customer.loyalty; }; export var selectCustomerLoyaltyProgram = 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 }; }); export var selectCustomerPointsProgram = 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; }); export var selectCustomerPoints = createSelector(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 }; }); export var selectPointsProgram = createSelector(function (state) { return state.order.orderType; }, selectCustomerPointsProgram, function (orderType, program) { return orderType ? program : null; }); export var customerReducer = CustomerSlice.reducer;