-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathPreviewTable.js
More file actions
252 lines (214 loc) · 8.04 KB
/
PreviewTable.js
File metadata and controls
252 lines (214 loc) · 8.04 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
import React from 'react';
import {Hashtag} from '@gravity-ui/icons';
import DataTable from '@gravity-ui/react-data-table';
import {Button, Icon} from '@gravity-ui/uikit';
import block from 'bem-cn-lite';
import {i18n} from 'i18n';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import {openDialogErrorWithTabs} from 'store/actions/dialog';
import {Utils} from 'ui';
import Markup from '../../../../components/Markup/Markup';
import {BI_ERRORS} from '../../../../constants';
import ContainerLoader from '../../components/ContainerLoader/ContainerLoader';
import './PreviewTable.scss';
const b = block('preview-table');
class PreviewTable extends React.Component {
_collator = new Intl.Collator(undefined, {
numeric: true,
});
get textPreviewLoader() {
const {preview: {readyPreview} = {}} = this.props;
switch (readyPreview.toLowerCase()) {
case 'pending':
return i18n('dataset.dataset-editor.modify', 'label_data-preparation-preview');
case 'loading':
default:
return i18n('dataset.dataset-editor.modify', 'label_loading-dataset-preview');
}
}
getRows() {
const {preview: {data: {Data = []} = {}} = {}} = this.props;
return Data.map((row, index) => {
const preparedRow = row.map((item, itemIndex) => {
if (item && typeof item === 'object') {
// In this place, except for the markup in the form of an object, nothing comes
return <Markup key={`${index}-${itemIndex}`} item={item} />;
}
return item;
});
return Object.assign(
{
positionIndex: index + 1,
},
preparedRow,
);
});
}
getColumns() {
const {preview: {data: {Type = []} = {}} = {}} = this.props;
const columns = Type[1][1].reduce((columnsTable, column, index) => {
const [name, type] = column;
columnsTable.push({
name: index,
header: name,
type,
});
return columnsTable;
}, []);
columns.unshift({
name: 'positionIndex',
header: <Icon className={b('header-icon-table-count')} data={Hashtag} size={14} />,
});
return columns;
}
getTableData() {
try {
const rows = this.getRows();
const columns = this.getColumns();
return {
columns: this.handleColumns(columns),
rows,
startIndex: 0,
};
} catch (error) {
return {
columns: [],
rows: [],
startIndex: 0,
};
}
}
sortRows =
(columnName) =>
({row: rowCurrent}, {row: rowNext}) => {
const valueCurrent = rowCurrent[columnName];
const valueNext = rowNext[columnName];
return this._collator.compare(valueCurrent, valueNext);
};
handleColumns(columns) {
return columns.map((column) => {
const {header, name: columnName} = column;
const sortAscending = this.sortRows(columnName);
return {
...column,
header: <div className={b('header')}>{header}</div>,
className: b('column'),
render: ({value}) => value,
sortAscending,
customStyle: ({header}) => {
const generalStyle = {
paddingTop: '0',
paddingBottom: '0',
};
if (header) {
return {
...generalStyle,
background: 'var(--ds-color-base-area)',
borderTop: '1px solid var(--ds-color-divider)',
borderBottom: '1px solid var(--ds-color-divider)',
};
}
},
};
});
}
getErrorMessage(code) {
switch (code) {
case BI_ERRORS.MATERIALIZATION_NOT_FINISHED:
case BI_ERRORS.DATA_PREPARATION_NOT_FINISHED:
return i18n(
'component.chartkit-error.codes',
'ERR.DS_API.DB.DATA_PREPARATION_NOT_FINISHED',
);
case BI_ERRORS.NO_AVAILABLE_SUBPRODUCTS:
return i18n(
'component.chartkit-error.codes',
'ERR.DS_API.NO_AVAILABLE_SUBPRODUCTS',
);
default:
return i18n('dataset.dataset-editor.modify', 'label_request-dataset-preview-error');
}
}
renderError() {
const {error} = this.props;
const {code} = Utils.parseErrorResponse(error);
return (
<div className={b()}>
<div className={b('error')}>
<span className={b('error-msg-text')}>{this.getErrorMessage(code)}</span>
{/*
For incomplete data preparation, we do not show the button with details
because it's kind of not a mistake
*/}
{code !== BI_ERRORS.DATA_PREPARATION_NOT_FINISHED &&
code !== BI_ERRORS.MATERIALIZATION_NOT_FINISHED && (
<Button
className={b('details-btn')}
view="outlined"
onClick={() => {
this.props.openDialogErrorWithTabs({
error,
title: i18n(
'dataset.dataset-editor.modify',
'label_request-dataset-preview-error',
),
});
}}
>
{i18n('dataset.notifications.view', 'toast_error-action-label')}
</Button>
)}
</div>
</div>
);
}
render() {
const {preview: {isLoading, readyPreview} = {}, view} = this.props;
const isDisplayError = ['failed'].includes(readyPreview);
if (isLoading) {
return (
<div className={b()}>
<div className={b('loader')}>
<ContainerLoader loaderSize="m" text={this.textPreviewLoader} />
</div>
</div>
);
}
if (isDisplayError) {
return this.renderError();
}
const {columns, rows} = this.getTableData();
const isNoData = !rows.length && !columns.length;
return (
<div className={b({view, 'align-center': isNoData})}>
<DataTable
columns={columns}
data={rows}
emptyDataMessage={i18n('dataset.dataset-editor.modify', 'label_no-data')}
settings={{
stickyHead: DataTable.FIXED,
stickyTop: 0,
syncHeadOnResize: true,
highlightRows: true,
stripedRows: false,
displayIndices: false,
sortable: false,
}}
rowClassName={() => b('row')}
theme={'preview-dataset'}
/>
</div>
);
}
}
PreviewTable.propTypes = {
view: PropTypes.string.isRequired,
preview: PropTypes.object.isRequired,
error: PropTypes.object,
openDialogErrorWithTabs: PropTypes.func.isRequired,
};
const mapDispatchToProps = {
openDialogErrorWithTabs,
};
export default connect(null, mapDispatchToProps)(PreviewTable);