-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathAPIController.react.js
212 lines (194 loc) · 6.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
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
import {connect} from 'react-redux';
import {includes, isEmpty} from 'ramda';
import React, {useEffect, useRef, useState, createContext} from 'react';
import PropTypes from 'prop-types';
import TreeContainer from './TreeContainer';
import GlobalErrorContainer from './components/error/GlobalErrorContainer.react';
import {
dispatchError,
hydrateInitialOutputs,
onError,
setGraphs,
setPaths,
setLayout
} from './actions';
import {computePaths} from './actions/paths';
import {computeGraphs} from './actions/dependencies';
import apiThunk from './actions/api';
import {EventEmitter} from './actions/utils';
import {applyPersistence} from './persistence';
import {getAppState} from './reducers/constants';
import {STATUS} from './constants/constants';
import {getLoadingState, getLoadingHash} from './utils/TreeContainer';
export const DashContext = createContext({});
/**
* Fire off API calls for initialization
* @param {*} props props
* @returns {*} component
*/
const UnconnectedContainer = props => {
const {
appLifecycle,
config,
dependenciesRequest,
error,
layoutRequest,
layout,
loadingMap
} = props;
const [errorLoading, setErrorLoading] = useState(false);
const events = useRef(null);
if (!events.current) {
events.current = new EventEmitter();
}
const renderedTree = useRef(false);
const propsRef = useRef({});
propsRef.current = props;
const provider = useRef({
fn: () => ({
_dashprivate_config: propsRef.current.config,
_dashprivate_dispatch: propsRef.current.dispatch,
_dashprivate_graphs: propsRef.current.graphs,
_dashprivate_loadingMap: propsRef.current.loadingMap
})
});
useEffect(storeEffect.bind(null, props, events, setErrorLoading));
useEffect(() => {
if (renderedTree.current) {
renderedTree.current = false;
events.current.emit('rendered');
}
});
let content;
if (
layoutRequest.status &&
!includes(layoutRequest.status, [STATUS.OK, 'loading'])
) {
content = <div className="_dash-error">Error loading layout</div>;
} else if (
errorLoading ||
(dependenciesRequest.status &&
!includes(dependenciesRequest.status, [STATUS.OK, 'loading']))
) {
content = <div className="_dash-error">Error loading dependencies</div>;
} else if (appLifecycle === getAppState('HYDRATED')) {
renderedTree.current = true;
content = (
<DashContext.Provider value={provider.current}>
<TreeContainer
_dashprivate_error={error}
_dashprivate_layout={layout}
_dashprivate_loadingState={getLoadingState(
layout,
[],
loadingMap
)}
_dashprivate_loadingStateHash={getLoadingHash(
[],
loadingMap
)}
_dashprivate_path={JSON.stringify([])}
/>
</DashContext.Provider>
);
} else {
content = <div className="_dash-loading">Loading...</div>;
}
return config && config.ui === true ? (
<GlobalErrorContainer>{content}</GlobalErrorContainer>
) : (
content
);
};
function storeEffect(props, events, setErrorLoading) {
const {
appLifecycle,
dependenciesRequest,
dispatch,
error,
graphs,
layout,
layoutRequest
} = props;
if (isEmpty(layoutRequest)) {
dispatch(apiThunk('_dash-layout', 'GET', 'layoutRequest'));
} else if (layoutRequest.status === STATUS.OK) {
if (isEmpty(layout)) {
const finalLayout = applyPersistence(
layoutRequest.content,
dispatch
);
dispatch(
setPaths(computePaths(finalLayout, [], null, events.current))
);
dispatch(setLayout(finalLayout));
}
}
if (isEmpty(dependenciesRequest)) {
dispatch(apiThunk('_dash-dependencies', 'GET', 'dependenciesRequest'));
} else if (dependenciesRequest.status === STATUS.OK && isEmpty(graphs)) {
dispatch(
setGraphs(
computeGraphs(
dependenciesRequest.content,
dispatchError(dispatch)
)
)
);
}
if (
// dependenciesRequest and its computed stores
dependenciesRequest.status === STATUS.OK &&
!isEmpty(graphs) &&
// LayoutRequest and its computed stores
layoutRequest.status === STATUS.OK &&
!isEmpty(layout) &&
// Hasn't already hydrated
appLifecycle === getAppState('STARTED')
) {
let hasError = false;
try {
dispatch(hydrateInitialOutputs(dispatchError(dispatch)));
} catch (err) {
// Display this error in devtools, unless we have errors
// already, in which case we assume this new one is moot
if (!error.frontEnd.length && !error.backEnd.length) {
dispatch(onError({type: 'backEnd', error: err}));
}
hasError = true;
} finally {
setErrorLoading(hasError);
}
}
}
UnconnectedContainer.propTypes = {
appLifecycle: PropTypes.oneOf([
getAppState('STARTED'),
getAppState('HYDRATED')
]),
dispatch: PropTypes.func,
dependenciesRequest: PropTypes.object,
graphs: PropTypes.object,
layoutRequest: PropTypes.object,
layout: PropTypes.object,
loadingMap: PropTypes.any,
history: PropTypes.any,
error: PropTypes.object,
config: PropTypes.object
};
const Container = connect(
// map state to props
state => ({
appLifecycle: state.appLifecycle,
dependenciesRequest: state.dependenciesRequest,
layoutRequest: state.layoutRequest,
layout: state.layout,
loadingMap: state.loadingMap,
graphs: state.graphs,
history: state.history,
error: state.error,
config: state.config
}),
dispatch => ({dispatch})
)(UnconnectedContainer);
export default Container;