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

205 lines (204 loc) 9.84 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.configReducer = exports.selectStore = exports.selectAccessibility = exports.selectCashDrawer = exports.selectPinpad = exports.selectLoyalty = exports.selectTimezone = exports.selectMenuColors = exports.selectSelectOptions = exports.selectDevices = exports.selectVersion = exports.selectApi = exports.selectConfig = exports.setAccessibility = exports.setApiTerminal = exports.setApi = exports.setVersion = exports.incrementRetries = exports.resetRetries = exports.resetConfig = exports.fetchConfig = exports.ConfigActionType = void 0; var tslib_1 = require("tslib"); var utils_1 = require("@open-tender/utils"); var toolkit_1 = require("@reduxjs/toolkit"); var services_1 = require("../services"); var errors_1 = require("../services/errors"); var kds_1 = require("./kds"); var order_1 = require("./order"); var pos_1 = require("./pos"); var types_1 = require("./types"); // const defaultApiUrl = `http://${window.location.hostname}:5001/pos-api` var defaultApiUrl = "http://localhost:5001/pos-api"; var initialState = { api: new services_1.PosAPI({ apiUrl: defaultApiUrl }), app: null, version: null, devices: null, store: null, selectOptions: null, menuColors: null, name: null, ipAddress: null, hasPinpad: false, hasCashDrawer: true, loading: 'idle', error: null, retries: 0, accessibilityOn: false, paymentConfigs: null }; var ConfigActionType; (function (ConfigActionType) { ConfigActionType["FetchConfig"] = "config/getConfig"; })(ConfigActionType || (exports.ConfigActionType = ConfigActionType = {})); exports.fetchConfig = (0, toolkit_1.createAsyncThunk)(ConfigActionType.FetchConfig, function (_a, _b) { return tslib_1.__awaiter(void 0, [_a, _b], void 0, function (_c, _d) { var posTerminalId, kdsTerminalId, app, api, _e, _f, devicesList, _g, stores, _h, selectOptions, _j, itemTypes, _k, menuColors, paymentConfigs, store, devices, err, cashier, employee, tz, businessDate, err_1, err_2; var apiUrl = _c.apiUrl; var dispatch = _d.dispatch, getState = _d.getState, rejectWithValue = _d.rejectWithValue; return tslib_1.__generator(this, function (_l) { switch (_l.label) { case 0: _l.trys.push([0, 6, , 7]); posTerminalId = getState().pos.terminalId; kdsTerminalId = getState().kds.terminalId; app = posTerminalId ? { apiUrl: apiUrl, posTerminalId: posTerminalId } : kdsTerminalId ? { apiUrl: apiUrl, kdsTerminalId: kdsTerminalId } : { apiUrl: apiUrl }; api = new services_1.PosAPI(app); return [4 /*yield*/, Promise.all([ api.getSettings('DEVICES'), api.getSettings('STORE'), // TODO: replace getSelectOptions when formatted correctly // api.getSettings<SelectOptions>('SELECT_OPTION'), api.getSelectOptions(), api.getSettings('ITEM_TYPE'), api.getSettings('ITEM_COLORS'), api.fetchPaymentConfigs() ])]; case 1: _e = _l.sent(), _f = _e[0], devicesList = _f === void 0 ? [] : _f, _g = _e[1], stores = _g === void 0 ? [] : _g, _h = _e[2], selectOptions = _h === void 0 ? [] : _h, _j = _e[3], itemTypes = _j === void 0 ? [] : _j, _k = _e[4], menuColors = _k === void 0 ? [] : _k, paymentConfigs = _e[5]; store = stores[0] || null; devices = devicesList[0] || null; if (!store || !devices) { err = errors_1.errorsApi.internalServerError; return [2 /*return*/, rejectWithValue(err)]; } cashier = getState().pos.cashier; if (!!cashier) return [3 /*break*/, 5]; _l.label = 2; case 2: _l.trys.push([2, 4, , 5]); return [4 /*yield*/, api.getCashier() // TODO: figure out how to get the timezone from BE // const tz = timezoneMap[store.timezone] ]; case 3: employee = _l.sent(); tz = (0, utils_1.getUserTimezone)(); businessDate = (0, utils_1.currentLocalDateStr)(tz); dispatch((0, pos_1.setCashier)(tslib_1.__assign(tslib_1.__assign({}, employee), { businessDate: businessDate }))); return [3 /*break*/, 5]; case 4: err_1 = _l.sent(); return [3 /*break*/, 5]; case 5: dispatch((0, order_1.setRevenueCenterId)(store.revenue_center_id)); dispatch((0, kds_1.setKdsItemTypes)(itemTypes)); dispatch((0, kds_1.setKdsPrepStations)(devices.prep_stations)); return [2 /*return*/, { api: api, app: app, devices: devices, store: store, selectOptions: selectOptions, menuColors: menuColors, name: 'Name', ipAddress: '10.0.0.1', hasPinpad: true, hasCashDrawer: true, error: null, loading: 'idle', retries: 0, paymentConfigs: paymentConfigs }]; case 6: err_2 = _l.sent(); return [2 /*return*/, rejectWithValue(err_2)]; case 7: return [2 /*return*/]; } }); }); }); var configSlice = (0, toolkit_1.createSlice)({ name: types_1.ReducerType.Config, initialState: initialState, reducers: { resetConfig: function () { return initialState; }, resetRetries: function (state) { state.retries = 0; }, incrementRetries: function (state) { state.retries = state.retries + 1; }, setVersion: function (state, action) { state.version = action.payload; }, setApi: function (state, action) { state.api = action.payload; }, setApiTerminal: function (state, action) { state.api = new services_1.PosAPI(action.payload); }, setAccessibility: function (state, action) { state.accessibilityOn = action.payload; } }, extraReducers: function (builder) { builder .addCase(exports.fetchConfig.pending, function (state) { state.loading = 'pending'; }) .addCase(exports.fetchConfig.fulfilled, function (state, action) { var _a = action.payload, api = _a.api, app = _a.app, devices = _a.devices, store = _a.store, selectOptions = _a.selectOptions, menuColors = _a.menuColors, hasPinpad = _a.hasPinpad, hasCashDrawer = _a.hasCashDrawer, paymentConfigs = _a.paymentConfigs; state.api = api; state.app = app; state.store = store; state.devices = devices; state.selectOptions = selectOptions; state.menuColors = menuColors; state.hasPinpad = hasPinpad; state.hasCashDrawer = hasCashDrawer; state.loading = 'idle'; state.retries = 0; state.error = null; state.paymentConfigs = paymentConfigs; }) .addCase(exports.fetchConfig.rejected, function (state, action) { state.error = action.payload; state.loading = 'idle'; }); } }); exports.resetConfig = (_a = configSlice.actions, _a.resetConfig), exports.resetRetries = _a.resetRetries, exports.incrementRetries = _a.incrementRetries, exports.setVersion = _a.setVersion, exports.setApi = _a.setApi, exports.setApiTerminal = _a.setApiTerminal, exports.setAccessibility = _a.setAccessibility; var selectConfig = function (state) { return state.config; }; exports.selectConfig = selectConfig; var selectApi = function (state) { return state.config.api; }; exports.selectApi = selectApi; var selectVersion = function (state) { return state.config.version; }; exports.selectVersion = selectVersion; var selectDevices = function (state) { return state.config.devices; }; exports.selectDevices = selectDevices; var selectSelectOptions = function (state) { return state.config.selectOptions || []; }; exports.selectSelectOptions = selectSelectOptions; var selectMenuColors = function (state) { return state.config.menuColors || []; }; exports.selectMenuColors = selectMenuColors; // export const selectTimezone = (state: AppState): Timezone => // state.config.store // ? timezoneMap[state.config.store.timezone as TimezonePython] // : (getUserTimezone() as Timezone) var selectTimezone = function () { return (0, utils_1.getUserTimezone)(); }; exports.selectTimezone = selectTimezone; var selectLoyalty = function (state) { return state.config.store ? state.config.store.has_loyalty : false; }; exports.selectLoyalty = selectLoyalty; var selectPinpad = function (state) { return state.config.hasPinpad; }; exports.selectPinpad = selectPinpad; var selectCashDrawer = function (state) { return state.config.hasCashDrawer; }; exports.selectCashDrawer = selectCashDrawer; var selectAccessibility = function (state) { return state.config.accessibilityOn; }; exports.selectAccessibility = selectAccessibility; var selectStore = function (state) { return state.config.store; }; exports.selectStore = selectStore; exports.configReducer = configSlice.reducer;