@open-tender/store
Version:
A library of hooks, reducers, utility functions, and types for use with Open Tender applications that utilize our in-store POS API
62 lines (61 loc) • 2.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.discountsReducer = exports.selectDiscounts = exports.resetDiscounts = exports.fetchDiscounts = exports.DiscountsActionType = void 0;
var tslib_1 = require("tslib");
var toolkit_1 = require("@reduxjs/toolkit");
var types_1 = require("./types");
var initialState = {
entities: [],
loading: 'idle',
error: null
};
var DiscountsActionType;
(function (DiscountsActionType) {
DiscountsActionType["GetDiscounts"] = "discounts/fetchDiscounts";
})(DiscountsActionType || (exports.DiscountsActionType = DiscountsActionType = {}));
exports.fetchDiscounts = (0, toolkit_1.createAsyncThunk)(DiscountsActionType.GetDiscounts, function (_1, _a) { return tslib_1.__awaiter(void 0, [_1, _a], void 0, function (_, _b) {
var api, _c, serviceType, orderType, err_1;
var getState = _b.getState, rejectWithValue = _b.rejectWithValue;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
_d.trys.push([0, 2, , 3]);
api = getState().config.api;
_c = getState().order, serviceType = _c.serviceType, orderType = _c.orderType;
if (!serviceType || !orderType)
return [2 /*return*/, []];
return [4 /*yield*/, api.getDiscounts(serviceType, orderType)];
case 1: return [2 /*return*/, _d.sent()];
case 2:
err_1 = _d.sent();
return [2 /*return*/, rejectWithValue(err_1)];
case 3: return [2 /*return*/];
}
});
}); });
var discountsSlice = (0, toolkit_1.createSlice)({
name: types_1.ReducerType.Discounts,
initialState: initialState,
reducers: {
resetDiscounts: function () { return initialState; }
},
extraReducers: function (builder) {
builder
.addCase(exports.fetchDiscounts.fulfilled, function (state, action) {
state.entities = action.payload;
state.loading = 'idle';
state.error = null;
})
.addCase(exports.fetchDiscounts.pending, function (state) {
state.loading = 'pending';
})
.addCase(exports.fetchDiscounts.rejected, function (state, action) {
state.error = action.payload;
state.loading = 'idle';
});
}
});
exports.resetDiscounts = discountsSlice.actions.resetDiscounts;
var selectDiscounts = function (state) { return state.discounts; };
exports.selectDiscounts = selectDiscounts;
exports.discountsReducer = discountsSlice.reducer;