@@ -5,6 +5,7 @@ import PropTypes from 'prop-types';
5
5
import Registry from './registry' ;
6
6
import { connect } from 'react-redux' ;
7
7
import {
8
+ any ,
8
9
contains ,
9
10
filter ,
10
11
forEach ,
@@ -18,7 +19,6 @@ import {
18
19
propOr ,
19
20
type
20
21
} from 'ramda' ;
21
- import { STATUS } from './constants/constants' ;
22
22
import { notifyObservers , updateProps } from './actions' ;
23
23
24
24
const SIMPLE_COMPONENT_TYPES = [ 'String' , 'Number' , 'Null' , 'Boolean' ] ;
@@ -75,38 +75,6 @@ class TreeContainer extends Component {
75
75
) ;
76
76
}
77
77
78
- getLoadingState ( id , requestQueue ) {
79
- // loading prop coming from TreeContainer
80
- let isLoading = false ;
81
- let loadingProp ;
82
- let loadingComponent ;
83
-
84
- if ( requestQueue && requestQueue . filter ) {
85
- forEach ( r => {
86
- const controllerId = isNil ( r . controllerId ) ? '' : r . controllerId ;
87
- if ( r . status === 'loading' && contains ( id , controllerId ) ) {
88
- isLoading = true ;
89
- [ loadingComponent , loadingProp ] = r . controllerId . split ( '.' ) ;
90
- }
91
- } , requestQueue ) ;
92
-
93
- const thisRequest = requestQueue . filter ( r => {
94
- const controllerId = isNil ( r . controllerId ) ? '' : r . controllerId ;
95
- return contains ( id , controllerId ) ;
96
- } ) ;
97
- if ( thisRequest . status === STATUS . OK ) {
98
- isLoading = false ;
99
- }
100
- }
101
-
102
- // Set loading state
103
- return {
104
- is_loading : isLoading ,
105
- prop_name : loadingProp ,
106
- component_name : loadingComponent ,
107
- } ;
108
- }
109
-
110
78
getSetProps ( ) {
111
79
return newProps => {
112
80
const {
@@ -145,7 +113,10 @@ class TreeContainer extends Component {
145
113
}
146
114
147
115
shouldComponentUpdate ( nextProps ) {
148
- return nextProps . _dashprivate_layout !== this . props . _dashprivate_layout ;
116
+ const { _dashprivate_layout, _dashprivate_loadingState } = nextProps ;
117
+
118
+ return _dashprivate_layout !== this . props . _dashprivate_layout ||
119
+ _dashprivate_loadingState . is_loading !== this . props . _dashprivate_loadingState . is_loading ;
149
120
}
150
121
151
122
getLayoutProps ( ) {
@@ -156,23 +127,23 @@ class TreeContainer extends Component {
156
127
const {
157
128
_dashprivate_dispatch,
158
129
_dashprivate_layout,
159
- _dashprivate_requestQueue
130
+ _dashprivate_loadingState
160
131
} = this . props ;
161
132
162
133
const layoutProps = this . getLayoutProps ( ) ;
163
134
164
135
const children = this . getChildren ( layoutProps . children ) ;
165
- const loadingState = this . getLoadingState ( layoutProps . id , _dashprivate_requestQueue ) ;
166
136
const setProps = this . getSetProps ( _dashprivate_dispatch ) ;
167
137
168
- return this . getComponent ( _dashprivate_layout , children , loadingState , setProps ) ;
138
+ return this . getComponent ( _dashprivate_layout , children , _dashprivate_loadingState , setProps ) ;
169
139
}
170
140
}
171
141
172
142
TreeContainer . propTypes = {
173
143
_dashprivate_dependencies : PropTypes . any ,
174
144
_dashprivate_dispatch : PropTypes . func ,
175
145
_dashprivate_layout : PropTypes . object ,
146
+ _dashprivate_loadingState : PropTypes . object ,
176
147
_dashprivate_paths : PropTypes . any ,
177
148
_dashprivate_requestQueue : PropTypes . object ,
178
149
} ;
@@ -194,12 +165,78 @@ function mergeProps(stateProps, dispatchProps, ownProps) {
194
165
_dashprivate_dependencies : stateProps . dependencies ,
195
166
_dashprivate_dispatch : dispatchProps . dispatch ,
196
167
_dashprivate_layout : ownProps . _dashprivate_layout ,
197
- _dashprivate_loading : ownProps . _dashprivate_loading ,
168
+ _dashprivate_loadingState : getLoadingState ( ownProps . _dashprivate_layout , stateProps . requestQueue ) ,
198
169
_dashprivate_paths : stateProps . paths ,
199
170
_dashprivate_requestQueue : stateProps . requestQueue ,
200
171
} ;
201
172
}
202
173
174
+ function getLoadingState ( layout , requestQueue ) {
175
+ const ids = isLoadingComponent ( layout ) ?
176
+ getNestedIds ( layout ) :
177
+ ( layout && layout . props . id ?
178
+ [ layout . props . id ] :
179
+ [ ] ) ;
180
+
181
+ let isLoading = false ;
182
+ let loadingProp ;
183
+ let loadingComponent ;
184
+
185
+ if ( requestQueue ) {
186
+ forEach ( r => {
187
+ const controllerId = isNil ( r . controllerId ) ? '' : r . controllerId ;
188
+ if ( r . status === 'loading' && any ( id => contains ( id , controllerId ) , ids ) ) {
189
+ isLoading = true ;
190
+ [ loadingComponent , loadingProp ] = r . controllerId . split ( '.' ) ;
191
+ }
192
+ } , requestQueue ) ;
193
+ }
194
+
195
+ // Set loading state
196
+ return {
197
+ is_loading : isLoading ,
198
+ prop_name : loadingProp ,
199
+ component_name : loadingComponent ,
200
+ } ;
201
+ }
202
+
203
+ function getNestedIds ( layout ) {
204
+ const ids = [ ] ;
205
+ const queue = [ layout ] ;
206
+
207
+ while ( queue . length ) {
208
+ const elementLayout = queue . shift ( ) ;
209
+
210
+ const props = elementLayout &&
211
+ elementLayout . props ;
212
+
213
+ if ( ! props ) {
214
+ continue ;
215
+ }
216
+
217
+ const { children, id } = props ;
218
+
219
+ if ( id ) {
220
+ ids . push ( id ) ;
221
+ }
222
+
223
+ if ( children ) {
224
+ const filteredChildren = filter (
225
+ child => ! isSimpleComponent ( child ) && ! isLoadingComponent ( child ) ,
226
+ Array . isArray ( children ) ? children : [ children ]
227
+ ) ;
228
+
229
+ queue . push ( ...filteredChildren ) ;
230
+ }
231
+ }
232
+
233
+ return ids ;
234
+ }
235
+
236
+ function isLoadingComponent ( layout ) {
237
+ return Registry . resolve ( layout . type , layout . namespace ) . _dashprivate_isLoadingComponent ;
238
+ }
239
+
203
240
export const AugmentedTreeContainer = connect ( mapStateToProps , mapDispatchToProps , mergeProps ) ( TreeContainer ) ;
204
241
205
242
export default AugmentedTreeContainer ;
0 commit comments