forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add datasource picker component and use it in devtools and tutorial p…
…age when multiple datasource is enabled (opensearch-project#5756) * add datasource picker as a component and use it in devtools and add sample data page Signed-off-by: Lu Yu <nluyu@amazon.com> * add changelog for 5756 Signed-off-by: Lu Yu <nluyu@amazon.com> * wraps prepend with i18n and fix typo Signed-off-by: Lu Yu <nluyu@amazon.com> --------- Signed-off-by: Lu Yu <nluyu@amazon.com>
- Loading branch information
Showing
5 changed files
with
111 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...plugins/data_source_management/public/components/data_source_picker/data_source_picker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { getDataSources } from '../utils'; | ||
import { i18n } from '@osd/i18n'; | ||
import { EuiComboBox } from '@elastic/eui'; | ||
|
||
export const LocalCluster = { | ||
label: i18n.translate('dataSource.localCluster', { | ||
defaultMessage: 'Local cluster', | ||
}), | ||
id: '', | ||
}; | ||
|
||
export class DataSourcePicker extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
dataSources: [], | ||
selectedOption: [LocalCluster], | ||
}; | ||
} | ||
|
||
componentWillUnmount() { | ||
this._isMounted = false; | ||
} | ||
|
||
async componentDidMount() { | ||
this._isMounted = true; | ||
getDataSources(this.props.savedObjectsClient) | ||
.then((fetchedDataSources) => { | ||
if (fetchedDataSources?.length) { | ||
const dataSourceOptions = fetchedDataSources.map((dataSource) => ({ | ||
id: dataSource.id, | ||
label: dataSource.title, | ||
})); | ||
|
||
dataSourceOptions.push(LocalCluster); | ||
|
||
if (!this._isMounted) return; | ||
this.setState({ | ||
...this.state, | ||
dataSources: dataSourceOptions, | ||
}); | ||
} | ||
}) | ||
.catch(() => { | ||
this.props.notifications.addWarning( | ||
i18n.translate('dataSource.fetchDataSourceError', { | ||
defaultMessage: 'Unable to fetch existing data sources', | ||
}) | ||
); | ||
}); | ||
} | ||
|
||
onChange(e) { | ||
if (!this._isMounted) return; | ||
this.setState({ | ||
selectedOption: e, | ||
}); | ||
this.props.onSelectedDataSource(e); | ||
} | ||
|
||
render() { | ||
return ( | ||
<EuiComboBox | ||
aria-label={i18n.translate('dataSourceComboBoxAriaLabel', { | ||
defaultMessage: 'Select a data source', | ||
})} | ||
placeholder={i18n.translate('dataSourceComboBoxPlaceholder', { | ||
defaultMessage: 'Select a data source', | ||
})} | ||
singleSelection={{ asPlainText: true }} | ||
options={this.state.dataSources} | ||
selectedOptions={this.state.selectedOption} | ||
onChange={(e) => this.onChange(e)} | ||
prepend={i18n.translate('dataSourceComboBoxPrepend', { | ||
defaultMessage: 'Data source', | ||
})} | ||
compressed | ||
isDisabled={this.props.disabled} | ||
/> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters