-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.tsx
258 lines (196 loc) · 8.78 KB
/
router.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* tslint:disable:no-string-literal */
import * as React from 'react';
import {Route, Switch, Redirect} from 'react-router'
import Header from './containers/Header';
import Sidebar from './containers/SideBar';
import Login from './containers/Login';
import Landing from './containers/Landing';
import NoMatch from './components/NoMatch';
// import Org from './components/Org';
import './router.scss';
import Cookies from 'universal-cookie';
const cookies = new Cookies();
function cloneObject(obj) {
const clone = {};
for(const i in obj) {
if(obj[i] != null && typeof(obj[i]) === "object") {
clone[i] = cloneObject(obj[i]);
} else {
clone[i] = obj[i];
}
}
return clone;
}
const asyncComponent = (importComponent) => {
return class extends React.Component<any, any> {
public state : { component : any , componentData : any}= {
component : null,
componentData : null
};
public componentDidMount() {
// console.log(importComponent);
console.log(this.props.passedProps.location.pathname);
const key = this.props.passedProps.location.pathname;
const runtimePathUrl = window['appPaths'][key]['src'];
console.log('script location for ' + key + ' is : ' , runtimePathUrl);
const scriptSource = document.createElement('script');
scriptSource.src = runtimePathUrl;
scriptSource.onload = () => {
try{
window['appPaths'][key]['isLoaded'] = true;
// deep copy object from tempLazyLoaded and flush tempLazyLoaded
console.log('tempLazyLoaded object from script ' , window['tempLazyLoaded']);
window['appPaths'][key]['exposedModule'] = cloneObject(window['tempLazyLoaded']);
console.log(window['appPaths'][key]);
const cmpDefinition = window['appPaths'][key]['exposedModule'][window['appPaths'][key]['moduleComponent']];
this.setState({component: cmpDefinition , componentData : this.props.passedProps});
// below is old one
// Very IMP to decide which cmp to render after script is loaded if script expose only single module after rollup
// than use below thing as it is
// this.setState({component: window[this.props.passedProps.location.pathname.split("/")[1]]});
// otherwise use relative cmp from exposedModule in window
}catch (e) {
console.log(e);
window['appPaths'][key]['isLoaded'] = false;
this.setState({component: "error"});
}
};
if(document.head){
document.head.appendChild(scriptSource);
}
// importComponent()
// .then(cmp => {
// this.setState({component: cmp.default});
// });
}
public render() {
if(this.state.component){
if(this.state.component !== 'error'){
const C = this.state.component;
console.log("componentData ", this.props);
return <C {...this.props}/>
}else{
return <div> error in loading component</div>
}
}else{
return <div> loading component</div>
}
}
}
};
window['asyncComponent'] = asyncComponent;
const AsyncPrivateRoute = ({component: Component, ...rest}) => (
<Route
{...rest}
render={props => {
console.log(props);
// props.location.pathname indexOf can load main module and main module can return relative cmp
const key = props.location.pathname;
console.log('current runtime paths are ' , window['appPaths']);
console.log('value for this path : ' + key + ' is : ' , window['appPaths'][key]);
const runtimePath = window['appPaths'][key];
if (runtimePath) {
if(runtimePath['isCookieSecure']){
if(cookies.get('isLoggedin')){
// console.log('continue with flow');
// caching control
if(runtimePath['isLoaded'] ){
// decide which cmp to return from exposedModule
// console.log(window['appPaths'][key]);
const tempCmp = window['appPaths'][key]['exposedModule'][window['appPaths'][key]['moduleComponent']];
// console.log(tempCmp);
const Cmp = React.createElement(tempCmp, props, null);
console.log('component definition form caching ' , Cmp);
return Cmp;
// return window['appPaths'][key]['exposedModule']['index'];
}else{
const AsyncCmp = asyncComponent(() => {
return import('./components/NoMatch');
});
return <AsyncCmp passedProps={props}/>
}
}else{
console.log('runtime path is defined and path(url) is CookieSecure but user has not logged-in. so redirecting to root path');
return <Redirect
to={{
pathname: "/",
state: {from: props.location}
}}
/>
}
}else{
// caching control
if(runtimePath['isLoaded'] ){
// decide which cmp to return from exposedModule
console.log(window['appPaths'][key]);
const tempCmp = window['appPaths'][key]['exposedModule'][window['appPaths'][key]['moduleComponent']];
// console.log(tempCmp);
const Cmp = React.createElement(tempCmp, props, null);
console.log('component definition form caching ' , Cmp);
return Cmp;
}else{
const AsyncCmp = asyncComponent(() => {
return import('./components/NoMatch');
});
return (<div>
<AsyncCmp passedProps={props}/>
</div>)
}
}
} else {
console.log('runtime path is not defined redirecting to root path');
return <Redirect
to={{
pathname: "/",
state: {from: props.location}
}}
/>
}
}
}
/>
);
const PrivateRoute = ({component: Component, ...rest}) => (
<Route
{...rest}
render={props => {
if (cookies.get('isLoggedin')) {
return <Component {...props} />
} else {
// handle redirect url after login in query string or state ( below state is used )
return <Redirect
to={{
pathname: "/login",
state: {from: props.location}
}}
/>
}
}
}
/>
);
const routes = (
<div className="contained-css">
<Header/>
<div className="container-fluid">
<div className="row">
<Sidebar/>
<Switch>
{<Route exact path="/" render={(props) =>
<Redirect
to={{
pathname: "/search"
}}
/>
}/>}
<Route path="/login" component={Login}/>
<PrivateRoute path="/search" component={Landing}/>
{/*<PrivateRoute exact path="/random" component={Org}/>*/}
<AsyncPrivateRoute component={NoMatch}/>
</Switch>
</div>
</div>
</div>
);
export default routes
/* tslint:disable:no-string-literal */