mirror of https://github.com/grafana/grafana
parent
ed349075a0
commit
4ecd33c79c
@ -0,0 +1,90 @@ |
||||
import React, { PureComponent } from 'react'; |
||||
import { hot } from 'react-hot-loader'; |
||||
import { connect } from 'react-redux'; |
||||
import PageHeader from '../../core/components/PageHeader/PageHeader'; |
||||
import { DataSource, NavModel } from 'app/types'; |
||||
import { loadDataSource } from './state/actions'; |
||||
import { getNavModel } from '../../core/selectors/navModel'; |
||||
import { getRouteParamsId, getRouteParamsPage } from '../../core/selectors/location'; |
||||
import { getDataSourceLoadingNav } from './state/navModel'; |
||||
import { getDataSource } from './state/selectors'; |
||||
|
||||
export interface Props { |
||||
navModel: NavModel; |
||||
dataSource: DataSource; |
||||
dataSourceId: number; |
||||
pageName: string; |
||||
loadDataSource: typeof loadDataSource; |
||||
} |
||||
|
||||
enum PageTypes { |
||||
Settings = 'settings', |
||||
Permissions = 'permissions', |
||||
Dashboards = 'dashboards', |
||||
} |
||||
|
||||
export class EditDataSourcePage extends PureComponent<Props> { |
||||
componentDidMount() { |
||||
this.fetchDataSource(); |
||||
} |
||||
|
||||
async fetchDataSource() { |
||||
await this.props.loadDataSource(this.props.dataSourceId); |
||||
} |
||||
|
||||
isValidPage(currentPage) { |
||||
return (Object as any).values(PageTypes).includes(currentPage); |
||||
} |
||||
|
||||
getCurrentPage() { |
||||
const currentPage = this.props.pageName; |
||||
|
||||
return this.isValidPage(currentPage) ? currentPage : PageTypes.Settings; |
||||
} |
||||
|
||||
renderPage() { |
||||
switch (this.getCurrentPage()) { |
||||
case PageTypes.Settings: |
||||
return <div>Settings</div>; |
||||
|
||||
case PageTypes.Permissions: |
||||
return <div>Permissions</div>; |
||||
|
||||
case PageTypes.Dashboards: |
||||
return <div>Dashboards</div>; |
||||
} |
||||
|
||||
return null; |
||||
} |
||||
|
||||
render() { |
||||
const { navModel } = this.props; |
||||
|
||||
return ( |
||||
<div> |
||||
<PageHeader model={navModel} /> |
||||
<div className="page-container page-body" /> |
||||
{this.renderPage()} |
||||
</div> |
||||
); |
||||
} |
||||
} |
||||
|
||||
function mapStateToProps(state) { |
||||
const pageName = getRouteParamsPage(state.location) || 'settings'; |
||||
const dataSourceId = getRouteParamsId(state.location); |
||||
const dataSourceLoadingNav = getDataSourceLoadingNav(pageName); |
||||
|
||||
return { |
||||
navModel: getNavModel(state.navIndex, `datasource-${pageName}-${dataSourceId}`, dataSourceLoadingNav), |
||||
dataSourceId: dataSourceId, |
||||
dataSource: getDataSource(state.dataSources, dataSourceId), |
||||
pageName: pageName, |
||||
}; |
||||
} |
||||
|
||||
const mapDispatchToProps = { |
||||
loadDataSource, |
||||
}; |
||||
|
||||
export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(EditDataSourcePage)); |
@ -0,0 +1,97 @@ |
||||
import { DataSource, NavModel, NavModelItem, PluginMeta } from 'app/types'; |
||||
|
||||
export function buildNavModel(dataSource: DataSource, pluginMeta: PluginMeta): NavModelItem { |
||||
const navModel = { |
||||
img: pluginMeta.info.logos.large, |
||||
id: 'datasource-' + dataSource.id, |
||||
subTitle: `Type: ${pluginMeta.name}`, |
||||
url: '', |
||||
text: dataSource.name, |
||||
breadcrumbs: [{ title: 'Data Sources', url: 'datasources' }], |
||||
children: [ |
||||
{ |
||||
active: false, |
||||
icon: 'fa fa-fw fa-sliders', |
||||
id: `datasource-settings-${dataSource.id}`, |
||||
text: 'Settings', |
||||
url: `datasources/edit/${dataSource.id}/settings`, |
||||
}, |
||||
{ |
||||
active: false, |
||||
icon: 'fa fa-fw fa-sliders', |
||||
id: `datasource-permissions-${dataSource.id}`, |
||||
text: 'Permissions', |
||||
url: `datasources/edit/${dataSource.id}/permissions`, |
||||
}, |
||||
], |
||||
}; |
||||
|
||||
if (pluginMeta.includes && pluginMeta.includes.length > 0) { |
||||
navModel.children.push({ |
||||
active: false, |
||||
icon: 'gicon gicon-dashboard', |
||||
id: `datasource-dashboards-${dataSource.id}`, |
||||
text: 'Dashboards', |
||||
url: `datasources/edit/${dataSource.id}/dashboards`, |
||||
}); |
||||
} |
||||
|
||||
return navModel; |
||||
} |
||||
|
||||
export function getDataSourceLoadingNav(pageName: string): NavModel { |
||||
const main = buildNavModel( |
||||
{ |
||||
access: '', |
||||
basicAuth: false, |
||||
database: '', |
||||
id: 1, |
||||
isDefault: false, |
||||
jsonData: { authType: 'credentials', defaultRegion: 'eu-west-2' }, |
||||
name: 'Loading', |
||||
orgId: 1, |
||||
password: '', |
||||
readOnly: false, |
||||
type: 'Loading', |
||||
typeLogoUrl: 'public/img/icn-datasource.svg', |
||||
url: '', |
||||
user: '', |
||||
}, |
||||
{ |
||||
id: '1', |
||||
name: '', |
||||
info: { |
||||
author: { |
||||
name: '', |
||||
url: '', |
||||
}, |
||||
description: '', |
||||
links: [''], |
||||
logos: { |
||||
large: '', |
||||
small: '', |
||||
}, |
||||
screenshots: '', |
||||
updated: '', |
||||
version: '', |
||||
}, |
||||
includes: [{ type: '', name: '', path: '' }], |
||||
} |
||||
); |
||||
|
||||
let node: NavModelItem; |
||||
|
||||
// find active page
|
||||
for (const child of main.children) { |
||||
if (child.id.indexOf(pageName) > 0) { |
||||
child.active = true; |
||||
node = child; |
||||
break; |
||||
} |
||||
} |
||||
|
||||
return { |
||||
main: main, |
||||
node: node, |
||||
}; |
||||
} |
Loading…
Reference in new issue