Disable Save Template button when data has been saved. Refactor remaining dashboard promises into async/await.

pull/1347/head
Hunter Trujillo 2017-04-25 16:36:59 -06:00
parent 20799a3ac1
commit a4815b188f
3 changed files with 45 additions and 16 deletions

View File

@ -143,16 +143,24 @@ export const getDashboardsAsync = () => async dispatch => {
}
}
export const putDashboard = dashboard => dispatch => {
updateDashboardAJAX(dashboard).then(({data}) => {
export const putDashboard = dashboard => async dispatch => {
try {
const {data} = await updateDashboardAJAX(dashboard)
dispatch(updateDashboard(data))
})
} catch (error) {
console.error(error)
// dispatch(errorThrown(error))
}
}
export const updateDashboardCell = (dashboard, cell) => dispatch => {
return updateDashboardCellAJAX(cell).then(({data}) => {
export const updateDashboardCell = (dashboard, cell) => async dispatch => {
try {
const {data} = await updateDashboardCellAJAX(cell)
dispatch(syncDashboardCell(dashboard, data))
})
} catch (error) {
console.error(error)
// dispatch(errorThrown(error))
}
}
export const deleteDashboardAsync = dashboard => async dispatch => {

View File

@ -1,4 +1,5 @@
import React, {Component, PropTypes} from 'react'
import classNames from 'classnames'
import OnClickOutside from 'react-onclickoutside'
import TemplateVariableTable
from 'src/dashboards/components/TemplateVariableTable'
@ -12,6 +13,8 @@ const TemplateVariableManager = ({
templates,
onRunQuerySuccess,
onRunQueryFailure,
onSaveTemplatesSuccess,
isEdited,
}) => (
<div className="template-variable-manager">
<div className="template-variable-manager--header">
@ -21,8 +24,10 @@ const TemplateVariableManager = ({
<div className="page-header__right">
<button className="btn btn-primary btn-sm">Add Variable</button>
<button
className="btn btn-primary btn-sm"
onClick={onEditTemplateVariables(templates)}
className={classNames('btn btn-primary btn-sm', {
disabled: !isEdited,
})}
onClick={onEditTemplateVariables(templates, onSaveTemplatesSuccess)}
>
Save Template
</button>
@ -50,9 +55,11 @@ class TemplateVariableManagerWrapper extends Component {
this.state = {
rows: this.props.templates,
isEdited: false,
}
this.onRunQuerySuccess = ::this.onRunQuerySuccess
this.onSaveTemplatesSuccess = ::this.onSaveTemplatesSuccess
}
onRunQuerySuccess(template, queryConfig, parsedData, {tempVar, label}) {
@ -110,15 +117,22 @@ class TemplateVariableManagerWrapper extends Component {
const newRows = rows.map(r => (r.id === template.id ? templateVariable : r))
this.setState({rows: newRows})
this.setState({rows: newRows, isEdited: true})
}
onSaveTemplatesSuccess() {
this.setState({isEdited: false})
}
render() {
const {rows, isEdited} = this.state
return (
<TemplateVariableManager
{...this.props}
onRunQuerySuccess={this.onRunQuerySuccess}
templates={this.state.rows}
onSaveTemplatesSuccess={this.onSaveTemplatesSuccess}
templates={rows}
isEdited={isEdited}
/>
)
}
@ -129,6 +143,8 @@ const {arrayOf, bool, func, shape, string} = PropTypes
TemplateVariableManager.propTypes = {
...TemplateVariableManagerWrapper.propTypes,
onRunQuerySuccess: func.isRequired,
onSaveTemplatesSuccess: func.isRequired,
isEdited: bool.isRequired,
}
TemplateVariableManagerWrapper.propTypes = {

View File

@ -177,15 +177,20 @@ class DashboardPage extends Component {
}
// TODO: make this work over array of template variables onSave in TVM
handleEditTemplateVariables(templates) {
return e => {
handleEditTemplateVariables(templates, onSaveTemplatesSuccess) {
return async e => {
const {params: {dashboardID}, dashboards} = this.props
const currentDashboard = dashboards.find(({id}) => id === +dashboardID)
this.props.dashboardActions.putDashboard({
...currentDashboard,
templates,
})
try {
await this.props.dashboardActions.putDashboard({
...currentDashboard,
templates,
})
onSaveTemplatesSuccess()
} catch (error) {
console.error(error)
}
}
}