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.

223 lines (222 loc) 10.6 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.customerGiftCardsReducer = exports.selectCustomerGiftCards = exports.setCustomerGiftCards = exports.resetCustomerGiftCardsError = exports.resetCustomerGiftCards = exports.assignCustomerGiftCardOther = exports.assignCustomerGiftCard = exports.addCustomerGiftCard = exports.removeCustomerGiftCard = exports.updateCustomerGiftCard = exports.fetchCustomerGiftCards = exports.CustomerGiftCardsActionType = 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 account_1 = require("./account"); const notifications_1 = require("../notifications"); const initialState = { entities: [], error: null, loading: 'idle', lookup: {} }; var CustomerGiftCardsActionType; (function (CustomerGiftCardsActionType) { CustomerGiftCardsActionType["FetchCustomerGiftCards"] = "customer/fetchCustomerGiftCards"; CustomerGiftCardsActionType["RemoveCustomerGiftCard"] = "customer/removeCustomerGiftCard"; CustomerGiftCardsActionType["AddCustomerGiftCard"] = "customer/addCustomerGiftCard"; CustomerGiftCardsActionType["AssignCustomerGiftCard"] = "customer/assignCustomerGiftCard"; CustomerGiftCardsActionType["AssignCustomerGiftCardOther"] = "customer/assignCustomerGiftCardOther"; CustomerGiftCardsActionType["UpdateCustomerGiftCard"] = "customer/updateCustomerGiftCard"; })(CustomerGiftCardsActionType = exports.CustomerGiftCardsActionType || (exports.CustomerGiftCardsActionType = {})); exports.fetchCustomerGiftCards = (0, toolkit_1.createAsyncThunk)(CustomerGiftCardsActionType.FetchCustomerGiftCards, (_, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const api = getState().config.api; const token = (0, account_1.selectToken)(getState()); if (!token) throw new Error(types_2.MISSING_CUSTOMER); return yield api.getCustomerGiftCards(token); } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); exports.updateCustomerGiftCard = (0, toolkit_1.createAsyncThunk)(CustomerGiftCardsActionType.UpdateCustomerGiftCard, ({ giftCardId, data, callback }, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const api = getState().config.api; const token = (0, account_1.selectToken)(getState()); if (!token) throw new Error(types_2.MISSING_CUSTOMER); yield api.putCustomerGiftCard(token, giftCardId, data); const giftCards = yield api.getCustomerGiftCards(token); dispatch((0, notifications_1.showNotification)('Gift card balance updated!')); if (callback) callback(); return giftCards; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); exports.removeCustomerGiftCard = (0, toolkit_1.createAsyncThunk)(CustomerGiftCardsActionType.RemoveCustomerGiftCard, ({ giftCardId, callback }, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const api = getState().config.api; const token = (0, account_1.selectToken)(getState()); if (!token) throw new Error(types_2.MISSING_CUSTOMER); yield api.deleteCustomerGiftCard(token, giftCardId); const giftCards = yield api.getCustomerGiftCards(token); dispatch((0, notifications_1.showNotification)('Gift card removed!')); if (callback) callback(); return giftCards; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); exports.addCustomerGiftCard = (0, toolkit_1.createAsyncThunk)(CustomerGiftCardsActionType.AddCustomerGiftCard, ({ data, callback }, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const api = getState().config.api; const token = (0, account_1.selectToken)(getState()); if (!token) throw new Error(types_2.MISSING_CUSTOMER); yield api.postCustomerGiftCard(token, data); const giftCards = yield api.getCustomerGiftCards(token); dispatch((0, notifications_1.showNotification)('Gift card added!')); if (callback) callback(); return giftCards; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); exports.assignCustomerGiftCard = (0, toolkit_1.createAsyncThunk)(CustomerGiftCardsActionType.AssignCustomerGiftCard, ({ cardNumber, callback }, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const api = getState().config.api; const token = (0, account_1.selectToken)(getState()); if (!token) throw new Error(types_2.MISSING_CUSTOMER); yield api.postCustomerGiftCardAssign(token, cardNumber); const giftCards = yield api.getCustomerGiftCards(token); dispatch((0, notifications_1.showNotification)('Gift card added to your account!')); if (callback) callback(); return giftCards; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); exports.assignCustomerGiftCardOther = (0, toolkit_1.createAsyncThunk)(CustomerGiftCardsActionType.AssignCustomerGiftCardOther, ({ giftCardId, email, callback }, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const api = getState().config.api; const token = (0, account_1.selectToken)(getState()); if (!token) throw new Error(types_2.MISSING_CUSTOMER); yield api.postCustomerGiftCardAssignOther(token, giftCardId, email); const giftCards = yield api.getCustomerGiftCards(token); dispatch((0, notifications_1.showNotification)(`Gift card assigned to ${email}`)); if (callback) callback(); return giftCards; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); const customerGiftCardsSlice = (0, toolkit_1.createSlice)({ name: types_1.ReducerType.GiftCards, initialState, reducers: { resetCustomerGiftCards: () => initialState, resetCustomerGiftCardsError: state => { state.error = null; state.loading = 'idle'; }, setCustomerGiftCards: (state, action) => { state.entities = action.payload; state.error = null; } }, extraReducers: builder => { builder .addCase(exports.fetchCustomerGiftCards.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.fetchCustomerGiftCards.pending, state => { state.loading = 'pending'; }) .addCase(exports.fetchCustomerGiftCards.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }) .addCase(exports.removeCustomerGiftCard.fulfilled, (state, action) => { state.loading = 'idle'; state.error = null; state.entities = action.payload; }) .addCase(exports.removeCustomerGiftCard.pending, state => { state.loading = 'pending'; }) .addCase(exports.removeCustomerGiftCard.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }) .addCase(exports.addCustomerGiftCard.fulfilled, (state, action) => { state.loading = 'idle'; state.error = null; state.entities = action.payload; }) .addCase(exports.addCustomerGiftCard.pending, state => { state.loading = 'pending'; }) .addCase(exports.addCustomerGiftCard.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }) .addCase(exports.updateCustomerGiftCard.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.updateCustomerGiftCard.pending, state => { state.loading = 'pending'; }) .addCase(exports.updateCustomerGiftCard.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }) .addCase(exports.assignCustomerGiftCardOther.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.assignCustomerGiftCardOther.pending, state => { state.loading = 'pending'; }) .addCase(exports.assignCustomerGiftCardOther.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }) .addCase(exports.assignCustomerGiftCard.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.assignCustomerGiftCard.pending, state => { state.loading = 'pending'; }) .addCase(exports.assignCustomerGiftCard.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }); } }); _a = customerGiftCardsSlice.actions, exports.resetCustomerGiftCards = _a.resetCustomerGiftCards, exports.resetCustomerGiftCardsError = _a.resetCustomerGiftCardsError, exports.setCustomerGiftCards = _a.setCustomerGiftCards; const selectCustomerGiftCards = (state) => state.customer.giftCards; exports.selectCustomerGiftCards = selectCustomerGiftCards; exports.customerGiftCardsReducer = customerGiftCardsSlice.reducer;