This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
APIController.react.js
154 lines (135 loc) · 4.26 KB
/
APIController.react.js
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
import {connect} from 'react-redux'
import {any, contains, equals, isEmpty, isNil} from 'ramda'
import React, {Component, PropTypes} from 'react';
import renderTree from './renderTree';
import {
computeGraphs,
computePaths,
hydrateInitialOutputs,
setLayout
} from './actions/index';
import {getDependencies, getLayout} from './actions/api';
import {APP_STATES} from './reducers/constants';
import AccessDenied from './AccessDenied.react';
/**
* Fire off API calls for initialization
*/
class UnconnectedContainer extends Component {
constructor(props) {
super(props);
this.initialization = this.initialization.bind(this);
}
componentDidMount() {
this.initialization(this.props);
}
componentWillReceiveProps(props) {
this.initialization(props);
}
initialization(props) {
const {
appLifecycle,
dependenciesRequest,
dispatch,
graphs,
layout,
layoutRequest,
paths
} = props;
if (isEmpty(layoutRequest)) {
dispatch(getLayout());
} else if (layoutRequest.status === 200) {
if (isEmpty(layout)) {
dispatch(setLayout(layoutRequest.content));
} else if (isNil(paths)) {
dispatch(computePaths({subTree: layout, startingPath: []}));
}
}
if (isEmpty(dependenciesRequest)) {
dispatch(getDependencies());
} else if (dependenciesRequest.status === 200 && isEmpty(graphs)) {
dispatch(computeGraphs(dependenciesRequest.content));
}
if (
// dependenciesRequest and its computed stores
dependenciesRequest.status === 200 &&
!isEmpty(graphs) &&
// LayoutRequest and its computed stores
layoutRequest.status === 200 &&
!isEmpty(layout) &&
!isNil(paths) &&
// Hasn't already hydrated
appLifecycle === APP_STATES('STARTED')
) {
dispatch(hydrateInitialOutputs());
}
}
render () {
const {
appLifecycle,
config,
dependenciesRequest,
lastUpdateComponentRequest,
layoutRequest,
layout
} = this.props;
// Auth protected routes
if (any(equals(true),
[dependenciesRequest, lastUpdateComponentRequest,
layoutRequest].map(
request => (request.status && request.status === 403))
)) {
return (<AccessDenied config={config}/>);
}
else if (layoutRequest.status &&
!contains(layoutRequest.status, [200, 'loading'])
) {
return (<div>{'Error loading layout'}</div>);
}
else if (
dependenciesRequest.status &&
!contains(dependenciesRequest.status, [200, 'loading'])
) {
return (<div>{'Error loading dependencies'}</div>);
}
else if (appLifecycle === APP_STATES('HYDRATED')) {
return (
<div id="_dash-app-content">
{renderTree(layout, dependenciesRequest.content)}
</div>
);
}
else {
return (<div className="_dash-loading">{'Loading...'}</div>);
}
}
}
UnconnectedContainer.propTypes = {
appLifecycle: PropTypes.oneOf([
APP_STATES('STARTED'),
APP_STATES('HYDRATED')
]),
dispatch: PropTypes.function,
config: PropTypes.object,
dependenciesRequest: PropTypes.object,
lastUpdateComponentRequest: PropTypes.objec,
layoutRequest: PropTypes.object,
layout: PropTypes.object,
paths: PropTypes.object,
history: PropTypes.array
}
const Container = connect(
// map state to props
state => ({
appLifecycle: state.appLifecycle,
config: state.config,
dependenciesRequest: state.dependenciesRequest,
lastUpdateComponentRequest: state.lastUpdateComponentRequest,
layoutRequest: state.layoutRequest,
layout: state.layout,
graphs: state.graphs,
paths: state.paths,
history: state.history
}),
dispatch => ({dispatch})
)(UnconnectedContainer);
export default Container;