UNPKG

react-redux-isomorphic-filterlist

Version:

Integration of react-redux-isomorphic and @vtaits/react-filterlist

224 lines (184 loc) 8.29 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _regeneratorRuntime from "@babel/runtime/regenerator"; import { useMemo as useMemoBase, useRef as useRefBase } from 'react'; import { useIsomorphic as useIsomorphicBase, useLoadParams as useLoadParamsBase, LoadContextError as IsomorphicError } from 'react-redux-isomorphic'; import { collectListInitialState as collectListInitialStateBase, collectOptions as collectOptionsBase, LoadListError as FilterlistError } from '@vtaits/filterlist'; import { useFilterlist as useFilterlistBase } from '@vtaits/react-filterlist'; import { LoadListError } from './errors'; export var DELAYED_INIT_POSTFIX = '/DELAYED_INIT'; export var useFilterlistIsomorphicPure = function useFilterlistIsomorphicPure(isomorphicIdParam, listParams, useIsomorphic, useLoadParams, collectListInitialState, collectOptions, useFilterlist, useRef, useMemo) { var isomorphicResponseRef = useRef(null); var canInit = typeof listParams.canInit === 'boolean' ? listParams.canInit : true; var isomorphicId = canInit ? isomorphicIdParam : "".concat(isomorphicIdParam).concat(DELAYED_INIT_POSTFIX); var listOptions = useMemo(function () { return collectOptions(listParams); }, [isomorphicId]); var listInitialState = useMemo(function () { return collectListInitialState(listParams); }, [isomorphicId]); var isomorphicParams = useLoadParams(); var isomorphicResponse = useIsomorphic(isomorphicId, /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var loadItems, _listParams$parseFilt, parseFiltersAndSort, _listParams$filtersAn, filtersAndSortData, parsedState, response, cathcedError, hasError; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (!(!canInit || !listOptions.autoload)) { _context.next = 2; break; } return _context.abrupt("return", null); case 2: loadItems = listParams.loadItems, _listParams$parseFilt = listParams.parseFiltersAndSort, parseFiltersAndSort = _listParams$parseFilt === void 0 ? null : _listParams$parseFilt, _listParams$filtersAn = listParams.filtersAndSortData, filtersAndSortData = _listParams$filtersAn === void 0 ? null : _listParams$filtersAn; if (!parseFiltersAndSort) { _context.next = 9; break; } _context.next = 6; return parseFiltersAndSort(filtersAndSortData); case 6: parsedState = _context.sent; _context.next = 10; break; case 9: parsedState = {}; case 10: _context.prev = 10; _context.next = 13; return loadItems(isomorphicParams, _objectSpread(_objectSpread({}, listInitialState), parsedState)); case 13: response = _context.sent; hasError = false; _context.next = 21; break; case 17: _context.prev = 17; _context.t0 = _context["catch"](10); cathcedError = _context.t0; hasError = true; case 21: if (!hasError) { _context.next = 25; break; } if (!(cathcedError instanceof LoadListError)) { _context.next = 24; break; } throw new IsomorphicError({ error: cathcedError.error, additional: cathcedError.additional }); case 24: throw cathcedError; case 25: return _context.abrupt("return", response); case 26: case "end": return _context.stop(); } } }, _callee, null, [[10, 17]]); }))); isomorphicResponseRef.current = isomorphicResponse; var filterlistParams = useMemo(function () { return _objectSpread(_objectSpread({}, listParams), {}, { loadItems: function () { var _loadItems = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(currentListState) { var _isomorphicResponseRe, isReady, isomorphicContext, error, response, cathcedError, hasError; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _isomorphicResponseRe = isomorphicResponseRef.current, isReady = _isomorphicResponseRe.isReady, isomorphicContext = _isomorphicResponseRe.context, error = _isomorphicResponseRe.error; if (isReady) { _context2.next = 3; break; } return _context2.abrupt("return", { items: [], additional: listInitialState.additional }); case 3: if (!(listOptions.autoload && currentListState.isFirstLoad)) { _context2.next = 7; break; } if (!error) { _context2.next = 6; break; } throw new FilterlistError(error); case 6: return _context2.abrupt("return", isomorphicContext); case 7: _context2.prev = 7; _context2.next = 10; return listParams.loadItems(isomorphicParams, currentListState); case 10: response = _context2.sent; hasError = false; _context2.next = 18; break; case 14: _context2.prev = 14; _context2.t0 = _context2["catch"](7); cathcedError = _context2.t0; hasError = true; case 18: if (!hasError) { _context2.next = 22; break; } if (!(cathcedError instanceof LoadListError)) { _context2.next = 21; break; } throw new FilterlistError({ error: cathcedError.error, additional: cathcedError.additional }); case 21: throw cathcedError; case 22: return _context2.abrupt("return", response); case 23: case "end": return _context2.stop(); } } }, _callee2, null, [[7, 14]]); })); function loadItems(_x) { return _loadItems.apply(this, arguments); } return loadItems; }() }); }, [isomorphicId, listParams.filtersAndSortData]); var isReady = isomorphicResponse.isReady, isomorphicContext = isomorphicResponse.context, isomorphicError = isomorphicResponse.error; var _useFilterlist = useFilterlist(filterlistParams, [isomorphicId, isReady]), _useFilterlist2 = _slicedToArray(_useFilterlist, 2), listState = _useFilterlist2[0], filterlist = _useFilterlist2[1]; if (isReady) { if (!listState) { return [null, filterlist]; } if (listOptions.autoload && listState.isFirstLoad) { if (isomorphicError) { return [_objectSpread(_objectSpread({}, listInitialState), isomorphicError), filterlist]; } return [_objectSpread(_objectSpread({}, listInitialState), isomorphicContext), filterlist]; } return [listState, filterlist]; } return [listInitialState, filterlist]; }; export var useFilterlistIsomorphic = function useFilterlistIsomorphic(isomorphicId, listParams) { return useFilterlistIsomorphicPure(isomorphicId, listParams, useIsomorphicBase, useLoadParamsBase, collectListInitialStateBase, collectOptionsBase, useFilterlistBase, useRefBase, useMemoBase); };