-
Notifications
You must be signed in to change notification settings - Fork 481
/
DataTableView.jsx
239 lines (222 loc) · 6.32 KB
/
DataTableView.jsx
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* @overview Component for detailed view of a data table.
*/
import TableControls from './TableControls';
import {DataView} from '../constants';
import DataTable from './DataTable';
import FirebaseStorage from '../firebaseStorage';
import FontAwesome from '../../templates/FontAwesome';
import PropTypes from 'prop-types';
import Radium from 'radium';
import React from 'react';
import {changeView, showWarning, tableType} from '../redux/data';
import * as dataStyles from './dataStyles';
import color from '../../util/color';
import {connect} from 'react-redux';
import {getColumnNamesFromRecords} from '../firebaseMetadata';
import experiments from '../../util/experiments';
const MIN_TABLE_WIDTH = 600;
const styles = {
addColumnHeader: [
dataStyles.headerCell,
{
width: 19
}
],
container: {
flexDirection: 'column',
height: '100%',
minWidth: MIN_TABLE_WIDTH,
maxWidth: '100%',
paddingLeft: experiments.isEnabled(experiments.APPLAB_DATASETS)
? '8px'
: '0px'
},
table: {
minWidth: MIN_TABLE_WIDTH
},
tableWrapper: {
flexGrow: 1,
overflow: 'scroll'
},
pagination: {
float: 'right',
display: 'inline',
marginTop: 10
},
plusIcon: {
alignItems: 'center',
borderRadius: 2,
backgroundColor: 'white',
color: color.teal,
cursor: 'pointer',
display: 'inline-flex',
height: 18,
justifyContent: 'center',
width: 18
}
};
const INITIAL_STATE = {
showDebugView: false
};
class DataTableView extends React.Component {
static propTypes = {
// from redux state
tableColumns: PropTypes.arrayOf(PropTypes.string).isRequired,
tableName: PropTypes.string.isRequired,
tableListMap: PropTypes.object.isRequired,
// "if all of the keys are integers, and more than half of the keys between 0 and
// the maximum key in the object have non-empty values, then Firebase will render
// it as an array."
// https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html
tableRecords: PropTypes.oneOfType([PropTypes.object, PropTypes.array])
.isRequired,
view: PropTypes.oneOf(Object.keys(DataView)),
// from redux dispatch
onShowWarning: PropTypes.func.isRequired,
onViewChange: PropTypes.func.isRequired
};
state = {...INITIAL_STATE};
componentWillReceiveProps(nextProps) {
// Forget about new columns or editing columns when switching between tables.
if (this.props.tableName !== nextProps.tableName) {
this.setState(INITIAL_STATE);
}
}
/**
* @param {Array} records Array of JSON-encoded records.
* @param {string} columns Array of column names.
*/
getColumnNames(records, columns) {
// Make sure 'id' is the first column.
const columnNames = getColumnNamesFromRecords(records);
columns.forEach(columnName => {
if (columnNames.indexOf(columnName) === -1) {
columnNames.push(columnName);
}
});
return columnNames;
}
importCsv = (csvData, onComplete) => {
FirebaseStorage.importCsv(
this.props.tableName,
csvData,
() => {
this.setState(INITIAL_STATE);
onComplete();
},
msg => {
if (String(msg).includes('data is too large')) {
this.props.onShowWarning(msg);
} else {
console.warn(msg);
}
onComplete();
}
);
};
exportCsv = () => {
const tableName = encodeURIComponent(this.props.tableName);
location.href = `/v3/export-firebase-tables/${
Applab.channelId
}/${tableName}`;
};
/** Delete all rows, but preserve the columns. */
clearTable = () => {
FirebaseStorage.clearTable(
this.props.tableName,
() => {},
msg => console.warn(msg)
);
};
toggleDebugView = () => {
const showDebugView = !this.state.showDebugView;
this.setState({showDebugView});
};
getTableJson() {
const records = [];
// Cast Array to Object
const tableRecords = Object.assign({}, this.props.tableRecords);
for (const id in tableRecords) {
records.push(JSON.parse(tableRecords[id]));
}
return JSON.stringify(records, null, 2);
}
render() {
let columnNames = this.getColumnNames(
this.props.tableRecords,
this.props.tableColumns
);
const visible = DataView.TABLE === this.props.view;
const containerStyle = [
styles.container,
{
display: visible ? '' : 'none'
}
];
const debugDataStyle = [
dataStyles.debugData,
{
display: this.state.showDebugView ? '' : 'none'
}
];
return (
<div id="dataTable" style={containerStyle} className="inline-flex">
<div style={dataStyles.viewHeader}>
<span style={dataStyles.backLink}>
<a
id="tableBackToOverview"
style={dataStyles.link}
onClick={() => this.props.onViewChange(DataView.OVERVIEW)}
>
<FontAwesome icon="arrow-circle-left" />
Back to data
</a>
</span>
<span style={dataStyles.debugLink}>
<a
id="uitest-tableDebugLink"
style={dataStyles.link}
onClick={this.toggleDebugView}
>
{this.state.showDebugView ? 'Table view' : 'Debug view'}
</a>
</span>
</div>
<TableControls
columns={columnNames}
clearTable={this.clearTable}
importCsv={this.importCsv}
exportCsv={this.exportCsv}
tableName={this.props.tableName}
/>
<div style={debugDataStyle}>{this.getTableJson()}</div>
{!this.state.showDebugView && (
<DataTable
getColumnNames={this.getColumnNames}
readOnly={
this.props.tableListMap[this.props.tableName] === tableType.SHARED
}
/>
)}
</div>
);
}
}
export default connect(
state => ({
view: state.data.view,
tableColumns: state.data.tableColumns || [],
tableRecords: state.data.tableRecords || {},
tableName: state.data.tableName || '',
tableListMap: state.data.tableListMap || {}
}),
dispatch => ({
onShowWarning(warningMsg, warningTitle) {
dispatch(showWarning(warningMsg, warningTitle));
},
onViewChange(view) {
dispatch(changeView(view));
}
})
)(Radium(DataTableView));