This repository has been archived by the owner on Dec 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
DataLayoutWrapper.js
120 lines (103 loc) · 2.68 KB
/
DataLayoutWrapper.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
import React, { cloneElement, Component } from "react";
import { connect } from "react-redux";
import { patchViewAttributes } from "../actions/ViewAttributesActions";
import { parseToDisplay } from "../actions/WindowActions";
class DataLayoutWrapper extends Component {
constructor(props) {
super(props);
this.state = {
layout: [],
data: [],
dataId: null
};
}
componentDidMount = () => {
this.mounted = true;
};
componentWillUnmount = () => {
this.mounted = false;
};
handleChange = (field, value) => {
this.setState({
data: {
...this.state.data,
[field]: {
...this.state.data[field],
value
}
}
});
};
handlePatch = (prop, value, cb) => {
const { windowType: windowId, viewId } = this.props;
const { dataId: rowId } = this.state;
/*eslint-disable */
patchViewAttributes(windowId, viewId, rowId, prop, value).then(({ data }) => {
if (data.length) {
const preparedData = parseToDisplay(data[0].fieldsByName);
if (preparedData) {
Object.keys(preparedData).map(key => {
this.setState({
data: {
...this.state.data,
[key]: {
...this.state.data[key],
...preparedData[key]
}
}
});
});
}
}
});
/*eslint-enable */
cb && cb();
};
setData = (data, dataId, cb) => {
const preparedData = parseToDisplay(data);
this.mounted &&
this.setState(
{
data: preparedData,
dataId: dataId
},
() => {
cb && cb();
}
);
};
setLayout = (layout, cb) => {
this.mounted &&
this.setState(
{
layout: layout
},
() => {
cb && cb();
}
);
};
render() {
const { layout, data } = this.state;
const { children, className } = this.props;
// sometimes it's a number, and React complaints about wrong type
const dataId = this.state.dataId + "";
return (
<div className={className}>
{// The nameing of props has a significant prefix
// to suggest dev that these props are from wrapper
cloneElement(children, {
...this.props,
DLWrapperData: data,
DLWrapperDataId: dataId,
DLWrapperLayout: layout,
DLWrapperSetData: this.setData,
DLWrapperSetLayout: this.setLayout,
DLWrapperHandleChange: this.handleChange,
DLWrapperHandlePatch: this.handlePatch
})}
</div>
);
}
}
export default connect()(DataLayoutWrapper);