2021-04-03 11:14:15 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
import { dataqueryService, authenticationService } from '@/_services';
|
|
|
|
|
import { ToastContainer, toast } from 'react-toastify';
|
|
|
|
|
import 'react-toastify/dist/ReactToastify.css';
|
2021-04-04 17:07:03 +00:00
|
|
|
import { RestApi } from './RestApi';
|
|
|
|
|
|
|
|
|
|
const staticDataSources = [
|
|
|
|
|
{ kind: 'js-code', id: 'js-code', name: 'Custom JS Code' },
|
|
|
|
|
{ kind: 'restapi', id: 'restapi', name: 'REST API' },
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
const defaultOptions = {
|
|
|
|
|
'postgresql': {
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
'restapi': {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
url: null,
|
|
|
|
|
url_params: [ ['', ''] ],
|
|
|
|
|
headers: [ ['', ''] ],
|
|
|
|
|
body: [ ['', ''] ],
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-03 11:14:15 +00:00
|
|
|
|
|
|
|
|
class QueryManager extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
currentUser: authenticationService.currentUserValue,
|
|
|
|
|
appId: props.appId,
|
|
|
|
|
dataSources: props.dataSources,
|
2021-04-04 06:26:23 +00:00
|
|
|
dataQueries: props.dataQueries
|
2021-04-03 11:14:15 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.state = {
|
|
|
|
|
appId: this.props.appId,
|
|
|
|
|
dataSources: this.props.dataSources,
|
2021-04-04 06:26:23 +00:00
|
|
|
dataQueries: this.props.dataQueries,
|
2021-04-03 11:14:15 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
changeDataSource = (sourceId) => {
|
2021-04-04 17:07:03 +00:00
|
|
|
const source = [...this.state.dataSources, ...staticDataSources].find(source => source.id === sourceId);
|
|
|
|
|
this.setState({ selectedDataSource: source, options: defaultOptions[source.kind] });
|
2021-04-03 11:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-04 06:26:23 +00:00
|
|
|
computeQueryName = (kind) => {
|
|
|
|
|
const { dataQueries } = this.state;
|
|
|
|
|
const currentQueriesForKind = dataQueries.filter(query => query.kind === kind);
|
|
|
|
|
let found = false;
|
|
|
|
|
let name = '';
|
|
|
|
|
let currentNumber = currentQueriesForKind.length;
|
|
|
|
|
while(!found) {
|
|
|
|
|
name = `${kind}${currentNumber}`;
|
|
|
|
|
if(dataQueries.find(query => query.name === name) === undefined) {
|
|
|
|
|
found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-03 11:14:15 +00:00
|
|
|
createDataQuery = () => {
|
|
|
|
|
const { appId, options, selectedDataSource } = this.state;
|
2021-04-04 06:26:23 +00:00
|
|
|
const name = this.computeQueryName(selectedDataSource.kind);
|
2021-04-03 11:14:15 +00:00
|
|
|
const kind = selectedDataSource.kind;
|
2021-04-04 03:48:04 +00:00
|
|
|
const data_source_id = selectedDataSource.id;
|
2021-04-03 11:14:15 +00:00
|
|
|
|
2021-04-04 03:48:04 +00:00
|
|
|
dataqueryService.create(appId, name, kind, options, data_source_id).then((data) => {
|
2021-04-03 11:14:15 +00:00
|
|
|
this.setState( { showModal: false } );
|
|
|
|
|
toast.success('Datasource Added', { hideProgressBar: true, position: "top-center", });
|
2021-04-04 06:26:23 +00:00
|
|
|
this.props.dataQueriesChanged();
|
2021-04-03 11:14:15 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
optionchanged = (option, value) => {
|
|
|
|
|
this.setState( { options: { ...this.state.options, [option]: value } } );
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-04 17:07:03 +00:00
|
|
|
optionsChanged = (newOptions) => {
|
|
|
|
|
this.setState({ options: newOptions });
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-03 11:14:15 +00:00
|
|
|
render() {
|
|
|
|
|
const { dataSources, selectedDataSource } = this.state;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
|
|
|
|
|
<ToastContainer/>
|
|
|
|
|
|
|
|
|
|
<div className="row header">
|
|
|
|
|
<div className="col-md-9">
|
|
|
|
|
</div>
|
|
|
|
|
<div className="col-md-3">
|
|
|
|
|
<button className="btn btn-light m-1 float-right">Preview</button>
|
2021-04-03 11:36:38 +00:00
|
|
|
<button onClick={this.createDataQuery} className="btn btn-primary m-1 float-right">Create</button>
|
2021-04-03 11:14:15 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="row row-deck p-3">
|
|
|
|
|
|
|
|
|
|
<label class="form-label">Datasource</label>
|
|
|
|
|
|
|
|
|
|
{dataSources &&
|
2021-04-04 17:07:03 +00:00
|
|
|
<select class="form-select m-2" style={{width: '300px'}} onChange={(e) => this.changeDataSource(e.target.value)} >
|
2021-04-03 11:14:15 +00:00
|
|
|
{dataSources.map((source) => (<option value={source.id}>{source.name}</option>))}
|
2021-04-04 17:07:03 +00:00
|
|
|
{staticDataSources.map((source) => (<option value={source.id}>{source.name}</option>))}
|
2021-04-03 11:14:15 +00:00
|
|
|
</select>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{selectedDataSource &&
|
|
|
|
|
<div>
|
|
|
|
|
{ selectedDataSource.kind === 'postgresql' &&
|
|
|
|
|
<div class="mb-3 mt-2">
|
|
|
|
|
<label class="form-label">SQL Query</label>
|
|
|
|
|
<textarea onChange={(e) => this.optionchanged('query', e.target.value)} class="form-control" placeholder="SELECT * FROM"></textarea>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
2021-04-04 17:07:03 +00:00
|
|
|
{ selectedDataSource.kind === 'restapi' &&
|
|
|
|
|
<RestApi
|
|
|
|
|
options={this.state.options}
|
|
|
|
|
optionsChanged={this.optionsChanged}
|
|
|
|
|
/>
|
|
|
|
|
}
|
2021-04-03 11:14:15 +00:00
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-04 06:46:53 +00:00
|
|
|
export { QueryManager };
|