use-async-reducer
Version:
Provides a reducer to simplify handling of async actions
168 lines (141 loc) • 4.38 kB
JavaScript
import { useReducer, useCallback } from 'react';
import { createStandardAction, getType } from 'typesafe-actions';
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === 'function') {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function (key) {
_defineProperty(target, key, source[key]);
});
}
return target;
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
var ACTION_TYPES;
(function (ACTION_TYPES) {
ACTION_TYPES["INITIALIZE"] = "INITIALIZE";
ACTION_TYPES["REQUEST"] = "REQUEST";
ACTION_TYPES["SUCCESS"] = "SUCCESS";
ACTION_TYPES["FAILURE"] = "FAILURE";
ACTION_TYPES["COMPLETE"] = "COMPLETE";
})(ACTION_TYPES || (ACTION_TYPES = {}));
const initialize = createStandardAction(ACTION_TYPES.INITIALIZE)();
const request = createStandardAction(ACTION_TYPES.REQUEST)();
const success = createStandardAction(ACTION_TYPES.SUCCESS)();
const failure = createStandardAction(ACTION_TYPES.FAILURE)();
const complete = createStandardAction(ACTION_TYPES.COMPLETE)();
const reducer = (state, action) => {
switch (action.type) {
case getType(initialize):
return {
data: null,
loading: true,
error: null
};
case getType(request):
return _objectSpread({}, state, {
error: null,
loading: true
});
case getType(success):
return {
data: action.payload,
error: null,
loading: false
};
case getType(failure):
return {
data: null,
error: action.payload,
loading: false
};
case getType(complete):
return _objectSpread({}, state, {
loading: false
});
/* istanbul ignore next */
default:
return state;
}
};
/**
* Provides a reducer and actions to manage states of async operations
* @param initialValue Will set the `data` attribute of the first returned value
* @returns An array of two values
* 0. the state of the async operation with the shape
* 1. object of bound action methods
*
* @see [https://github.com/azmenak/use-async-reducer/blob/master/README.md](https://github.com/azmenak/use-async-reducer/blob/master/README.md)
*/
function useAsyncReducer(initialValue) {
const _useReducer = useReducer(reducer, {
data: initialValue || null,
loading: false,
error: null
}),
_useReducer2 = _slicedToArray(_useReducer, 2),
state = _useReducer2[0],
dispatch = _useReducer2[1];
const initialize$1 = useCallback(() => dispatch(initialize()), []);
const request$1 = useCallback(() => dispatch(request()), []);
const success$1 = useCallback(payload => dispatch(success(payload)), []);
const failure$1 = useCallback(error => dispatch(failure(error)), []);
const complete$1 = useCallback(() => dispatch(complete()), []);
return [state, {
initialize: initialize$1,
request: request$1,
success: success$1,
failure: failure$1,
complete: complete$1
}];
}
export default useAsyncReducer;
export { ACTION_TYPES, complete, failure, initialize, reducer, request, success };