ToolJet/frontend/src/Editor/DataSourceManager/DataSourceManager.jsx

211 lines
7.4 KiB
React
Raw Normal View History

2021-04-03 05:25:41 +00:00
import React from 'react';
2021-04-03 05:57:55 +00:00
import { datasourceService, authenticationService } from '@/_services';
2021-04-03 05:25:41 +00:00
import Modal from 'react-bootstrap/Modal';
import Button from 'react-bootstrap/Button';
2021-04-03 05:57:55 +00:00
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import { DataSourceTypes } from './DataSourceTypes';
2021-04-06 03:27:58 +00:00
import { Elasticsearch } from './Elasticsearch';
2021-04-06 03:43:46 +00:00
import { Redis } from './Redis';
import { Postgresql } from './Postgresql';
import { Mysql } from './Mysql';
2021-04-06 03:27:58 +00:00
const defaultOptions = {
'postgresql': {
2021-04-06 03:45:11 +00:00
host: 'localhost',
port: 5000,
username: '',
password: ''
},
'mysql': {
host: 'localhost',
port: 5000,
username: '',
password: ''
},
'redis': {
host: 'localhost',
port: 5000,
username: '',
password: ''
2021-04-06 03:27:58 +00:00
},
'elasticsearch': {
host: 'localhost',
port: 5000,
username: '',
password: ''
}
}
2021-04-03 05:25:41 +00:00
class DataSourceManager extends React.Component {
constructor(props) {
super(props);
this.state = {
currentUser: authenticationService.currentUserValue,
showModal: false,
2021-04-03 05:57:55 +00:00
appId: props.appId,
2021-04-03 05:25:41 +00:00
options: {
2021-04-06 03:27:58 +00:00
2021-04-03 05:25:41 +00:00
}
};
}
componentDidMount() {
console.log('props',this.props);
this.state = {
appId: this.props.appId
}
}
2021-04-03 05:25:41 +00:00
selectDataSource = (source) => {
2021-04-06 03:27:58 +00:00
this.setState({
selectedDataSource: source,
options: defaultOptions[source.kind],
name: source.kind
2021-04-06 03:27:58 +00:00
});
2021-04-03 05:25:41 +00:00
}
onNameChanged = (newName) => {
this.setState({
name: newName
})
}
2021-04-03 05:25:41 +00:00
optionchanged = (option, value) => {
this.setState( { options: { ...this.state.options, [option]: value } } );
}
2021-04-06 03:27:58 +00:00
hideModal = () => {
2021-04-06 03:43:46 +00:00
this.setState({
showModal: false,
selectedDataSource: null
});
2021-04-06 03:27:58 +00:00
}
2021-04-03 05:57:55 +00:00
createDataSource = () => {
let _self = this;
2021-04-03 05:25:41 +00:00
const { appId, options, selectedDataSource, name } = this.state;
2021-04-03 05:57:55 +00:00
const kind = selectedDataSource.kind;
datasourceService.create(appId, name, kind, options).then((data) => {
2021-04-03 05:57:55 +00:00
this.setState( { showModal: false } );
toast.success('Datasource Added', { hideProgressBar: true, position: "top-center", });
this.props.dataSourcesChanged();
2021-04-03 05:57:55 +00:00
});
2021-04-03 05:25:41 +00:00
}
render() {
const { showModal, selectedDataSource, options } = this.state;
return (
<div>
2021-04-03 05:57:55 +00:00
<ToastContainer/>
2021-04-03 05:25:41 +00:00
{!showModal && <button className="btn btn-light" onClick={() => this.setState({ showModal: true })}>+</button>}
<Modal
show={this.state.showModal}
size="xl"
className="mt-5"
// onHide={handleClose}
backdrop="static"
keyboard={false}>
<Modal.Header>
<Modal.Title>
{selectedDataSource &&
<div className="row">
<img src={selectedDataSource.icon} height="25" width="25" className="mt-2 col-md-2"></img>
<input
type="text"
onChange={(e) => this.onNameChanged(e.target.value)}
class="form-control-plaintext form-control-plaintext-sm col"
value={this.state.name}
autoFocus
/>
</div>
}
</Modal.Title>
2021-04-06 03:43:46 +00:00
<Button variant="light" onClick={() => this.hideModal()}>
2021-04-03 05:25:41 +00:00
Close
</Button>
</Modal.Header>
<Modal.Body>
{!selectedDataSource &&
<div class="row row-deck">
{DataSourceTypes.map((dataSource) => (<div class="col-md-2">
2021-04-03 05:25:41 +00:00
<div class="card" role="button" onClick={() => this.selectDataSource(dataSource)}>
<div class="card-body">
<center>
<img src={dataSource.icon} width="50" height="50" alt=""/>
<br></br>
<br></br>
{dataSource.name}
</center>
</div>
</div>
</div>
))}
</div>
}
{selectedDataSource &&
<div>
2021-04-06 03:27:58 +00:00
{selectedDataSource.kind === 'elasticsearch' &&
<Elasticsearch
optionchanged={this.optionchanged}
createDataSource={this.createDataSource}
options={options}
hideModal={this.hideModal}
/>
}
2021-04-06 03:43:46 +00:00
{selectedDataSource.kind === 'redis' &&
<Redis
optionchanged={this.optionchanged}
createDataSource={this.createDataSource}
options={options}
hideModal={this.hideModal}
/>
}
2021-04-03 05:25:41 +00:00
{selectedDataSource.kind === 'postgresql' &&
2021-04-06 03:43:46 +00:00
<Postgresql
optionchanged={this.optionchanged}
createDataSource={this.createDataSource}
options={options}
hideModal={this.hideModal}
/>
2021-04-03 05:25:41 +00:00
}
2021-04-06 03:45:11 +00:00
{selectedDataSource.kind === 'mysql' &&
<Mysql
optionchanged={this.optionchanged}
createDataSource={this.createDataSource}
options={options}
hideModal={this.hideModal}
/>
}
2021-04-06 03:43:46 +00:00
2021-04-03 05:25:41 +00:00
</div>
}
</Modal.Body>
<Modal.Footer>
</Modal.Footer>
</Modal>
</div>
)
}
}
export { DataSourceManager };