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.

68 lines (67 loc) 2.93 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.customerRewardsReducer = exports.selectCustomerRewards = exports.setCustomerRewards = exports.resetCustomerRewardsError = exports.resetCustomerRewards = exports.fetchCustomerRewards = exports.CustomerRewardsActionType = 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 initialState = { entities: [], error: null, loading: 'idle', lookup: {} }; var CustomerRewardsActionType; (function (CustomerRewardsActionType) { CustomerRewardsActionType["FetchCustomerRewards"] = "customer/fetchCustomerRewards"; })(CustomerRewardsActionType = exports.CustomerRewardsActionType || (exports.CustomerRewardsActionType = {})); exports.fetchCustomerRewards = (0, toolkit_1.createAsyncThunk)(CustomerRewardsActionType.FetchCustomerRewards, (_, { 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 rewards = yield api.getCustomerRewards(token); return rewards; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); const customerRewardsSlice = (0, toolkit_1.createSlice)({ name: types_1.ReducerType.Rewards, initialState, reducers: { resetCustomerRewards: () => initialState, resetCustomerRewardsError: state => { state.error = null; state.loading = 'idle'; }, setCustomerRewards: (state, action) => { state.entities = action.payload; state.error = null; } }, extraReducers: builder => { builder .addCase(exports.fetchCustomerRewards.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.fetchCustomerRewards.pending, state => { state.loading = 'pending'; }) .addCase(exports.fetchCustomerRewards.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }); } }); _a = customerRewardsSlice.actions, exports.resetCustomerRewards = _a.resetCustomerRewards, exports.resetCustomerRewardsError = _a.resetCustomerRewardsError, exports.setCustomerRewards = _a.setCustomerRewards; const selectCustomerRewards = (state) => state.customer.rewards; exports.selectCustomerRewards = selectCustomerRewards; exports.customerRewardsReducer = customerRewardsSlice.reducer;