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.

352 lines (351 loc) 16.3 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.accountReducer = exports.selectCustomer = exports.selectToken = exports.setCustomerAuth = exports.resetLoginError = exports.resetCustomer = exports.deleteCustomer = exports.authCustomerThanx = exports.loginCustomerThanx = exports.sendCustomerVerificationEmail = exports.updateCustomer = exports.linkPosToken = exports.logoutCustomer = exports.loginCustomer = exports.fetchCustomer = exports.checkAuth = exports.AccountActionType = void 0; const tslib_1 = require("tslib"); const toolkit_1 = require("@reduxjs/toolkit"); const types_1 = require("../types"); const types_2 = require("@open-tender/types"); const utils_1 = require("@open-tender/utils"); const notifications_1 = require("../notifications"); const allergens_1 = require("./allergens"); const allergens_2 = require("../allergens"); const favorites_1 = require("./favorites"); const creditCards_1 = require("./creditCards"); const addresses_1 = require("./addresses"); const houseAccounts_1 = require("./houseAccounts"); const loyalty_1 = require("./loyalty"); const order_1 = require("./order"); const orders_1 = require("./orders"); const rewards_1 = require("./rewards"); const communicationPreferences_1 = require("./communicationPreferences"); const giftCards_1 = require("./giftCards"); const order_2 = require("../order"); const checkout_1 = require("../checkout"); const groupOrder_1 = require("../groupOrder"); const guest_1 = require("../guest"); const tpls_1 = require("./tpls"); const initialState = { auth: null, profile: null, loading: 'idle', error: null }; var AccountActionType; (function (AccountActionType) { AccountActionType["LoginCustomer"] = "customer/loginCustomer"; AccountActionType["LogoutCustomer"] = "customer/logoutCustomer"; AccountActionType["FetchCustomer"] = "customer/fetchCustomer"; AccountActionType["UpdateCustomer"] = "customer/updateCustomer"; AccountActionType["VerifyCustomer"] = "customer/verifyCustomer"; AccountActionType["LinkPosToken"] = "customer/linkPosToken"; AccountActionType["DeleteCustomer"] = "customer/deleteCustomer"; AccountActionType["LoginCustomerThanx"] = "customer/loginCustomerThanx"; AccountActionType["AuthCustomerThanx"] = "customer/authCustomerThanx"; })(AccountActionType = exports.AccountActionType || (exports.AccountActionType = {})); const checkAuth = (err, dispatch, rejectWithValue) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { if ((err === null || err === void 0 ? void 0 : err.status) === 401) { yield dispatch((0, exports.logoutCustomer)()); dispatch((0, order_2.addMessage)('Please login to reauthenticate your account')); return null; } else { return rejectWithValue(); } }); exports.checkAuth = checkAuth; exports.fetchCustomer = (0, toolkit_1.createAsyncThunk)(AccountActionType.FetchCustomer, // eslint-disable-next-line no-unused-vars (_, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const api = getState().config.api; const token = (0, exports.selectToken)(getState()); if (!token) throw new Error(types_2.MISSING_CUSTOMER); const customer = yield api.getCustomer(token); const { allergens, gift_cards, favorites } = customer; dispatch((0, allergens_1.setCustomerAllergens)(allergens || [])); dispatch((0, allergens_2.setSelectedAllergens)(allergens || [])); dispatch((0, giftCards_1.setCustomerGiftCards)(gift_cards || [])); dispatch((0, favorites_1.setCustomerFavorites)(favorites || [])); const lookup = (0, utils_1.makeFavoritesLookup)(favorites); dispatch((0, favorites_1.setCustomerFavoritesLookup)(lookup || {})); const profile = (0, utils_1.makeCustomerProfile)(customer); return profile; } catch (err) { const error = err; return (0, exports.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); exports.loginCustomer = (0, toolkit_1.createAsyncThunk)(AccountActionType.LoginCustomer, ({ email, password, recaptcha_token }, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const api = getState().config.api; const auth = yield api.postLogin(email, password, recaptcha_token); dispatch((0, exports.setCustomerAuth)(auth)); yield dispatch((0, exports.fetchCustomer)()); return; } catch (err) { return rejectWithValue(err); } })); exports.logoutCustomer = (0, toolkit_1.createAsyncThunk)(AccountActionType.LogoutCustomer, (isReset, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const { api } = getState().config; const token = (0, exports.selectToken)(getState()); if (!token) throw new Error(undefined); if (isReset) { dispatch((0, order_2.resetOrder)()); dispatch((0, checkout_1.resetCheckout)()); dispatch((0, allergens_2.setSelectedAllergens)(null)); } yield api.postLogout(token); dispatch((0, checkout_1.updateCheckoutCustomer)(null)); dispatch((0, addresses_1.resetCustomerAddresses)()); dispatch((0, allergens_1.resetCustomerAllergens)()); dispatch((0, creditCards_1.resetCustomerCreditCards)()); dispatch((0, favorites_1.resetCustomerFavorites)()); dispatch((0, giftCards_1.resetCustomerGiftCards)()); dispatch((0, houseAccounts_1.resetCustomerHouseAccounts)()); dispatch((0, loyalty_1.resetCustomerLoyalty)()); dispatch((0, order_1.resetCustomerOrder)()); dispatch((0, orders_1.resetCustomerOrders)()); dispatch((0, groupOrder_1.resetGroupOrder)()); dispatch((0, rewards_1.resetCustomerRewards)()); dispatch((0, communicationPreferences_1.resetCustomerCommunicationPreferences)()); dispatch((0, guest_1.resetGuest)()); dispatch((0, tpls_1.resetCustomerTpls)()); return null; } catch (err) { return rejectWithValue(err); } })); exports.linkPosToken = (0, toolkit_1.createAsyncThunk)(AccountActionType.LinkPosToken, (posToken, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const api = getState().config.api; const token = (0, exports.selectToken)(getState()); if (!token) throw new Error(types_2.MISSING_CUSTOMER); yield api.postCustomerPosToken(token, posToken); dispatch((0, notifications_1.showNotification)('Order successfully linked!')); dispatch((0, exports.fetchCustomer)()); return; } catch (err) { const error = err; dispatch((0, order_2.addMessage)((error === null || error === void 0 ? void 0 : error.detail) || (error === null || error === void 0 ? void 0 : error.message))); return rejectWithValue(error); } })); exports.updateCustomer = (0, toolkit_1.createAsyncThunk)(AccountActionType.UpdateCustomer, ({ data, callback }, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const token = (0, exports.selectToken)(getState()); const api = getState().config.api; if (!token) throw new Error(types_2.MISSING_CUSTOMER); const customer = yield api.putCustomer(token, data); const profile = (0, utils_1.makeCustomerProfile)(customer); dispatch((0, notifications_1.showNotification)('Account updated!')); if (callback) callback(data); return profile; } catch (err) { const error = err; return (0, exports.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); exports.sendCustomerVerificationEmail = (0, toolkit_1.createAsyncThunk)(AccountActionType.VerifyCustomer, (linkUrl, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const api = getState().config.api; const token = (0, exports.selectToken)(getState()); if (!token) { dispatch((0, order_2.addMessage)('Missing customer token')); throw new Error(types_2.MISSING_CUSTOMER_TOKEN); } yield api.postSendVerificationEmail(token, linkUrl); dispatch((0, notifications_1.showNotification)('Verification email sent!')); return; } catch (err) { dispatch((0, order_2.addMessage)(err.detail || err.message)); return rejectWithValue(err); } })); exports.loginCustomerThanx = (0, toolkit_1.createAsyncThunk)(AccountActionType.LoginCustomerThanx, ({ email, origin, recaptcha_token }, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const api = getState().config.api; yield api.postThanxLogin(email, origin, recaptcha_token); dispatch((0, order_2.setAlert)({ type: 'close' })); dispatch((0, order_2.addMessage)('Thanks! Please check your email on this device.')); return; } catch (err) { const errr = err; if (errr) { const error = (errr === null || errr === void 0 ? void 0 : errr.params) ? errr.params['$.email'] : null; if (error && error.includes("'email'")) { errr.detail = 'Please enter a valid email address'; } } return rejectWithValue(errr); } })); exports.authCustomerThanx = (0, toolkit_1.createAsyncThunk)(AccountActionType.AuthCustomerThanx, (data, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const api = getState().config.api; const auth = yield api.postThanxAuth(data.code, data.path, data.origin); dispatch((0, exports.setCustomerAuth)(auth)); yield dispatch((0, exports.fetchCustomer)()); return; } catch (err) { return rejectWithValue(err); } })); exports.deleteCustomer = (0, toolkit_1.createAsyncThunk)(AccountActionType.DeleteCustomer, ({ data, callback }, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const token = (0, exports.selectToken)(getState()); const api = getState().config.api; if (!token) throw new Error(types_2.MISSING_CUSTOMER); yield api.deleteCustomer(token); yield api.postLogout(token); dispatch((0, checkout_1.updateCheckoutCustomer)(null)); dispatch((0, addresses_1.resetCustomerAddresses)()); dispatch((0, allergens_1.resetCustomerAllergens)()); dispatch((0, creditCards_1.resetCustomerCreditCards)()); dispatch((0, favorites_1.resetCustomerFavorites)()); dispatch((0, giftCards_1.resetCustomerGiftCards)()); dispatch((0, houseAccounts_1.resetCustomerHouseAccounts)()); dispatch((0, loyalty_1.resetCustomerLoyalty)()); dispatch((0, order_1.resetCustomerOrder)()); dispatch((0, orders_1.resetCustomerOrders)()); dispatch((0, groupOrder_1.resetGroupOrder)()); dispatch((0, rewards_1.resetCustomerRewards)()); dispatch((0, communicationPreferences_1.resetCustomerCommunicationPreferences)()); dispatch((0, guest_1.resetGuest)()); dispatch((0, notifications_1.showNotification)('Account deleted!')); if (callback) callback(data); return; } catch (err) { const error = err; return (0, exports.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); const accountSlice = (0, toolkit_1.createSlice)({ name: types_1.ReducerType.Account, initialState, reducers: { resetCustomer: () => initialState, resetLoginError: state => { state.loading = 'idle'; state.error = null; }, setCustomerAuth: (state, action) => { state.auth = action.payload; // state.loading = 'idle' // state.error = null } }, extraReducers: builder => { builder .addCase(exports.loginCustomer.fulfilled, state => { // state.auth = action.payload state.loading = 'idle'; state.error = null; }) .addCase(exports.loginCustomer.pending, state => { state.loading = 'pending'; }) .addCase(exports.loginCustomer.rejected, (state, action) => { state.loading = 'idle'; state.error = action.payload; }) .addCase(exports.fetchCustomer.fulfilled, (state, action) => { state.profile = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.fetchCustomer.pending, state => { state.loading = 'pending'; }) .addCase(exports.fetchCustomer.rejected, (state, action) => { state.loading = 'idle'; state.error = action.payload; }) .addCase(exports.updateCustomer.fulfilled, (state, action) => { state.profile = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.updateCustomer.pending, state => { state.loading = 'pending'; }) .addCase(exports.updateCustomer.rejected, (state, action) => { state.loading = 'idle'; state.error = action.payload; }) .addCase(exports.logoutCustomer.fulfilled, () => initialState) .addCase(exports.logoutCustomer.pending, state => { state.loading = 'pending'; }) .addCase(exports.logoutCustomer.rejected, () => initialState) .addCase(exports.sendCustomerVerificationEmail.fulfilled, state => { state.loading = 'idle'; }) .addCase(exports.sendCustomerVerificationEmail.pending, state => { state.loading = 'pending'; }) .addCase(exports.sendCustomerVerificationEmail.rejected, state => { state.loading = 'idle'; }) .addCase(exports.linkPosToken.fulfilled, () => { //do nothing }) .addCase(exports.linkPosToken.pending, () => { //do nothing }) .addCase(exports.linkPosToken.rejected, () => { //do nothing }) .addCase(exports.deleteCustomer.fulfilled, () => initialState) .addCase(exports.deleteCustomer.pending, state => { state.loading = 'pending'; }) .addCase(exports.deleteCustomer.rejected, () => initialState) .addCase(exports.loginCustomerThanx.fulfilled, state => { state.loading = 'idle'; state.error = null; }) .addCase(exports.loginCustomerThanx.pending, state => { state.loading = 'pending'; }) .addCase(exports.loginCustomerThanx.rejected, (state, action) => { state.loading = 'idle'; state.error = action.payload; }) .addCase(exports.authCustomerThanx.fulfilled, state => { state.loading = 'idle'; state.error = null; }) .addCase(exports.authCustomerThanx.pending, state => { state.loading = 'pending'; }) .addCase(exports.authCustomerThanx.rejected, (state, action) => { state.loading = 'idle'; state.error = action.payload; }); } }); _a = accountSlice.actions, exports.resetCustomer = _a.resetCustomer, exports.resetLoginError = _a.resetLoginError, exports.setCustomerAuth = _a.setCustomerAuth; const selectToken = (state) => state.customer.account.auth ? state.customer.account.auth.access_token : null; exports.selectToken = selectToken; const selectCustomer = (state) => state.customer.account; exports.selectCustomer = selectCustomer; exports.accountReducer = accountSlice.reducer;