This repository was 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
/
Copy pathAPIController.react.js
137 lines (126 loc) · 3.88 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
import {connect} from 'react-redux';
import {contains, isEmpty, isNil} from 'ramda';
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import TreeContainer from './TreeContainer';
import {
computeGraphs,
computePaths,
hydrateInitialOutputs,
setLayout,
} from './actions/index';
import {getDependencies, getLayout} from './actions/api';
import {getAppState} from './reducers/constants';
import {STATUS} from './constants/constants';
/**
* 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 === STATUS.OK) {
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 === STATUS.OK &&
isEmpty(graphs)
) {
dispatch(computeGraphs(dependenciesRequest.content));
}
if (
// dependenciesRequest and its computed stores
dependenciesRequest.status === STATUS.OK &&
!isEmpty(graphs) &&
// LayoutRequest and its computed stores
layoutRequest.status === STATUS.OK &&
!isEmpty(layout) &&
!isNil(paths) &&
// Hasn't already hydrated
appLifecycle === getAppState('STARTED')
) {
dispatch(hydrateInitialOutputs());
}
}
render() {
const {
appLifecycle,
dependenciesRequest,
layoutRequest,
layout,
} = this.props;
if (
layoutRequest.status &&
!contains(layoutRequest.status, [STATUS.OK, 'loading'])
) {
return <div className="_dash-error">{'Error loading layout'}</div>;
} else if (
dependenciesRequest.status &&
!contains(dependenciesRequest.status, [STATUS.OK, 'loading'])
) {
return (
<div className="_dash-error">
{'Error loading dependencies'}
</div>
);
} else if (appLifecycle === getAppState('HYDRATED')) {
return (
<div id="_dash-app-content">
<TreeContainer _dashprivate_layout={layout} />
</div>
);
}
return <div className="_dash-loading">{'Loading...'}</div>;
}
}
UnconnectedContainer.propTypes = {
appLifecycle: PropTypes.oneOf([
getAppState('STARTED'),
getAppState('HYDRATED'),
]),
dispatch: PropTypes.func,
dependenciesRequest: PropTypes.object,
layoutRequest: PropTypes.object,
layout: PropTypes.object,
paths: PropTypes.object,
history: PropTypes.array,
};
const Container = connect(
// map state to props
state => ({
appLifecycle: state.appLifecycle,
dependenciesRequest: state.dependenciesRequest,
layoutRequest: state.layoutRequest,
layout: state.layout,
graphs: state.graphs,
paths: state.paths,
history: state.history,
}),
dispatch => ({dispatch})
)(UnconnectedContainer);
export default Container;