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

240 lines
10 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';
2021-04-06 10:01:05 +00:00
import { dataBaseSources, apiSources, 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-08 01:20:30 +00:00
import { Stripe } from './Stripe';
2021-04-13 09:33:31 +00:00
import { Firestore } from './Firestore';
2021-04-08 01:20:30 +00:00
import { defaultOptions } from './DefaultOptions';
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-08 01:20:30 +00:00
options: {}
2021-04-03 05:25:41 +00:00
};
}
componentDidMount() {
console.log('props',this.props);
2021-04-07 05:02:44 +00:00
this.setState({
appId: this.props.appId
2021-04-07 05:02:44 +00:00
})
}
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;
2021-04-15 07:39:45 +00:00
const parsedOptions = Object.keys(options).map((key) => { return {
key: key,
value: options[key],
encrypted: selectedDataSource.options[key].encrypted
}});
datasourceService.create(appId, name, kind, parsedOptions).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
}
2021-04-12 13:35:48 +00:00
testDataSource = () => {
let _self = this;
const { appId, options, selectedDataSource, name } = this.state;
const kind = selectedDataSource.kind;
datasourceService.test(appId, name, kind, options).then((data) => {
toast.success('Datasource Connection Tested, Successfully!', { hideProgressBar: true, position: "top-center", });
},(error) => {
toast.error('Datasource Connection Error', { hideProgressBar: true, position: "top-center", });
});
}
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
2021-04-10 02:06:17 +00:00
{!showModal && <button className="btn btn-light btn-sm" onClick={() => this.setState({ showModal: true, selectedDataSource: null })}>+</button>}
2021-04-03 05:25:41 +00:00
<Modal
show={this.state.showModal}
2021-04-06 10:01:05 +00:00
size="lg"
2021-04-03 05:25:41 +00:00
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-06 10:01:05 +00:00
x
2021-04-03 05:25:41 +00:00
</Button>
</Modal.Header>
<Modal.Body>
{!selectedDataSource &&
2021-04-06 10:01:05 +00:00
<div>
<div class="row row-deck">
<h4 className="text-muted mb-2">DATABASES</h4>
2021-04-09 15:59:27 +00:00
{dataBaseSources.map((dataSource) => (<div class="col-md-3" key={dataSource.name}>
2021-04-13 09:33:31 +00:00
<div class="card mb-3" role="button" onClick={() => this.selectDataSource(dataSource)}>
2021-04-06 10:01:05 +00:00
<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>
<div class="row row-deck mt-5">
<h4 className="text-muted mb-2">APIS</h4>
2021-04-09 15:59:27 +00:00
{apiSources.map((dataSource) => (<div class="col-md-3" key={dataSource.name}>
2021-04-06 10:01:05 +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>
2021-04-03 05:25:41 +00:00
</div>
</div>
2021-04-06 10:01:05 +00:00
))}
</div>
2021-04-03 05:25:41 +00:00
</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}
2021-04-12 13:35:48 +00:00
testDataSource={this.testDataSource}
2021-04-06 03:43:46 +00:00
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-08 01:20:30 +00:00
{selectedDataSource.kind === 'stripe' &&
<Stripe
optionchanged={this.optionchanged}
createDataSource={this.createDataSource}
options={options}
hideModal={this.hideModal}
/>
}
2021-04-13 09:33:31 +00:00
{selectedDataSource.kind === 'firestore' &&
<Firestore
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>
)
}
}
2021-04-09 15:59:27 +00:00
export { DataSourceManager };