Skip to content

Commit 6ad54c8

Browse files
committed
feat: add table api for client requests
1 parent db7ab29 commit 6ad54c8

2 files changed

Lines changed: 66 additions & 48 deletions

File tree

src/pages/project/$db/request.js

Lines changed: 65 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
Form,
1414
Icon,
1515
Checkbox,
16+
notification,
1617
} from 'antd'
1718
import classnames from 'classnames'
1819
import CodeMirror from 'react-codemirror'
@@ -67,20 +68,19 @@ class RequestPage extends React.Component {
6768
keys: [1],
6869
result: null,
6970
visible: true,
71+
code: '',
7072
}
7173
}
7274

7375
constructAPIDomain = () => {
7476
const alias = _get(this.props.config, ['misc', 'alias'], '')
75-
console.log('...l..', this.props.config)
7677
return '//' + alias + '.stg-api.covenantsql.io:15153'
7778
}
7879

7980
constructTableSelection = () => {
80-
const tables = _get(this.props.config, ['misc', 'tables'], [])
81-
return tables.map(t => ({}))
81+
const tables = _get(this.props.config, ['tables'], [])
8282
return (
83-
<Select placeholder="Please select a country">
83+
<Select placeholder="Select a table" style={{ minWidth: '160px' }}>
8484
{tables.map(t => {
8585
if (!t.config.is_deleted) {
8686
return (
@@ -103,26 +103,47 @@ class RequestPage extends React.Component {
103103
this.props.form.validateFields((err, values) => {
104104
if (!err) {
105105
const params = {}
106-
console.log('.///', values)
107-
if (values.key) {
108-
values.key.forEach((item, index) => {
109-
if (item && values.check[index]) {
110-
params[item] = values.value[index]
111-
}
112-
})
113106

114-
if (values.value.data) {
115-
console.log('parsing, ', values.value.data)
116-
params.data = JSON.parse(values.value.data)
107+
if (this.state.url.indexOf(':table') > -1) {
108+
const { table_value, data_value } = values
109+
params.table = table_value
110+
try {
111+
let d = data_value.replace(/[\n\r]+/g, '')
112+
params.data = data_value && JSON.parse(d)
113+
} catch (e) {
114+
notification.error({
115+
message: 'Table change `data` JSON parse error',
116+
duration: 10,
117+
})
118+
return
119+
}
120+
} else {
121+
if (values.key) {
122+
values.key.forEach((item, index) => {
123+
if (item && values.check[index]) {
124+
params[item] = values.value[index]
125+
}
126+
})
117127
}
118128
}
119129

120130
console.log(params)
121-
122-
request({ method, url, data: params }).then(data => {
123-
this.setState({
124-
result: JSON.stringify(data),
131+
request({ method, url, data: params })
132+
.then(data => {
133+
this.setState({
134+
result: JSON.stringify(data),
135+
})
125136
})
137+
.catch(e => {
138+
notification.error({
139+
message: e.message,
140+
duration: 10,
141+
})
142+
})
143+
} else {
144+
notification.error({
145+
message: 'Please input required fields',
146+
description: '',
126147
})
127148
}
128149
})
@@ -171,10 +192,15 @@ class RequestPage extends React.Component {
171192
})
172193
}
173194

195+
onCodeMirrorChange = v => {
196+
this.setState({
197+
code: v,
198+
})
199+
}
200+
174201
render() {
175202
const { result, url, method, keys, visible } = this.state
176203
const { getFieldDecorator } = this.props.form
177-
console.log('///////////', this.state)
178204

179205
return (
180206
<Page inner>
@@ -304,42 +330,35 @@ class RequestPage extends React.Component {
304330
</Col>
305331
<Col style={{ marginTop: 8 }}>
306332
{getFieldDecorator(`key[table]`)(
307-
<Input placeholder="table" value={'table'} disabled />
333+
<Input placeholder="table" disabled />
308334
)}
309335
</Col>
310336
<Col style={{ marginTop: 8 }}>
311-
{getFieldDecorator(`value[table]`)(
312-
<Input placeholder="Value" />
313-
)}
337+
{getFieldDecorator(`table_value`, {
338+
rules: [
339+
{
340+
required: true,
341+
message: 'Please select a table',
342+
},
343+
],
344+
})(this.constructTableSelection())}
314345
</Col>
315346
</Row>
316347
<Row gutter={8}>
317348
<div style={{ padding: '15px 10px 5px', fontWeight: '600' }}>
318349
Data:
319350
</div>
320-
{getFieldDecorator('value[data]', {
321-
rules: [
322-
{
323-
required: true,
324-
message: 'Please input table data',
325-
type: 'json',
326-
},
327-
],
328-
})(
329-
<div style={{ margin: '10px 0' }}>
330-
<CodeMirror
331-
value={this.state.rules}
332-
onChange={this.onCodeMirrorChange}
333-
options={{
334-
lineNumbers: true,
335-
matchBrackets: true,
336-
autoCloseBrackets: true,
337-
mode: 'application/ld+json',
338-
lineWrapping: true,
339-
theme: 'monokai',
340-
}}
341-
/>
342-
</div>
351+
{getFieldDecorator('data_value')(
352+
<CodeMirror
353+
options={{
354+
lineNumbers: true,
355+
matchBrackets: true,
356+
autoCloseBrackets: true,
357+
mode: 'application/ld+json',
358+
lineWrapping: true,
359+
theme: 'monokai',
360+
}}
361+
/>
343362
)}
344363
</Row>
345364
</div>

src/utils/request.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export default function request(options) {
1919
const urlMatch = url.match(/[a-zA-z]+:\/\/[^/]*/)
2020
if (urlMatch) {
2121
;[domain] = urlMatch
22-
console.log('...................', domain, urlMatch)
2322
url = url.slice(domain.length)
2423
}
2524

@@ -33,7 +32,7 @@ export default function request(options) {
3332
}
3433
url = domain + url
3534
} catch (e) {
36-
message.error(e.message)
35+
console.error(e.message)
3736
}
3837

3938
options.url =

0 commit comments

Comments
 (0)