Fix error when dashboard import does not contain template variables

pull/3559/head
Iris Scholten 2018-06-01 16:57:23 -07:00
parent 2eea86707b
commit 0f6c60329e
2 changed files with 36 additions and 25 deletions

View File

@ -35,8 +35,9 @@ import {
import {CellType} from 'src/types/dashboard' import {CellType} from 'src/types/dashboard'
import {makeQueryForTemplate} from 'src/dashboards/utils/templateVariableQueryGenerator' import {makeQueryForTemplate} from 'src/dashboards/utils/templateVariableQueryGenerator'
import parsers from 'src/shared/parsing' import parsers from 'src/shared/parsing'
import {getDeep} from 'src/utils/wrappers'
import {Dashboard, TimeRange, Cell, Query, Source} from 'src/types' import {Dashboard, TimeRange, Cell, Query, Source, Template} from 'src/types'
interface LoadDashboardsAction { interface LoadDashboardsAction {
type: 'LOAD_DASHBOARDS' type: 'LOAD_DASHBOARDS'
@ -477,8 +478,9 @@ export const getChronografVersion = () => async (): Promise<string | void> => {
} }
} }
const removeUnselectedTemplateValues = (dashboard: Dashboard) => { const removeUnselectedTemplateValues = (dashboard: Dashboard): Template[] => {
const templates = dashboard.templates.map(template => { const templates = getDeep<Template[]>(dashboard, 'templates', []).map(
template => {
if (template.type === 'csv') { if (template.type === 'csv') {
return template return template
} }
@ -487,7 +489,8 @@ const removeUnselectedTemplateValues = (dashboard: Dashboard) => {
const values = value ? [value] : [] const values = value ? [value] : []
return {...template, values} return {...template, values}
}) }
)
return templates return templates
} }

View File

@ -5,7 +5,9 @@ import _ from 'lodash'
import Authorized, {EDITOR_ROLE, VIEWER_ROLE} from 'src/auth/Authorized' import Authorized, {EDITOR_ROLE, VIEWER_ROLE} from 'src/auth/Authorized'
import ConfirmButton from 'src/shared/components/ConfirmButton' import ConfirmButton from 'src/shared/components/ConfirmButton'
import {Dashboard} from 'src/types' import {getDeep} from 'src/utils/wrappers'
import {Dashboard, Template} from 'src/types'
interface Props { interface Props {
dashboards: Dashboard[] dashboards: Dashboard[]
@ -49,17 +51,7 @@ class DashboardsTable extends PureComponent<Props> {
{dashboard.name} {dashboard.name}
</Link> </Link>
</td> </td>
<td> <td>{this.getDashboardTemplates(dashboard)}</td>
{dashboard.templates.length ? (
dashboard.templates.map(tv => (
<code className="table--temp-var" key={tv.id}>
{tv.tempVar}
</code>
))
) : (
<span className="empty-string">None</span>
)}
</td>
<td className="text-right"> <td className="text-right">
<Authorized <Authorized
requiredRole={VIEWER_ROLE} requiredRole={VIEWER_ROLE}
@ -101,7 +93,23 @@ class DashboardsTable extends PureComponent<Props> {
) )
} }
private get emptyStateDashboard() { private getDashboardTemplates = (
dashboard: Dashboard
): JSX.Element | JSX.Element[] => {
const templates = getDeep<Template[]>(dashboard, 'templates', [])
if (templates.length) {
return templates.map(tv => (
<code className="table--temp-var" key={tv.id}>
{tv.tempVar}
</code>
))
}
return <span className="empty-string">None</span>
}
private get emptyStateDashboard(): JSX.Element {
const {onCreateDashboard} = this.props const {onCreateDashboard} = this.props
return ( return (
<Authorized <Authorized
@ -125,7 +133,7 @@ class DashboardsTable extends PureComponent<Props> {
) )
} }
private get unauthorizedEmptyState() { private get unauthorizedEmptyState(): JSX.Element {
return ( return (
<div className="generic-empty-state"> <div className="generic-empty-state">
<h4 style={{margin: '90px 0'}}> <h4 style={{margin: '90px 0'}}>