UNPKG

react-redux-isomorphic

Version:

Set of utilities for creating isomorphic applications using react-redux

134 lines (108 loc) 5.84 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.componentReducer = exports.componentInitialState = exports.initialState = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _objectSpread7 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2")); var _invariant = _interopRequireDefault(require("invariant")); var _actionsTypes = require("./actionsTypes"); var initialState = { pendingComponents: [], componentsParams: {} }; exports.initialState = initialState; var componentInitialState = { isReady: false, isLoading: false, isReloading: false, context: null, error: null }; exports.componentInitialState = componentInitialState; var componentReducer = function componentReducer() { var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : componentInitialState; var action = arguments.length > 1 ? arguments[1] : undefined; switch (action.type) { case _actionsTypes.LOAD_CONTEXT: return (0, _objectSpread7["default"])((0, _objectSpread7["default"])({}, state), {}, { isLoading: true }); case _actionsTypes.RELOAD_CONTEXT: return (0, _objectSpread7["default"])((0, _objectSpread7["default"])({}, state), {}, { isLoading: true, isReloading: true }); case _actionsTypes.LOAD_CONTEXT_SUCCESS: return (0, _objectSpread7["default"])((0, _objectSpread7["default"])({}, state), {}, { isReady: true, isLoading: false, isReloading: false, context: action.payload.context }); case _actionsTypes.LOAD_CONTEXT_ERROR: return (0, _objectSpread7["default"])((0, _objectSpread7["default"])({}, state), {}, { isReady: true, isLoading: false, isReloading: false, error: action.payload.error }); case _actionsTypes.DESTROY: return componentInitialState; default: return state; } }; exports.componentReducer = componentReducer; var reactReduxIsomorphic = function reactReduxIsomorphic() { var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState; var action = arguments.length > 1 ? arguments[1] : undefined; switch (action.type) { case _actionsTypes.LOAD_CONTEXT: (0, _invariant["default"])(!state.pendingComponents.includes(action.payload.isomorphicId), "Duplication of components with isomorphicId \"".concat(action.payload.isomorphicId, "\"")); return (0, _objectSpread7["default"])((0, _objectSpread7["default"])({}, state), {}, { pendingComponents: state.pendingComponents.concat([action.payload.isomorphicId]), componentsParams: (0, _objectSpread7["default"])((0, _objectSpread7["default"])({}, state.componentsParams), {}, (0, _defineProperty2["default"])({}, action.payload.isomorphicId, componentReducer(state.componentsParams[action.payload.isomorphicId], action))) }); case _actionsTypes.RELOAD_CONTEXT: if (!state.componentsParams[action.payload.isomorphicId]) { return state; } return (0, _objectSpread7["default"])((0, _objectSpread7["default"])({}, state), {}, { componentsParams: (0, _objectSpread7["default"])((0, _objectSpread7["default"])({}, state.componentsParams), {}, (0, _defineProperty2["default"])({}, action.payload.isomorphicId, componentReducer(state.componentsParams[action.payload.isomorphicId], action))) }); case _actionsTypes.LOAD_CONTEXT_SUCCESS: if (!state.componentsParams[action.payload.isomorphicId]) { return state; } return (0, _objectSpread7["default"])((0, _objectSpread7["default"])({}, state), {}, { pendingComponents: state.pendingComponents.filter(function (pendingIsomorphicId) { return pendingIsomorphicId !== action.payload.isomorphicId; }), componentsParams: (0, _objectSpread7["default"])((0, _objectSpread7["default"])({}, state.componentsParams), {}, (0, _defineProperty2["default"])({}, action.payload.isomorphicId, componentReducer(state.componentsParams[action.payload.isomorphicId], action))) }); case _actionsTypes.LOAD_CONTEXT_ERROR: if (!state.componentsParams[action.payload.isomorphicId]) { return state; } return (0, _objectSpread7["default"])((0, _objectSpread7["default"])({}, state), {}, { pendingComponents: state.pendingComponents.filter(function (pendingIsomorphicId) { return pendingIsomorphicId !== action.payload.isomorphicId; }), componentsParams: (0, _objectSpread7["default"])((0, _objectSpread7["default"])({}, state.componentsParams), {}, (0, _defineProperty2["default"])({}, action.payload.isomorphicId, componentReducer(state.componentsParams[action.payload.isomorphicId], action))) }); case _actionsTypes.DESTROY: (0, _invariant["default"])(state.componentsParams[action.payload.isomorphicId], "Components with isomorphicId \"".concat(action.payload.isomorphicId, "\" is not registered")); return (0, _objectSpread7["default"])((0, _objectSpread7["default"])({}, state), {}, { pendingComponents: state.pendingComponents.filter(function (pendingIsomorphicId) { return pendingIsomorphicId !== action.payload.isomorphicId; }), componentsParams: (0, _objectSpread7["default"])((0, _objectSpread7["default"])({}, state.componentsParams), {}, (0, _defineProperty2["default"])({}, action.payload.isomorphicId, componentReducer(state.componentsParams[action.payload.isomorphicId], action))) }); default: return state; } }; var _default = reactReduxIsomorphic; exports["default"] = _default;