@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
58 lines (57 loc) • 2.31 kB
JavaScript
import { __awaiter, __generator } from "tslib";
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import { ReducerType } from './types';
var initialState = {
entities: [],
loading: 'idle',
error: null
};
export var DiscountsActionType;
(function (DiscountsActionType) {
DiscountsActionType["GetDiscounts"] = "discounts/fetchDiscounts";
})(DiscountsActionType || (DiscountsActionType = {}));
export var fetchDiscounts = createAsyncThunk(DiscountsActionType.GetDiscounts, function (_1, _a) { return __awaiter(void 0, [_1, _a], void 0, function (_, _b) {
var api, _c, serviceType, orderType, err_1;
var getState = _b.getState, rejectWithValue = _b.rejectWithValue;
return __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 = createSlice({
name: ReducerType.Discounts,
initialState: initialState,
reducers: {
resetDiscounts: function () { return initialState; }
},
extraReducers: function (builder) {
builder
.addCase(fetchDiscounts.fulfilled, function (state, action) {
state.entities = action.payload;
state.loading = 'idle';
state.error = null;
})
.addCase(fetchDiscounts.pending, function (state) {
state.loading = 'pending';
})
.addCase(fetchDiscounts.rejected, function (state, action) {
state.error = action.payload;
state.loading = 'idle';
});
}
});
export var resetDiscounts = discountsSlice.actions.resetDiscounts;
export var selectDiscounts = function (state) { return state.discounts; };
export var discountsReducer = discountsSlice.reducer;