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 pathTreeContainer.js
146 lines (127 loc) · 3.84 KB
/
TreeContainer.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
'use strict';
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import Registry from './registry';
import NotifyObservers from './components/core/NotifyObservers.react';
import {connect} from 'react-redux';
import {
isNil,
omit,
contains,
isEmpty,
forEach,
propOr,
type,
has,
} from 'ramda';
import {STATUS} from './constants/constants';
class TreeContainer extends Component {
shouldComponentUpdate(nextProps) {
return nextProps.layout !== this.props.layout;
}
render() {
return recursivelyRender(this.props.layout, this.props.requestQueue);
}
}
TreeContainer.propTypes = {
layout: PropTypes.object,
requestQueue: PropTypes.object,
};
function recursivelyRender(component, requestQueue) {
if (contains(type(component), ['String', 'Number', 'Null', 'Boolean'])) {
return component;
}
if (isEmpty(component)) {
return null;
}
// Create list of child elements
let children;
const componentProps = propOr({}, 'props', component);
if (
!has('props', component) ||
!has('children', component.props) ||
typeof component.props.children === 'undefined'
) {
// No children
children = [];
} else if (
contains(type(component.props.children), [
'String',
'Number',
'Null',
'Boolean',
])
) {
children = [component.props.children];
} else {
// One or multiple objects
// Recursively render the tree
// TODO - I think we should pass in `key` here.
children = (Array.isArray(componentProps.children)
? componentProps.children
: [componentProps.children]
).map(child => recursivelyRender(child, requestQueue));
}
if (!component.type) {
/* eslint-disable no-console */
console.error(type(component), component);
/* eslint-enable no-console */
throw new Error('component.type is undefined');
}
if (!component.namespace) {
/* eslint-disable no-console */
console.error(type(component), component);
/* eslint-enable no-console */
throw new Error('component.namespace is undefined');
}
const element = Registry.resolve(component.type, component.namespace);
const parent = React.createElement(
element,
omit(['children'], component.props),
...children
);
// loading prop coming from TreeContainer
let isLoading = false;
let loadingProp;
let loadingComponent;
const id = componentProps.id;
if (requestQueue && requestQueue.filter) {
forEach(r => {
const controllerId = isNil(r.controllerId) ? '' : r.controllerId;
if (r.status === 'loading' && contains(id, controllerId)) {
isLoading = true;
[loadingComponent, loadingProp] = r.controllerId.split('.');
}
}, requestQueue);
const thisRequest = requestQueue.filter(r => {
const controllerId = isNil(r.controllerId) ? '' : r.controllerId;
return contains(id, controllerId);
});
if (thisRequest.status === STATUS.OK) {
isLoading = false;
}
}
// Set loading state
const loading_state = {
is_loading: isLoading,
prop_name: loadingProp,
component_name: loadingComponent,
};
return (
<NotifyObservers
key={componentProps.id}
id={componentProps.id}
loading_state={loading_state}
>
{parent}
</NotifyObservers>
);
}
function mapStateToProps(state, ownProps) {
return {
layout: ownProps.layout,
loading: ownProps.loading,
requestQueue: state.requestQueue,
};
}
export default connect(mapStateToProps)(TreeContainer);