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

190 lines (189 loc) 8.66 kB
var _a; import { __assign, __awaiter, __generator } from "tslib"; import { currentLocalDateStr, getUserTimezone } from '@open-tender/utils'; import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'; import { PosAPI } from '../services'; import { errorsApi } from '../services/errors'; import { setKdsItemTypes, setKdsPrepStations } from './kds'; import { setRevenueCenterId } from './order'; import { setCashier } from './pos'; import { ReducerType } from './types'; // const defaultApiUrl = `http://${window.location.hostname}:5001/pos-api` var defaultApiUrl = "http://localhost:5001/pos-api"; var initialState = { api: new 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 }; export var ConfigActionType; (function (ConfigActionType) { ConfigActionType["FetchConfig"] = "config/getConfig"; })(ConfigActionType || (ConfigActionType = {})); export var fetchConfig = createAsyncThunk(ConfigActionType.FetchConfig, function (_a, _b) { return __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 __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 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 = 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 = getUserTimezone(); businessDate = currentLocalDateStr(tz); dispatch(setCashier(__assign(__assign({}, employee), { businessDate: businessDate }))); return [3 /*break*/, 5]; case 4: err_1 = _l.sent(); return [3 /*break*/, 5]; case 5: dispatch(setRevenueCenterId(store.revenue_center_id)); dispatch(setKdsItemTypes(itemTypes)); dispatch(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 = createSlice({ name: 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 PosAPI(action.payload); }, setAccessibility: function (state, action) { state.accessibilityOn = action.payload; } }, extraReducers: function (builder) { builder .addCase(fetchConfig.pending, function (state) { state.loading = 'pending'; }) .addCase(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(fetchConfig.rejected, function (state, action) { state.error = action.payload; state.loading = 'idle'; }); } }); export var resetConfig = (_a = configSlice.actions, _a.resetConfig), resetRetries = _a.resetRetries, incrementRetries = _a.incrementRetries, setVersion = _a.setVersion, setApi = _a.setApi, setApiTerminal = _a.setApiTerminal, setAccessibility = _a.setAccessibility; export var selectConfig = function (state) { return state.config; }; export var selectApi = function (state) { return state.config.api; }; export var selectVersion = function (state) { return state.config.version; }; export var selectDevices = function (state) { return state.config.devices; }; export var selectSelectOptions = function (state) { return state.config.selectOptions || []; }; export var selectMenuColors = function (state) { return state.config.menuColors || []; }; // export const selectTimezone = (state: AppState): Timezone => // state.config.store // ? timezoneMap[state.config.store.timezone as TimezonePython] // : (getUserTimezone() as Timezone) export var selectTimezone = function () { return getUserTimezone(); }; export var selectLoyalty = function (state) { return state.config.store ? state.config.store.has_loyalty : false; }; export var selectPinpad = function (state) { return state.config.hasPinpad; }; export var selectCashDrawer = function (state) { return state.config.hasCashDrawer; }; export var selectAccessibility = function (state) { return state.config.accessibilityOn; }; export var selectStore = function (state) { return state.config.store; }; export var configReducer = configSlice.reducer;