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.

171 lines (170 loc) 8.15 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.customerCreditCardsReducer = exports.selectCustomerCreditCardsForPayment = exports.selectCustomerCreditCards = exports.setCustomerCreditCards = exports.resetCustomerCreditCardsError = exports.resetCustomerCreditCards = exports.addCustomerCreditCard = exports.removeCustomerCreditCard = exports.updateCustomerCreditCard = exports.fetchCustomerCreditCards = exports.CustomerCreditCardsActionType = 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: [], lookup: {}, loading: 'idle', error: null }; var CustomerCreditCardsActionType; (function (CustomerCreditCardsActionType) { CustomerCreditCardsActionType["FetchCustomerCreditCards"] = "customer/fetchCustomerCreditCards"; CustomerCreditCardsActionType["UpdateCustomerCreditCard"] = "customer/updateCustomerCreditCard"; CustomerCreditCardsActionType["RemoveCustomerCreditCard"] = "customer/removeCustomerCreditCard"; CustomerCreditCardsActionType["AddCustomerCreditCard"] = "customer/addCustomerCreditCard"; })(CustomerCreditCardsActionType = exports.CustomerCreditCardsActionType || (exports.CustomerCreditCardsActionType = {})); exports.fetchCustomerCreditCards = (0, toolkit_1.createAsyncThunk)(CustomerCreditCardsActionType.FetchCustomerCreditCards, (includeLinked, { 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); const creditCards = yield api.getCustomerCreditCards(token, includeLinked); return creditCards; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); exports.updateCustomerCreditCard = (0, toolkit_1.createAsyncThunk)(CustomerCreditCardsActionType.UpdateCustomerCreditCard, ({ cardId, 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.putCustomerCreditCard(token, cardId, data); const includeLinked = true; const creditCards = yield api.getCustomerCreditCards(token, includeLinked); dispatch((0, notifications_1.showNotification)('Credit card updated!')); if (callback) callback(); return creditCards; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); exports.removeCustomerCreditCard = (0, toolkit_1.createAsyncThunk)(CustomerCreditCardsActionType.RemoveCustomerCreditCard, ({ cardId, 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.deleteCustomerCreditCard(token, cardId); const includeLinked = true; const creditCards = yield api.getCustomerCreditCards(token, includeLinked); dispatch((0, notifications_1.showNotification)('Credit card removed!')); if (callback) callback(); return creditCards; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); exports.addCustomerCreditCard = (0, toolkit_1.createAsyncThunk)(CustomerCreditCardsActionType.AddCustomerCreditCard, ({ 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.postCustomerCreditCard(token, data); const includeLinked = true; const creditCards = yield api.getCustomerCreditCards(token, includeLinked); dispatch((0, notifications_1.showNotification)('Credit card added!')); if (callback) callback(); return creditCards; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); const customerCreditCardsSlice = (0, toolkit_1.createSlice)({ name: types_1.ReducerType.CreditCards, initialState, reducers: { resetCustomerCreditCards: () => initialState, resetCustomerCreditCardsError: state => { state.error = null; state.loading = 'idle'; }, setCustomerCreditCards: (state, action) => { state.entities = action.payload; state.error = null; } }, extraReducers: builder => { builder .addCase(exports.fetchCustomerCreditCards.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.fetchCustomerCreditCards.pending, state => { state.loading = 'pending'; }) .addCase(exports.fetchCustomerCreditCards.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }) .addCase(exports.updateCustomerCreditCard.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.updateCustomerCreditCard.pending, state => { state.loading = 'pending'; }) .addCase(exports.updateCustomerCreditCard.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }) .addCase(exports.removeCustomerCreditCard.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.removeCustomerCreditCard.pending, state => { state.loading = 'pending'; }) .addCase(exports.removeCustomerCreditCard.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }) .addCase(exports.addCustomerCreditCard.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.addCustomerCreditCard.pending, state => { state.loading = 'pending'; }) .addCase(exports.addCustomerCreditCard.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }); } }); _a = customerCreditCardsSlice.actions, exports.resetCustomerCreditCards = _a.resetCustomerCreditCards, exports.resetCustomerCreditCardsError = _a.resetCustomerCreditCardsError, exports.setCustomerCreditCards = _a.setCustomerCreditCards; const selectCustomerCreditCards = (state) => state.customer.creditCards; exports.selectCustomerCreditCards = selectCustomerCreditCards; exports.selectCustomerCreditCardsForPayment = (0, toolkit_1.createSelector)((state) => { const creditCards = state.customer.creditCards.entities; return creditCards; }, creditCards => { return creditCards.filter(i => i.has_profile); }); exports.customerCreditCardsReducer = customerCreditCardsSlice.reducer;