@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.
107 lines (106 loc) • 4.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.tplsPointsShopReducer = exports.selectTplsPointsShop = exports.resetTplsPointsShop = exports.retroClaimTplsPointsShop = exports.exchangeTplsPointsShopReward = exports.fetchTplsPointsShop = exports.TplsPointsShopActionType = 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 TplsPointsShopActionType;
(function (TplsPointsShopActionType) {
TplsPointsShopActionType["FetchTplsPointsShop"] = "pointsShop/fetchTplsPointsShop";
TplsPointsShopActionType["ExchangeTplsPointsShopReward"] = "pointsShop/exchangeTplsPointsShopReward";
TplsPointsShopActionType["RetroClaimTplsPointsShop"] = "pointsShop/retroClaimTplsPointsShop";
})(TplsPointsShopActionType = exports.TplsPointsShopActionType || (exports.TplsPointsShopActionType = {}));
exports.fetchTplsPointsShop = (0, toolkit_1.createAsyncThunk)(TplsPointsShopActionType.FetchTplsPointsShop, (_, { 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.getTplsPointsShop(token);
}
catch (err) {
return rejectWithValue(err);
}
}));
exports.exchangeTplsPointsShopReward = (0, toolkit_1.createAsyncThunk)(TplsPointsShopActionType.ExchangeTplsPointsShopReward, (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);
const data = yield api.postTplsPointsShopReward(token, rewardId);
dispatch((0, notifications_1.showNotification)('Reward successfully redeemed!'));
const entities = yield api.getTplsPointsShop(token);
return { data, entities };
}
catch (err) {
return rejectWithValue(err);
}
}));
exports.retroClaimTplsPointsShop = (0, toolkit_1.createAsyncThunk)(TplsPointsShopActionType.RetroClaimTplsPointsShop, (receiptId, { 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);
yield api.retroClaimPoints(token, receiptId);
return;
}
catch (err) {
return rejectWithValue(err);
}
}));
const tplsPointsShopSlice = (0, toolkit_1.createSlice)({
name: types_1.ReducerType.TplsPointsShop,
initialState,
reducers: {
resetTplsPointsShop: () => initialState
},
extraReducers: builder => {
builder
.addCase(exports.fetchTplsPointsShop.fulfilled, (state, action) => {
state.entities = action.payload;
state.loading = 'idle';
state.error = null;
})
.addCase(exports.fetchTplsPointsShop.pending, state => {
state.loading = 'pending';
})
.addCase(exports.fetchTplsPointsShop.rejected, (state, action) => {
state.error = action.payload;
state.loading = 'idle';
})
.addCase(exports.exchangeTplsPointsShopReward.fulfilled, (state, action) => {
state.entities = action.payload.entities;
state.data = action.payload.data;
state.loading = 'idle';
state.error = null;
})
.addCase(exports.exchangeTplsPointsShopReward.pending, state => {
state.loading = 'pending';
})
.addCase(exports.exchangeTplsPointsShopReward.rejected, (state, action) => {
state.error = action.payload;
state.loading = 'idle';
});
}
});
exports.resetTplsPointsShop = tplsPointsShopSlice.actions.resetTplsPointsShop;
const selectTplsPointsShop = (state) => state.customer.tplsPointsShop;
exports.selectTplsPointsShop = selectTplsPointsShop;
exports.tplsPointsShopReducer = tplsPointsShopSlice.reducer;