UNPKG

use-async-reducer

Version:

Provides a reducer to simplify handling of async actions

175 lines (147 loc) 4.75 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var react = require('react'); var typesafeActions = require('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"); } (function (ACTION_TYPES) { ACTION_TYPES["INITIALIZE"] = "INITIALIZE"; ACTION_TYPES["REQUEST"] = "REQUEST"; ACTION_TYPES["SUCCESS"] = "SUCCESS"; ACTION_TYPES["FAILURE"] = "FAILURE"; ACTION_TYPES["COMPLETE"] = "COMPLETE"; })(exports.ACTION_TYPES || (exports.ACTION_TYPES = {})); const initialize = typesafeActions.createStandardAction(exports.ACTION_TYPES.INITIALIZE)(); const request = typesafeActions.createStandardAction(exports.ACTION_TYPES.REQUEST)(); const success = typesafeActions.createStandardAction(exports.ACTION_TYPES.SUCCESS)(); const failure = typesafeActions.createStandardAction(exports.ACTION_TYPES.FAILURE)(); const complete = typesafeActions.createStandardAction(exports.ACTION_TYPES.COMPLETE)(); const reducer = (state, action) => { switch (action.type) { case typesafeActions.getType(initialize): return { data: null, loading: true, error: null }; case typesafeActions.getType(request): return _objectSpread({}, state, { error: null, loading: true }); case typesafeActions.getType(success): return { data: action.payload, error: null, loading: false }; case typesafeActions.getType(failure): return { data: null, error: action.payload, loading: false }; case typesafeActions.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 = react.useReducer(reducer, { data: initialValue || null, loading: false, error: null }), _useReducer2 = _slicedToArray(_useReducer, 2), state = _useReducer2[0], dispatch = _useReducer2[1]; const initialize$1 = react.useCallback(() => dispatch(initialize()), []); const request$1 = react.useCallback(() => dispatch(request()), []); const success$1 = react.useCallback(payload => dispatch(success(payload)), []); const failure$1 = react.useCallback(error => dispatch(failure(error)), []); const complete$1 = react.useCallback(() => dispatch(complete()), []); return [state, { initialize: initialize$1, request: request$1, success: success$1, failure: failure$1, complete: complete$1 }]; } exports.complete = complete; exports.default = useAsyncReducer; exports.failure = failure; exports.initialize = initialize; exports.reducer = reducer; exports.request = request; exports.success = success;