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.

103 lines (102 loc) 4.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.customerTplsReducer = exports.selectCustomerTplsProgress = exports.selectCustomerTplsRewards = exports.selectCustomerTplsPoints = exports.selectCustomerTpls = exports.resetCustomerTpls = exports.verifyCustomerTpls = exports.fetchCustomerTpls = exports.CustomerTplsActionType = 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 = { tpls: null, error: null, loading: 'idle' }; var CustomerTplsActionType; (function (CustomerTplsActionType) { CustomerTplsActionType["FetchCustomerTpls"] = "customer/fetchCustomerTpls"; CustomerTplsActionType["VerifyCustomerTpls"] = "customer/verifyCustomerTpls"; })(CustomerTplsActionType = exports.CustomerTplsActionType || (exports.CustomerTplsActionType = {})); exports.fetchCustomerTpls = (0, toolkit_1.createAsyncThunk)(CustomerTplsActionType.FetchCustomerTpls, (_, { 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 resp = yield api.getCustomerTpls(token); return resp; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); exports.verifyCustomerTpls = (0, toolkit_1.createAsyncThunk)(CustomerTplsActionType.VerifyCustomerTpls, (_, { 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.postCustomerTplsVerify(token); dispatch((0, notifications_1.showNotification)('Verification email sent!')); return; } catch (err) { return rejectWithValue(err); } })); const customerTplsSlice = (0, toolkit_1.createSlice)({ name: types_1.ReducerType.Tpls, initialState, reducers: { resetCustomerTpls: () => initialState }, extraReducers: builder => { builder .addCase(exports.fetchCustomerTpls.fulfilled, (state, action) => { state.tpls = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.fetchCustomerTpls.pending, state => { state.loading = 'pending'; }) .addCase(exports.fetchCustomerTpls.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }) .addCase(exports.verifyCustomerTpls.fulfilled, state => { state.loading = 'idle'; state.error = null; }) .addCase(exports.verifyCustomerTpls.pending, state => { state.loading = 'pending'; }) .addCase(exports.verifyCustomerTpls.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }); } }); exports.resetCustomerTpls = customerTplsSlice.actions.resetCustomerTpls; const selectCustomerTpls = (state) => state.customer.tpls; exports.selectCustomerTpls = selectCustomerTpls; exports.selectCustomerTplsPoints = (0, toolkit_1.createSelector)((state) => { const { points } = state.customer.tpls.tpls || {}; return points; }, points => { return points ? points.balance : null; }); exports.selectCustomerTplsRewards = (0, toolkit_1.createSelector)((state) => { const { rewards } = state.customer.tpls.tpls || {}; return rewards; }, rewards => { return rewards !== null && rewards !== void 0 ? rewards : []; }); exports.selectCustomerTplsProgress = (0, toolkit_1.createSelector)((state) => { const { progress } = state.customer.tpls.tpls || {}; return progress; }, progress => { return progress !== null && progress !== void 0 ? progress : null; }); exports.customerTplsReducer = customerTplsSlice.reducer;