@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
65 lines (64 loc) • 2.45 kB
JavaScript
var _a;
import { __awaiter, __generator } from "tslib";
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import { ReducerType } from './types';
var initialState = {
businessDate: null,
entities: [],
loading: 'idle',
error: null
};
export var PunchesActionType;
(function (PunchesActionType) {
PunchesActionType["FetchPunches"] = "punches/getPunches";
})(PunchesActionType || (PunchesActionType = {}));
export var fetchPunches = createAsyncThunk(PunchesActionType.FetchPunches, function (businessDate_1, _a) { return __awaiter(void 0, [businessDate_1, _a], void 0, function (businessDate, _b) {
var api, err_1;
var getState = _b.getState, rejectWithValue = _b.rejectWithValue;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_c.trys.push([0, 2, , 3]);
api = getState().config.api;
return [4 /*yield*/, api.getTimePunchesReport(businessDate)];
case 1: return [2 /*return*/, _c.sent()];
case 2:
err_1 = _c.sent();
return [2 /*return*/, rejectWithValue(err_1)];
case 3: return [2 /*return*/];
}
});
}); });
var punchesSlice = createSlice({
name: ReducerType.Punches,
initialState: initialState,
reducers: {
resetPunches: function () { return initialState; },
setPunchesBusinessDate: function (state, action) {
state.businessDate = action.payload;
}
},
extraReducers: function (builder) {
builder
.addCase(fetchPunches.fulfilled, function (state, action) {
if (action.payload) {
state.entities = action.payload;
}
state.loading = 'idle';
state.error = null;
})
.addCase(fetchPunches.pending, function (state) {
state.loading = 'pending';
})
.addCase(fetchPunches.rejected, function (state, action) {
state.error = action.payload;
state.loading = 'idle';
});
}
});
export var resetPunches = (_a = punchesSlice.actions, _a.resetPunches), setPunchesBusinessDate = _a.setPunchesBusinessDate;
export var selectPunches = function (state) { return state.punches; };
export var selectPunchesBusinessDate = function (state) {
return state.punches.businessDate;
};
export var punchesReducer = punchesSlice.reducer;