ToolJet/frontend/src/Editor/QueryManager/QueryManager.jsx

183 lines
5.8 KiB
React
Raw Normal View History

import React from 'react';
import { dataqueryService, authenticationService } from '@/_services';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import { Restapi } from './Restapi';
import { Mysql } from './Mysql';
import { Postgresql } from './Postgresql';
const allSources = {
Restapi,
Mysql,
Postgresql
}
2021-04-04 17:07:03 +00:00
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: [ ['', ''] ],
}
}
class QueryManager extends React.Component {
constructor(props) {
super(props);
this.state = {
currentUser: authenticationService.currentUserValue,
appId: props.appId,
dataSources: props.dataSources,
2021-04-07 04:14:40 +00:00
dataQueries: props.dataQueries,
selectedQuery: props.selectedQuery,
mode: props.mode
};
}
componentDidMount() {
2021-04-07 04:14:40 +00:00
const selectedQuery = this.state.selectedQuery;
this.state = {
appId: this.props.appId,
dataSources: this.props.dataSources,
2021-04-04 06:26:23 +00:00
dataQueries: this.props.dataQueries,
2021-04-07 04:14:40 +00:00
mode: this.props.mode
};
2021-04-07 04:14:40 +00:00
if(this.props.mode === 'edit') {
const source = this.props.dataSources.find(source => source.id === selectedQuery.data_source_id)
this.setState({
options: selectedQuery.options,
selectedDataSource: source
})
}
}
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-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;
2021-04-06 03:14:52 +00:00
2021-04-04 06:26:23 +00:00
while(!found) {
name = `${kind}${currentNumber}`;
if(dataQueries.find(query => query.name === name) === undefined) {
found = true;
}
2021-04-06 03:14:52 +00:00
currentNumber = currentNumber + 1
2021-04-04 06:26:23 +00:00
}
return name;
}
2021-04-07 04:14:40 +00:00
createOrUpdateDataQuery = () => {
const { appId, options, selectedDataSource, mode } = this.state;
2021-04-04 06:26:23 +00:00
const name = this.computeQueryName(selectedDataSource.kind);
const kind = selectedDataSource.kind;
2021-04-06 03:14:52 +00:00
const dataSourceId = selectedDataSource.id;
2021-04-07 04:14:40 +00:00
if ( mode === 'edit') {
dataqueryService.update(this.state.selectedQuery.id, options).then((data) => {
toast.success('Datasource Updated', { hideProgressBar: true, position: "top-center", });
this.props.dataQueriesChanged();
});
} else {
dataqueryService.create(appId, name, kind, options, dataSourceId).then((data) => {
toast.success('Datasource Added', { hideProgressBar: true, position: "top-center", });
this.props.dataQueriesChanged();
});
}
2021-04-07 04:14:40 +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 });
}
render() {
2021-04-07 04:14:40 +00:00
const { dataSources, selectedDataSource, mode } = this.state;
let ElementToRender = '';
if(selectedDataSource) {
const sourcecomponentName = selectedDataSource.kind.charAt(0).toUpperCase() + selectedDataSource.kind.slice(1);
ElementToRender = allSources[sourcecomponentName];
}
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-07 04:14:40 +00:00
<button onClick={this.createOrUpdateDataQuery} className="btn btn-primary m-1 float-right">
{ mode === 'edit' ? 'Save' : 'Create' }
</button>
</div>
</div>
<div class="row row-deck p-3">
<label class="form-label col-md-2 p-2">Datasource</label>
{dataSources &&
2021-04-07 04:14:40 +00:00
<select
class="form-select form-sm mb-2"
value={selectedDataSource ? selectedDataSource.id : ''}
style={{width: '300px'}}
onChange={(e) => this.changeDataSource(e.target.value)}
>
{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>))}
</select>
}
{selectedDataSource &&
<div>
<ElementToRender
2021-04-04 17:07:03 +00:00
options={this.state.options}
optionsChanged={this.optionsChanged}
/>
</div>
}
</div>
</div>
)
}
}
2021-04-04 06:46:53 +00:00
export { QueryManager };