UNPKG

@open-tender/cloud

Version:

A library of hooks, reducers, utility functions, and types for use with Open Tender applications that utilize our cloud-based Order API.

196 lines (195 loc) 9.5 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.configReducer = exports.selectCategoryType = exports.selectHasCatering = exports.selectOutpostName = exports.selectHeaderHeight = exports.selectSoldOutMsg = exports.selectBottomTabsTitles = exports.selectBottomTabsDisplayed = exports.selectPage = exports.selectContentSection = exports.selectRecaptcha = exports.selectFulfillment = exports.selectShowLoyalty = exports.selectOptIns = exports.selectLightColor = exports.selectConfigRetries = exports.selectDisplaySettings = exports.selectCateringOnly = exports.selectAccountConfig = exports.selectApi = exports.selectPages = exports.selectStylesheet = exports.selectSettings = exports.selectContent = exports.selectTheme = exports.selectBrand = exports.selectConfig = exports.incrementRetries = exports.resetRetries = exports.resetConfig = exports.fetchConfig = exports.ConfigActionType = void 0; const tslib_1 = require("tslib"); const toolkit_1 = require("@reduxjs/toolkit"); const utils_1 = require("@open-tender/utils"); const types_1 = require("./types"); const api_1 = tslib_1.__importDefault(require("../services/api")); const initialState = { api: null, app: null, appTheme: null, brand: null, content: null, error: null, isApp: false, isBackgroundFetch: false, loading: 'idle', pages: null, retries: 0, settings: null, stylesheet: null, theme: null }; var ConfigActionType; (function (ConfigActionType) { ConfigActionType["FetchConfig"] = "config/getConfig"; })(ConfigActionType = exports.ConfigActionType || (exports.ConfigActionType = {})); exports.fetchConfig = (0, toolkit_1.createAsyncThunk)(ConfigActionType.FetchConfig, (options, { rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const api = new api_1.default(options); const response = yield api.getConfig(); const { baseUrl, authUrl, clientId, brandId, callback, isApp } = options; const app = { baseUrl, authUrl, clientId, brandId }; if (!app.clientId) app.clientId = response.clientId; if (!app.brandId) app.brandId = response.brand.brandId; if (callback) callback(); return Object.assign(Object.assign({}, response), { isApp, app }); } catch (err) { return rejectWithValue(err); } })); const configSlice = (0, toolkit_1.createSlice)({ name: types_1.ReducerType.Config, initialState: initialState, reducers: { resetConfig: () => initialState, resetRetries: state => { state.retries = 0; }, incrementRetries: state => { state.retries = state.retries + 1; } }, extraReducers: builder => { builder .addCase(exports.fetchConfig.fulfilled, (state, action) => { const { app, isApp, brand, content, theme, settings, stylesheet, pages } = action.payload; state.app = app; state.brand = brand; state.content = content; state.theme = !isApp && theme ? theme : null; state.appTheme = isApp && theme ? (0, utils_1.decorateTheme)(theme) : null; state.settings = settings; state.stylesheet = stylesheet; state.pages = pages; state.loading = 'idle'; state.api = new api_1.default(app); state.error = null; state.retries = 0; }) .addCase(exports.fetchConfig.pending, (state, action) => { var _a; state.loading = 'pending'; state.isBackgroundFetch = ((_a = action === null || action === void 0 ? void 0 : action.meta) === null || _a === void 0 ? void 0 : _a.arg) !== undefined ? true : false; }) .addCase(exports.fetchConfig.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }); } }); _a = configSlice.actions, exports.resetConfig = _a.resetConfig, exports.resetRetries = _a.resetRetries, exports.incrementRetries = _a.incrementRetries; const selectConfig = (state) => state.config; exports.selectConfig = selectConfig; const selectBrand = (state) => state.config.brand; exports.selectBrand = selectBrand; const selectTheme = (state) => state.config.theme; exports.selectTheme = selectTheme; const selectContent = (state) => state.config.content; exports.selectContent = selectContent; const selectSettings = (state) => state.config.settings; exports.selectSettings = selectSettings; const selectStylesheet = (state) => state.config.stylesheet; exports.selectStylesheet = selectStylesheet; const selectPages = (state) => state.config.pages; exports.selectPages = selectPages; const selectApi = (state) => state.config.api; exports.selectApi = selectApi; const selectAccountConfig = (state) => { var _a; return (_a = state.config.content) === null || _a === void 0 ? void 0 : _a.account; }; exports.selectAccountConfig = selectAccountConfig; const selectCateringOnly = (state) => { const { orderTypes } = state.config.settings || {}; return !!((orderTypes === null || orderTypes === void 0 ? void 0 : orderTypes.includes('CATERING')) && orderTypes.length === 1); }; exports.selectCateringOnly = selectCateringOnly; const selectDisplaySettings = (state) => { var _a; const orderType = state.order.orderType || 'OLO'; return (_a = state.config.settings) === null || _a === void 0 ? void 0 : _a.displaySettings[orderType]; }; exports.selectDisplaySettings = selectDisplaySettings; const selectConfigRetries = (state) => state.config.retries; exports.selectConfigRetries = selectConfigRetries; const selectLightColor = (state) => state.config.theme ? state.config.theme.colors.light : '#ffffff'; exports.selectLightColor = selectLightColor; exports.selectOptIns = (0, toolkit_1.createSelector)((state) => { const { accepts_marketing, order_notifications } = state.config.brand || {}; return { accepts_marketing, order_notifications }; }, ({ accepts_marketing, order_notifications }) => { return { accepts_marketing, order_notifications }; }); exports.selectShowLoyalty = (0, toolkit_1.createSelector)((state) => { const { has_loyalty, tpls } = state.config.brand || {}; return { has_loyalty, tpls }; }, ({ has_loyalty, tpls }) => { if (has_loyalty) return true; if (tpls === 'PUNCHH' || tpls === 'COMO') return true; return false; }); const selectFulfillment = (state) => { var _a; return (_a = state.config.brand) === null || _a === void 0 ? void 0 : _a.fulfillment; }; exports.selectFulfillment = selectFulfillment; const selectRecaptcha = (state) => { var _a; return (_a = state.config.settings) === null || _a === void 0 ? void 0 : _a.recaptcha; }; exports.selectRecaptcha = selectRecaptcha; const selectContentSection = (page) => (state) => state.config.content ? state.config.content[page] : undefined; exports.selectContentSection = selectContentSection; const selectPage = (page) => (state) => state.config.pages ? state.config.pages[page] : undefined; exports.selectPage = selectPage; const selectBottomTabsDisplayed = (state) => { var _a; return ((_a = state.config.content) === null || _a === void 0 ? void 0 : _a.bottomTabs.displayed) || []; }; exports.selectBottomTabsDisplayed = selectBottomTabsDisplayed; exports.selectBottomTabsTitles = (0, toolkit_1.createSelector)((state) => { const { bottomTabs } = state.config.content || {}; return bottomTabs; }, bottomTabs => { if (!bottomTabs) return {}; return Object.entries(bottomTabs).reduce((obj, [key, value]) => { if (key === 'displayed') return obj; return Object.assign(Object.assign({}, obj), { [key]: value.title }); }, {}); }); const selectSoldOutMsg = (state) => { var _a; const { soldOutMessage } = ((_a = state.config.content) === null || _a === void 0 ? void 0 : _a.menu) || {}; return soldOutMessage || 'Sold out for day'; }; exports.selectSoldOutMsg = selectSoldOutMsg; const selectHeaderHeight = (state) => (isBrowser) => { if (!state.config.theme) return 0; const { navHeight, navHeightMobile } = state.config.theme.layout; const height = isBrowser ? navHeight : navHeightMobile; return parseInt(height.replace('rem', '')) * 10; }; exports.selectHeaderHeight = selectHeaderHeight; const selectOutpostName = (state) => { var _a, _b, _c; return ((_a = state.config.settings) === null || _a === void 0 ? void 0 : _a.locationName.OUTPOST) ? (0, utils_1.capitalize)((_c = (_b = state.config.settings) === null || _b === void 0 ? void 0 : _b.locationName.OUTPOST[0]) !== null && _c !== void 0 ? _c : '') : undefined; }; exports.selectOutpostName = selectOutpostName; const selectHasCatering = (state) => { var _a; const { orderTypes } = state.config.settings || {}; return (_a = orderTypes === null || orderTypes === void 0 ? void 0 : orderTypes.includes('CATERING')) !== null && _a !== void 0 ? _a : false; }; exports.selectHasCatering = selectHasCatering; const selectCategoryType = (isMobile) => (state) => { const { categories } = state.config.theme || {}; if (!categories) return 'LIST'; const { desktop, mobile } = categories; return isMobile ? mobile.displayType : desktop.displayType; }; exports.selectCategoryType = selectCategoryType; exports.configReducer = configSlice.reducer;