UNPKG

react-application-core

Version:

A react-based application core for the business applications.

236 lines 10.3 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UniversalApplicationContainer = void 0; var ts_smart_logger_1 = require("ts-smart-logger"); var util_1 = require("../../util"); var definition_1 = require("../../definition"); var application_action_builder_1 = require("./application-action.builder"); var generic_container_1 = require("../base/generic.container"); var UniversalApplicationContainer = /** @class */ (function (_super) { __extends(UniversalApplicationContainer, _super); /** * @stable [17.11.2019] * @param {TProps} props */ function UniversalApplicationContainer(props) { var _this = _super.call(this, props) || this; _this.extraRoutes = new Map(); _this.syncAppState = _this.syncAppState.bind(_this); _this.onBeforeLogout = _this.onBeforeLogout.bind(_this); _this.registerLogoutRoute(); UniversalApplicationContainer.logger.debug('[$UniversalApplicationContainer][constructor] The app has been instantiated. The initial props are:', props); return _this; } /** * @stable [17.11.2019] */ UniversalApplicationContainer.prototype.componentDidMount = function () { this.storeProxy.dispatchActionByType(application_action_builder_1.ApplicationActionBuilder.buildMountActionType()); if (this.storageSettings.appStateSyncEnabled) { this.storeProxy.dispatchActionByType(definition_1.$RAC_STORAGE_REGISTER_SYNC_APP_STATE_TASK_ACTION_TYPE); } }; /** * @stable [24.09.2019] */ UniversalApplicationContainer.prototype.componentWillUnmount = function () { if (this.storageSettings.appStateSyncEnabled) { this.storeProxy.dispatchActionByType(definition_1.$RAC_STORAGE_UNREGISTER_SYNC_APP_STATE_TASK_ACTION_TYPE); this.syncAppState(); } }; /** * @stable [18.11.2019] */ UniversalApplicationContainer.prototype.syncAppState = function () { this.storeProxy.dispatchActionByType(definition_1.$RAC_STORAGE_SYNC_APP_STATE_ACTION_TYPE); }; /** * @stable [28.11.2019] * @param {RoutePredicateT} routePredicate * @returns {React.ReactNode} */ UniversalApplicationContainer.prototype.getRoutes = function (routePredicate) { if (routePredicate === void 0) { routePredicate = function () { return true; }; } return this.isMessageVisible ? this.getMessageElement() : __spreadArrays(this.buildRoutes(this.dynamicRoutes, routePredicate), this.buildRoutes(this.extraRoutes, routePredicate)); }; /** * @stable [17.11.2019] * @param {string} path * @returns {IGenericContainerCtor} */ UniversalApplicationContainer.prototype.lookupDynamicContainerByRoutePath = function (path) { if (!path) { UniversalApplicationContainer.logger.warn('[$UniversalApplicationContainer][lookupDynamicContainerByRoutePath] The path is empty'); return null; } var result; this.dynamicRoutes.forEach(function (config, ctor) { util_1.ifNotNilThanValue(config.routeConfiguration, function (routeCfg) { if ([util_1.calc(routeCfg.path), routeCfg.key].includes(path)) { result = ctor; } }); }); if (!result) { UniversalApplicationContainer.logger.warn("[$UniversalApplicationContainer][lookupDynamicContainerByRoutePath] A container is not found by path: " + path); } return result; }; /** * @stable [17.11.2019] * @param {IGenericContainerCtor} container * @param {IConnectorEntity} config */ UniversalApplicationContainer.prototype.registerExtraRoute = function (container, config) { this.extraRoutes.set(container, config); }; UniversalApplicationContainer.prototype.registerLogoutRoute = function () { var loginContainer = this.lookupDynamicContainerByRoutePath(this.settings.routes.signIn); if (!loginContainer) { UniversalApplicationContainer.logger.debug('[$UniversalApplicationContainer][registerLogoutRoute] The login route is not registered.'); } else { this.registerExtraRoute(loginContainer, { routeConfiguration: { type: definition_1.ContainerVisibilityTypesEnum.PUBLIC, path: this.settings.routes.logout, beforeEnter: this.onBeforeLogout, onEnter: this.onBeforeLogout, }, }); UniversalApplicationContainer.logger.debug('[$UniversalApplicationContainer][registerLogoutRoute] Logout router has been registered.'); } }; /** * @stable [17.11.2019] */ UniversalApplicationContainer.prototype.onBeforeLogout = function () { UniversalApplicationContainer.logger.debug('[$UniversalApplicationContainer][onBeforeLogout] Send a logout action.'); this.storeProxy.dispatchActionByType(application_action_builder_1.ApplicationActionBuilder.buildLogoutActionType()); }; /** * @stable [17.11.2019] * @param {IRouteEntity} routeCfg * @returns {string} */ UniversalApplicationContainer.prototype.toRouteId = function (routeCfg) { return util_1.calc(routeCfg.path) || routeCfg.key; }; /** * @stable [28.11.2019] * @returns {React.ReactNode} */ UniversalApplicationContainer.prototype.getMessageElement = function () { var error = this.originalProps.error; var messages = this.settings.messages; return (this.inProgress ? this.uiFactory.makeMessage(this.prepareMessage({ message: messages.PLEASE_WAIT })) : (this.doesErrorExist ? (this.uiFactory.makeReactError(util_1.TypeUtils.isBoolean(error) ? new Error(messages.UNKNOWN_ERROR) : new Error(String(error)))) : (this.uiFactory.makeMessage(this.prepareMessage({ message: messages.APPLICATION_IS_INITIALIZING }))))); }; /** * @stable [28.11.2019] * @param {IUniversalUiMessageConfigEntity} message * @returns {IUniversalUiMessageConfigEntity} */ UniversalApplicationContainer.prototype.prepareMessage = function (message) { return message; }; /** * @stable [17.11.2019] * @param {Map<IGenericContainerCtor, IConnectorEntity>} map * @param {RoutePredicateT} routePredicate * @returns {JSX.Element[]} */ UniversalApplicationContainer.prototype.buildRoutes = function (map, routePredicate) { var _this = this; var routesToDebug = []; var result = []; map.forEach(function (connectorEntity, containerCtor) { var routeCfg = connectorEntity.routeConfiguration; if (routePredicate.call(null, routeCfg)) { routesToDebug.push(_this.toRouteId(routeCfg)); result.push(_this.buildRoute(containerCtor, connectorEntity)); } }); UniversalApplicationContainer.logger.debug(function () { return "[$UniversalApplicationContainer][buildRoutes] The routes have been built. Routes: " + routesToDebug.join('\n'); }); return result; }; Object.defineProperty(UniversalApplicationContainer.prototype, "storageSettings", { /** * @stable [17.11.2019] * @returns {IStorageSettingsEntity} */ get: function () { return this.settings.storage || {}; }, enumerable: false, configurable: true }); Object.defineProperty(UniversalApplicationContainer.prototype, "isMessageVisible", { /** * @stable [28.11.2019] * @returns {boolean} */ get: function () { return util_1.isApplicationMessageVisible(this.props); }, enumerable: false, configurable: true }); Object.defineProperty(UniversalApplicationContainer.prototype, "inProgress", { /** * @stable [28.11.2019] * @returns {boolean} */ get: function () { return util_1.isApplicationInProgress(this.props); }, enumerable: false, configurable: true }); Object.defineProperty(UniversalApplicationContainer.prototype, "doesErrorExist", { /** * @stable [28.11.2019] * @returns {boolean} */ get: function () { return util_1.doesApplicationErrorExist(this.props); }, enumerable: false, configurable: true }); UniversalApplicationContainer.defaultProps = { sectionName: definition_1.APPLICATION_SECTION, }; UniversalApplicationContainer.logger = ts_smart_logger_1.LoggerFactory.makeLogger('UniversalApplicationContainer'); return UniversalApplicationContainer; }(generic_container_1.GenericContainer)); exports.UniversalApplicationContainer = UniversalApplicationContainer; //# sourceMappingURL=universal-application.container.js.map