1 | import React, {ReactNode} from 'react';
|
2 |
|
3 | import {IComponent} from "../types/component";
|
4 | import Types, { IInfrastructure } from "../types";
|
5 |
|
6 | import { isMiddleware } from '../middleware/middleware-component';
|
7 | import {getChildrenArray, findComponentRecursively} from '../libs';
|
8 |
|
9 |
|
10 | export const ROUTE_INSTANCE_TYPE = "RouteComponent";
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export interface IRouteArgs {
|
17 |
|
18 | path: string,
|
19 |
|
20 | name: string,
|
21 |
|
22 | render?: any,
|
23 |
|
24 | component?: any,
|
25 |
|
26 | exact?: boolean,
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | export interface IRouteProps {
|
34 |
|
35 | |
36 |
|
37 |
|
38 | middlewares: Array<any>,
|
39 |
|
40 |
|
41 |
|
42 | isSecured?: boolean
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | export default (props: IRouteArgs | any) => {
|
52 |
|
53 |
|
54 |
|
55 | const componentProps: IInfrastructure & IComponent = {
|
56 | infrastructureType: Types.INFRASTRUCTURE_TYPE_COMPONENT,
|
57 | instanceType: ROUTE_INSTANCE_TYPE,
|
58 | instanceId: undefined,
|
59 |
|
60 | insulatesChildComponent: (child) => {
|
61 |
|
62 | return isMiddleware(child)
|
63 | }
|
64 | };
|
65 |
|
66 | const routeProps: IRouteProps = {
|
67 | middlewares:findComponentRecursively(props.children, isMiddleware),
|
68 | }
|
69 |
|
70 | return Object.assign({}, props, componentProps, routeProps, {exact: props.exact === undefined ? true : props.exact});
|
71 |
|
72 |
|
73 | };
|
74 |
|
75 | export const isRoute = (component) => {
|
76 |
|
77 | return component !== undefined &&
|
78 | component.instanceType === ROUTE_INSTANCE_TYPE;
|
79 | }; |
\ | No newline at end of file |