Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @todo console.group  these
 *
 * @throws LogicException
 *
 *  1. renders routes
 *    @example /myaccount
 *    - this calls
 *  2. renders subroutes
 *    @example /myaccount/orders
 *    a. checks if the route params HAS subroutes
 *    b. checks the route.component.getComponentForRoute
 *    c. else, route.component.getComponentForSubRoute
 *
 *
 * @module Router
 *
 * @variation minimal can be
 *  1. create a flat array
 *  2. sort by length (longest, shortest)
 *
 * @variation
 *  1. use context
 *  2. if context.subroutes
 *      - render the first to match
 *      - then it would keep traversing
 *
 */
const react_1 = __importDefault(require("react"));
const router_1 = require("@skava/router");
const exotic_1 = require("exotic");
const types_1 = require("@skava/modules/___dist/view-container/types");
const react_router_config_1 = require("@skava/router/dist/react-router-config");
const identifier_1 = require("@skava/modules/___dist/identifier");
const routes_1 = __importDefault(require("routes"));
const observableRouting_1 = require("src/bootstrapper/setup/observableRouting");
const ErrorBoundary_1 = __importDefault(require("../ErrorBoundary"));
// @@demo
// wasn't happening...
observableRouting_1.setup();
/**
 * used to DI the current route
 */
const scoped = {
    params: undefined,
    // doubly linked list would be great for page animations here
    prevRoute: exotic_1.EMPTY_OBJ,
    prevView: undefined,
    prevLocation: undefined,
};
function fromPropsToPrevLocation(props) {
    // props.location.key
    const url = props.match ? props.match.url : '';
    const params = props.match ? JSON.stringify(props.match.params) : '';
    const hashed = url +
        props.location.pathname +
        props.location.hash +
        props.location.search +
        params;
    if (typeof window === 'object') {
        return hashed + window.location.href;
    }
    else {
        return hashed;
    }
}
/**
 * @todo really need to optimize `entries`
 * @todo @fixme this is temporary
 * @description change 1router .entries
 *              to use the scoped params for this route
 *              (.matched is a getter)
 */
function tapOneRouter() {
    // only tap 1x
    if (router_1.oneRouter.entries.isTapped) {
        return;
    }
    // oneRouter.notify = action
    function entriesTapped() {
        // console.log('[routing] calling tapped entries')
        // @note - we could use [v] before overriding
        // but we can use `this.toObj` which is why that fn was method that way
        // (protected name, entries being aliased)
        // const entries = oneRouter.entries
        const obj = router_1.oneRouter.toObj();
        // update when scoped params updates
        Object.assign(obj, scoped.params);
        return obj;
    }
    // make sure we only tap once
    entriesTapped.isTapped = true;
    // set on the oneRouter
    router_1.oneRouter.entries = entriesTapped;
}
// init
tapOneRouter();
function debugRouting(props) {
    console.log('[ROUTING] - mounting OneRouterContainer');
    // console.log(oneRouter.entries())
    // console.log('ROUTING_PROPS_FROM_ROUTER')
    // console.log(props)
}
// component
// @NOTE !!! USING @withRouter HERE PUTS A ROUTE INSIDE THE ROUTE!!!!!!
class OneRouterContainerTemporary extends react_1.default.PureComponent {
    componentWillMount() {
        const params = this.props.match.params;
        scoped.params = params;
        debugRouting(this.props);
    }
    componentDidMount() {
        // only doing this on the browser
        router_1.oneRouter.router = this.context.router || this.props.router;
    }
    render() {
        return '';
    }
}
OneRouterContainerTemporary.contextTypes = {
    router: types_1.object,
};
/**
 * @description decorate a route, DI the params out into 1router
 */
// @todo @split
// eslint-disable-next-line max-statements
function toRouteItem(route) {
    const { component, path, exact } = route;
    const Component = component;
    // named route - this wraps the route
    // @note this is basically @withRouter & setting oneRouter props from the props
    // @todo @lint @split @@demo
    // eslint-disable-next-line
    function decoratedRouteComponent(props) {
        // !!! probably most of this is not needed since we are not deduping
        // ^ however, we can keep as comment for route transitions
        // ^ thanks to removing @withRouter
        // always update 1router
        scoped.oneRouterView = (react_1.default.createElement(OneRouterContainerTemporary, Object.assign({}, props, { decoratedRouteItem: route })));
        const prevLocation = fromPropsToPrevLocation(props);
        // I see many duplicate route updates, no good - use previously rendered
        if (route.path === scoped.prevRoute.path &&
            scoped.prevLocation === prevLocation) {
            // todo
            console.log('[ROUTING] deduped route ', route);
            return scoped.prevView;
        }
        // .render
        console.log('[ROUTING] rendering route: ', route);
        router_1.oneRouter.observable.urlList.push(prevLocation);
        router_1.oneRouter.observable.url = prevLocation;
        scoped.prevLocation = prevLocation;
        scoped.prevRoute = route;
        scoped.prevView = (react_1.default.createElement(react_1.default.Fragment, { key: path },
            scoped.oneRouterView,
            react_1.default.createElement(Component, Object.assign({}, props))));
        return scoped.prevView;
    }
    // if (process.env.DEBUG_REACT_DISPLAY_NAME) {
    const componentName = identifier_1.toComponentName(Component);
    const wrapName = `decoratedRouteComponent`;
    if (componentName.includes(wrapName)) {
        console.warn('[ROUTING] component duplicated in mapping - fix: ', wrapName);
        return route;
    }
    const displayName = `${wrapName}(${componentName})`;
    decoratedRouteComponent.displayName = displayName;
    route.component = decoratedRouteComponent;
    return route;
}
/**
 * @description take all routes, add 1router extractor for DI
 */
function toRoutesWithOneRouterUpdater(routes) {
    return routes.map(toRouteItem);
}
/**
 * @description OneRouterRouter
 */
class Routing extends react_1.default.PureComponent {
    constructor(props, ...args) {
        super(props, ...args);
        // no need to make it update, only needs to happen 1x
        this.routesWithOneRouterUpdater = toRoutesWithOneRouterUpdater(this.props.routes);
    }
    componentDidMount() {
        if (typeof window === 'object') {
            window.scrollTo({
                top: 0,
                behavior: 'smooth',
            });
        }
    }
    render() {
        const { routesWithOneRouterUpdater } = this;
        return (react_1.default.createElement(ErrorBoundary_1.default, null, react_router_config_1.renderRoutes(routesWithOneRouterUpdater)));
    }
}
Routing.propTypes = {
    routes: types_1.array.isRequired,
    location: types_1.object.isRequired,
};
Routing.defaultProps = {
    routes: routes_1.default,
    location: exotic_1.EMPTY_OBJ,
};
exports.Routing = Routing;
exports.default = Routing;
//# sourceMappingURL=Routing.js.map