@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
86 lines (85 loc) • 3.44 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 ErrorAlertsActionType;
(function (ErrorAlertsActionType) {
ErrorAlertsActionType["FetchErrorAlerts"] = "errorAlerts/fetchErrorAlerts";
ErrorAlertsActionType["DismissErrorAlert"] = "errorAlerts/dismissErrorAlert";
})(ErrorAlertsActionType || (ErrorAlertsActionType = {}));
export var fetchErrorAlerts = createAsyncThunk(ErrorAlertsActionType.FetchErrorAlerts, function (_1, _a) { return __awaiter(void 0, [_1, _a], void 0, function (_, _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.getAlerts()];
case 1: return [2 /*return*/, _c.sent()];
case 2:
err_1 = _c.sent();
return [2 /*return*/, rejectWithValue(err_1)];
case 3: return [2 /*return*/];
}
});
}); });
export var dismissErrorAlert = createAsyncThunk(ErrorAlertsActionType.DismissErrorAlert, function (alertId_1, _a) { return __awaiter(void 0, [alertId_1, _a], void 0, function (alertId, _b) {
var api, err_2;
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.deleteAlert(alertId)];
case 1:
_c.sent();
return [2 /*return*/];
case 2:
err_2 = _c.sent();
return [2 /*return*/, rejectWithValue(err_2)];
case 3: return [2 /*return*/];
}
});
}); });
var errorAlertsSlice = createSlice({
name: ReducerType.ErrorAlerts,
initialState: initialState,
reducers: {
resetErrorAlerts: function () { return initialState; }
},
extraReducers: function (builder) {
builder
.addCase(fetchErrorAlerts.fulfilled, function (state, action) {
state.entities = action.payload || [];
state.loading = 'idle';
state.error = null;
})
.addCase(fetchErrorAlerts.pending, function (state) {
state.loading = 'pending';
})
.addCase(fetchErrorAlerts.rejected, function (state, action) {
state.error = action.payload;
state.loading = 'idle';
})
.addCase(dismissErrorAlert.fulfilled, function (state) {
state.loading = 'idle';
state.error = null;
})
.addCase(dismissErrorAlert.pending, function (state) {
state.loading = 'pending';
})
.addCase(dismissErrorAlert.rejected, function (state, action) {
state.error = action.payload;
state.loading = 'idle';
});
}
});
export var resetErrorAlerts = errorAlertsSlice.actions.resetErrorAlerts;
export var selectErrorAlerts = function (state) { return state.errorAlerts; };
export var errorAlertsReducer = errorAlertsSlice.reducer;