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.

89 lines (88 loc) 3.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.pointsShopReducer = exports.selectPointsShop = exports.resetPointsShop = exports.exchangePointsShopReward = exports.fetchPointsShop = exports.PointsShopActionType = 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: [], data: null, error: null, loading: 'idle' }; var PointsShopActionType; (function (PointsShopActionType) { PointsShopActionType["FetchPointsShop"] = "pointsShop/fetchPointsShop"; PointsShopActionType["ExchangePointsShopReward"] = "pointsShop/exchangePointsShopReward"; })(PointsShopActionType = exports.PointsShopActionType || (exports.PointsShopActionType = {})); exports.fetchPointsShop = (0, toolkit_1.createAsyncThunk)(PointsShopActionType.FetchPointsShop, (_, { getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const { api } = getState().config; if (!api) return; const token = (0, account_1.selectToken)(getState()); if (!token) throw new Error(types_2.MISSING_CUSTOMER); return yield api.getPointsShop(token); } catch (err) { return rejectWithValue(err); } })); exports.exchangePointsShopReward = (0, toolkit_1.createAsyncThunk)(PointsShopActionType.ExchangePointsShopReward, (rewardId, { getState, dispatch, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const { api } = getState().config; if (!api) return { data: null, entities: [] }; const token = (0, account_1.selectToken)(getState()); if (!token) throw new Error(types_2.MISSING_CUSTOMER); yield api.postPointsShopReward(token, rewardId); dispatch((0, notifications_1.showNotification)('Reward successfully redeemed!')); return yield api.getPointsShop(token); } catch (err) { return rejectWithValue(err); } })); const pointsShopSlice = (0, toolkit_1.createSlice)({ name: types_1.ReducerType.PointsShop, initialState, reducers: { resetPointsShop: () => initialState }, extraReducers: builder => { builder .addCase(exports.fetchPointsShop.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.fetchPointsShop.pending, state => { state.loading = 'pending'; }) .addCase(exports.fetchPointsShop.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }) .addCase(exports.exchangePointsShopReward.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.exchangePointsShopReward.pending, state => { state.loading = 'pending'; }) .addCase(exports.exchangePointsShopReward.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }); } }); exports.resetPointsShop = pointsShopSlice.actions.resetPointsShop; const selectPointsShop = (state) => state.customer.pointsShop; exports.selectPointsShop = selectPointsShop; exports.pointsShopReducer = pointsShopSlice.reducer;