import React from 'react'; import { datasourceService, authenticationService } from '@/_services'; import Modal from 'react-bootstrap/Modal'; import Button from 'react-bootstrap/Button'; import { ToastContainer, toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import { dataBaseSources, apiSources, DataSourceTypes } from './DataSourceTypes'; import { Elasticsearch } from './Elasticsearch'; import { Redis } from './Redis'; import { Postgresql } from './Postgresql'; import { Mysql } from './Mysql'; import { Stripe } from './Stripe'; import { Firestore } from './Firestore'; import { RestApi } from './RestApi'; import { defaultOptions } from './DefaultOptions'; class DataSourceManager extends React.Component { constructor(props) { super(props); this.state = { currentUser: authenticationService.currentUserValue, showModal: false, appId: props.appId, options: {} }; } componentDidMount() { console.log('props',this.props); this.setState({ appId: this.props.appId }) } selectDataSource = (source) => { this.setState({ selectedDataSource: source, options: defaultOptions[source.kind], name: source.kind }); } onNameChanged = (newName) => { this.setState({ name: newName }) } optionchanged = (option, value) => { this.setState( { options: { ...this.state.options, [option]: value } } ); } hideModal = () => { this.setState({ showModal: false, selectedDataSource: null }); } createDataSource = () => { let _self = this; const { appId, options, selectedDataSource, name } = this.state; const kind = selectedDataSource.kind; const parsedOptions = Object.keys(options).map((key) => { console.log('ky', key); return { key: key, value: options[key], encrypted: selectedDataSource.options[key].encrypted }}); datasourceService.create(appId, name, kind, parsedOptions).then((data) => { this.setState( { showModal: false } ); toast.success('Datasource Added', { hideProgressBar: true, position: "top-center", }); this.props.dataSourcesChanged(); }); } 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", }); }); } render() { const { showModal, selectedDataSource, options } = this.state; return (
{!showModal && } {selectedDataSource &&
this.onNameChanged(e.target.value)} class="form-control-plaintext form-control-plaintext-sm col" value={this.state.name} autoFocus />
}
{!selectedDataSource &&

DATABASES

{dataBaseSources.map((dataSource) => (
this.selectDataSource(dataSource)}>




{dataSource.name}
))}

APIS

{apiSources.map((dataSource) => (
this.selectDataSource(dataSource)}>




{dataSource.name}
))}
} {selectedDataSource &&
{selectedDataSource.kind === 'elasticsearch' && } {selectedDataSource.kind === 'redis' && } {selectedDataSource.kind === 'postgresql' && } {selectedDataSource.kind === 'mysql' && } {selectedDataSource.kind === 'stripe' && } {selectedDataSource.kind === 'firestore' && } {selectedDataSource.kind === 'restapi' && }
}
) } } export { DataSourceManager };