/
csvtable.jsx
304 lines (284 loc) · 10 KB
/
csvtable.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import React, { Component } from 'react'
import axios from 'axios'
import BootstrapTable from 'react-bootstrap-table-next'
import { Collapse, CustomInput, Input, FormGroup, ButtonGroup, Button } from 'reactstrap'
import update from 'react-addons-update'
import PropTypes from 'prop-types'
import AdvancedOptions from './advancedoptions'
import Utils from '../../classes/utils'
import ErrorDiv from '../error/error'
export default class CsvTable extends Component {
constructor (props) {
super(props)
this.utils = new Utils()
this.state = {
name: props.file.name,
id: props.file.id,
header: props.file.data.header.map(elem => {
return {name: elem, newName: elem, input: false}
}),
columns_type: props.file.data.columns_type,
integrated: false,
publicTick: false,
privateTick: false,
customUri: "",
externalEndpoint: "",
error: false,
errorMessage: null,
status: null
}
this.cancelRequest
this.headerFormatter = this.headerFormatter.bind(this)
this.handleChangeType = this.handleChangeType.bind(this)
this.integrate = this.integrate.bind(this)
this.toggleHeaderForm = this.toggleHeaderForm.bind(this)
this.handleChangeHeader = this.handleChangeHeader.bind(this)
}
handleChangeType (index, event) {
this.setState({
columns_type: this.state.columns_type.map((elem, i) => i == index ? event.target.value : elem),
publicTick: false,
privateTick: false
})
}
confirmUpdateHeader (event) {
if (event.key === "Enter") {
// Update column type if a relation is entered
if (event.target.value.split("@").length == 2) {
let newType = "general_relation"
this.setState({
header: update(this.state.header, { [event.target.id]: {input: { $set: false}}}),
columns_type: update(this.state.columns_type, { [event.target.id]: { $set: newType }})
})
} else {
this.setState({
header: update(this.state.header, { [event.target.id]: {input: { $set: false}}})
})
}
}
}
headerFormatter (column, colIndex) {
let boundChangeType = this.handleChangeType.bind(this, colIndex)
let colInput = <p id={colIndex} onClick={this.toggleHeaderForm}>{this.state.header[colIndex]["newName"]}</p>
if (this.state.header[colIndex]["input"]) {
colInput = <Input
value={this.state.header[colIndex]["newName"]}
name="header"
id={colIndex}
onChange={this.handleChangeHeader}
onFocus={event => {event.target.select()}}
autoFocus
onKeyPress={event => this.confirmUpdateHeader(event)}
/>
}
if (colIndex == 0) {
return (
<div>
<FormGroup>
{colInput}
<CustomInput type="select" id="typeSelect" name="typeSelect" value={this.state.columns_type[colIndex]} onChange={boundChangeType}>
<option value="start_entity" >Start entity</option>
<option value="entity" >Entity</option>
</CustomInput>
</FormGroup>
</div>
)
}
if (colIndex == 1) {
return (
<div>
<FormGroup>
{colInput}
<CustomInput type="select" id="typeSelect" name="typeSelect" value={this.state.columns_type[colIndex]} onChange={boundChangeType}>
<optgroup label="Properties">
<option value="label" >Entity label</option>
</optgroup>
<optgroup label="Attributes">
<option value="numeric" >Numeric</option>
<option value="text" >Text</option>
<option value="category" >Category</option>
<option value="boolean" >Boolean</option>
<option value="date" >Date</option>
</optgroup>
<optgroup label="Faldo attributes">
<option value="reference" >Reference</option>
<option value="strand" >Strand</option>
<option value="start" >Start</option>
<option value="end" >End</option>
</optgroup>
<optgroup label="Relation">
<option value="general_relation" >Directed</option>
<option value="symetric_relation" >Symetric</option>
</optgroup>
</CustomInput>
</FormGroup>
</div>
)
}
return (
<div>
<FormGroup>
{colInput}
<CustomInput type="select" id="typeSelect" name="typeSelect" value={this.state.columns_type[colIndex]} onChange={boundChangeType}>
<optgroup label="Attributes">
<option value="numeric" >Numeric</option>
<option value="text" >Text</option>
<option value="category" >Category</option>
<option value="boolean" >Boolean</option>
<option value="date" >Date</option>
</optgroup>
<optgroup label="Faldo attributes">
<option value="reference" >Reference</option>
<option value="strand" >Strand</option>
<option value="start" >Start</option>
<option value="end" >End</option>
</optgroup>
<optgroup label="Relation">
<option value="general_relation" >Directed</option>
<option value="symetric_relation" >Symetric</option>
</optgroup>
</CustomInput>
</FormGroup>
</div>
)
}
integrate (event) {
let requestUrl = '/api/files/integrate'
let tick = event.target.value == 'public' ? 'publicTick' : 'privateTick'
let data = {
fileId: this.state.id,
columns_type: this.state.columns_type,
header_names: this.state.header.map(header => {
return header.newName
}),
public: event.target.value == 'public',
type: 'csv',
customUri: this.state.customUri,
externalEndpoint: this.state.externalEndpoint
}
axios.post(requestUrl, data, { baseURL: this.props.config.proxyPath, cancelToken: new axios.CancelToken((c) => { this.cancelRequest = c }) })
.then(response => {
console.log(requestUrl, response.data)
this.setState({
[tick]: true
})
})
.catch(error => {
console.log(error, error.response.data.errorMessage)
this.setState({
error: true,
errorMessage: error.response.data.errorMessage,
status: error.response.status,
waiting: false
})
})
}
handleChangeUri (event) {
this.setState({
customUri: event.target.value,
publicTick: false,
privateTick: false
})
}
handleChangeEndpoint (event) {
this.setState({
externalEndpoint: event.target.value,
publicTick: false,
privateTick: false
})
}
toggleHeaderForm(event) {
this.setState({
header: update(this.state.header, { [event.target.id]: { input: { $set: true } } })
})
}
handleChangeHeader(event) {
this.setState({
header: update(this.state.header, { [event.target.id]: { newName: { $set: event.target.value } } })
})
}
render () {
let columns = this.state.header.map((colName, index) => {
return ({
dataField: this.state.header[index]["name"],
text: this.state.header[index]["name"],
sort: false,
headerFormatter: this.headerFormatter,
index: index,
selectedType: this.state.columns_type[index],
formatter: (cell, row) => {
let text = row[this.state.header[index]["name"]]
if (this.utils.isUrl(text)) {
return <a href={text} target="_blank" rel="noreferrer">{this.utils.truncate(this.utils.splitUrl(text), 25)}</a>
} else {
return this.utils.truncate(text, 25)
}
},
})
})
let privateIcon = <i className="fas fa-lock"></i>
if (this.state.privateTick) {
privateIcon = <i className="fas fa-check text-success"></i>
}
let publicIcon = <i className="fas fa-globe-europe"></i>
if (this.state.publicTick) {
publicIcon = <i className="fas fa-check text-success"></i>
}
let privateButton
if (this.props.config.user.admin || !this.props.config.singleTenant){
privateButton = <Button onClick={this.integrate} value="private" color="secondary" disabled={this.state.privateTick}>{privateIcon} Integrate (private dataset)</Button>
}
let publicButton
if (this.props.config.user.admin || this.props.config.singleTenant) {
publicButton = <Button onClick={this.integrate} value="public" color="secondary" disabled={this.state.publicTick}>{publicIcon} Integrate (public dataset)</Button>
}
let body
if (this.props.file.error) {
body = (
<ErrorDiv status={500} error={this.props.file.error} errorMessage={this.props.file.error_message} />
)
} else {
body = (
<div>
<div className="asko-table-div">
<BootstrapTable
classes="asko-table"
wrapperClasses="asko-table-wrapper"
bootstrap4
keyField={this.state.header[0]["name"]}
data={this.props.file.data.content_preview}
columns={columns}
/>
</div>
<br />
<AdvancedOptions
config={this.props.config}
hideDistantEndpoint={true}
handleChangeUri={p => this.handleChangeUri(p)}
handleChangeEndpoint={p => this.handleChangeEndpoint(p)}
customUri={this.state.customUri}
/>
<br />
<div className="center-div">
<ButtonGroup>
{privateButton}
{publicButton}
</ButtonGroup>
</div>
</div>
)
}
return (
<div>
<h4>{this.state.name} (preview)</h4>
{body}
{this.state.error ? <br /> : ''}
<ErrorDiv status={this.state.status} error={this.state.error} errorMessage={this.state.errorMessage} />
</div>
)
}
}
CsvTable.propTypes = {
file: PropTypes.object,
config: PropTypes.object
}