spud-framework
Version:
Spud framework is a lightweight, strongly-typed, and declarative way to minimize boilerplate while using [thunks](https://github.com/reduxjs/redux-thunk) with [redux](https://react-redux.js.org/) for asynchronous actions, implemented as a [hook](https://r
69 lines • 2.63 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import { AsyncActionStatusConsts, } from './types';
export var setWaiting = function (opts) { return ({
type: opts.schema.type,
id: opts.id,
status: AsyncActionStatusConsts.Waiting,
}); };
export var setSuccess = function (opts, data) { return ({
type: opts.schema.type,
id: opts.id,
status: AsyncActionStatusConsts.Success,
payload: data,
}); };
export var setError = function (opts, error) { return ({
type: opts.schema.type,
id: opts.id,
status: AsyncActionStatusConsts.Error,
payload: error,
}); };
export var setIdle = function (id, type) { return ({
type: type,
id: id,
status: AsyncActionStatusConsts.Idle,
}); };
var createAsyncReducer = function (actionSchemas) {
var defaultState = Object.keys(actionSchemas).reduce(function (a, e) {
var _a;
return (__assign(__assign({}, a), (_a = {}, _a[actionSchemas[e].type] = {}, _a)));
}, {});
var reducer = function (state, action) {
var _a, _b, _c;
if (state === void 0) { state = defaultState; }
if (!action.status) {
return state;
}
if (action.status === AsyncActionStatusConsts.Idle) {
var _d = state[action.type], _e = action.id, _value = _d[_e], rest = __rest(_d, [typeof _e === "symbol" ? _e : _e + ""]);
return __assign(__assign({}, state), (_a = {}, _a[action.type] = rest, _a));
}
return __assign(__assign({}, state), (_b = {}, _b[action.type] = __assign(__assign({}, state[action.type]), (_c = {}, _c[action.id] = {
status: action.status,
data: action.payload,
}, _c)), _b));
};
return reducer;
};
export default createAsyncReducer;
//# sourceMappingURL=createAsyncReducer.js.map