@mic-rexjs/usecases-react
Version:
React-based solution for use usecases of Clean Architecture
41 lines • 1.97 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
const _excluded = ["watch", "onChange", "options"];
import { useConstantReducers } from '../useConstantReducers';
import { useContextualItem } from '../useContextualItem';
import { createEntityReducers } from '@mic-rexjs/usecases';
import { useLatest } from 'ahooks';
import { useContext } from 'react';
import { Statuses } from '../../enums/Statuses';
import { methodUseCase } from '../../usecases/methodUseCase';
export const useReducers = function (usecase, context, store, statuses, options) {
let deps = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : [];
const optionsRef = useLatest(options);
const contextValue = useContext(context);
const _ref = contextValue || {},
_ref$reducers = _ref.reducers,
contextReducers = _ref$reducers === void 0 ? null : _ref$reducers;
const _useConstantReducers = useConstantReducers(methodUseCase),
captureCalls = _useConstantReducers.captureCalls;
const reducers = useContextualItem(contextReducers, statuses, () => {
const opts = captureCalls(options, (key, callArgs) => {
var _optionsRef$current$k, _optionsRef$current;
return (_optionsRef$current$k = (_optionsRef$current = optionsRef.current)[key]) === null || _optionsRef$current$k === void 0 ? void 0 : _optionsRef$current$k.call(_optionsRef$current, ...callArgs);
});
if ((statuses & Statuses.EntityEnabled) !== Statuses.EntityEnabled) {
return usecase(opts);
}
const hookOptions = opts;
const watch = hookOptions.watch,
onChange = hookOptions.onChange,
usecaseOptions = hookOptions.options,
restUseCaseOptions = _objectWithoutProperties(hookOptions, _excluded);
return createEntityReducers(store, usecase, {
...restUseCaseOptions,
...usecaseOptions,
onGenerate(newEntity, result) {
return result;
}
});
}, deps);
return reducers;
};