Refactor DashboardPage to use js class

pull/1292/head
Andrew Watkins 2017-04-14 15:28:05 -07:00
parent f97476780b
commit 23ecccb650
1 changed files with 91 additions and 86 deletions

View File

@ -1,4 +1,4 @@
import React, {PropTypes} from 'react' import React, {PropTypes, Component} from 'react'
import {Link} from 'react-router' import {Link} from 'react-router'
import {connect} from 'react-redux' import {connect} from 'react-redux'
import {bindActionCreators} from 'redux' import {bindActionCreators} from 'redux'
@ -13,73 +13,32 @@ import * as dashboardActionCreators from 'src/dashboards/actions'
import {setAutoRefresh} from 'shared/actions/app' import {setAutoRefresh} from 'shared/actions/app'
import {presentationButtonDispatcher} from 'shared/dispatchers' import {presentationButtonDispatcher} from 'shared/dispatchers'
const { class DashboardPage extends Component {
arrayOf, constructor(props) {
bool, super(props)
func,
number,
shape,
string,
} = PropTypes
const DashboardPage = React.createClass({ this.state = {
propTypes: {
source: shape({
links: shape({
proxy: string,
self: string,
}),
}),
params: shape({
sourceID: string.isRequired,
dashboardID: string.isRequired,
}).isRequired,
location: shape({
pathname: string.isRequired,
}).isRequired,
dashboardActions: shape({
putDashboard: func.isRequired,
getDashboardsAsync: func.isRequired,
setTimeRange: func.isRequired,
addDashboardCellAsync: func.isRequired,
editDashboardCell: func.isRequired,
renameDashboardCell: func.isRequired,
}).isRequired,
dashboards: arrayOf(shape({
id: number.isRequired,
cells: arrayOf(shape({})).isRequired,
})),
handleChooseAutoRefresh: func.isRequired,
autoRefresh: number.isRequired,
timeRange: shape({}).isRequired,
inPresentationMode: bool.isRequired,
handleClickPresentationButton: func,
cellQueryStatus: shape({
queryID: string,
status: shape(),
}).isRequired,
},
childContextTypes: {
source: shape({
links: shape({
proxy: string.isRequired,
self: string.isRequired,
}).isRequired,
}).isRequired,
},
getChildContext() {
return {source: this.props.source}
},
getInitialState() {
return {
selectedCell: null, selectedCell: null,
isEditMode: false, isEditMode: false,
isTemplating: false, isTemplating: false,
} }
},
this.handleAddCell = ::this.handleAddCell
this.handleEditDashboard = ::this.handleEditDashboard
this.handleSaveEditedCell = ::this.handleSaveEditedCell
this.handleDismissOverlay = ::this.handleDismissOverlay
this.handleUpdatePosition = ::this.handleUpdatePosition
this.handleChooseTimeRange = ::this.handleChooseTimeRange
this.handleRenameDashboard = ::this.handleRenameDashboard
this.handleEditDashboardCell = ::this.handleEditDashboardCell
this.handleCancelEditDashboard = ::this.handleCancelEditDashboard
this.handleDeleteDashboardCell = ::this.handleDeleteDashboardCell
this.handleOpenTemplateManager = ::this.handleOpenTemplateManager
this.handleRenameDashboardCell = ::this.handleRenameDashboardCell
this.handleUpdateDashboardCell = ::this.handleUpdateDashboardCell
this.handleCloseTemplateManager = ::this.handleCloseTemplateManager
this.handleSummonOverlayTechnologies = ::this.handleSummonOverlayTechnologies
}
componentDidMount() { componentDidMount() {
const { const {
@ -88,86 +47,86 @@ const DashboardPage = React.createClass({
} = this.props } = this.props
getDashboardsAsync(dashboardID) getDashboardsAsync(dashboardID)
}, }
handleOpenTemplateManager() { handleOpenTemplateManager() {
this.setState({isTemplating: true}) this.setState({isTemplating: true})
}, }
handleCloseTemplateManager() { handleCloseTemplateManager() {
this.setState({isTemplating: false}) this.setState({isTemplating: false})
}, }
handleDismissOverlay() { handleDismissOverlay() {
this.setState({selectedCell: null}) this.setState({selectedCell: null})
}, }
handleSaveEditedCell(newCell) { handleSaveEditedCell(newCell) {
this.props.dashboardActions.updateDashboardCell(this.getActiveDashboard(), newCell) this.props.dashboardActions.updateDashboardCell(this.getActiveDashboard(), newCell)
.then(this.handleDismissOverlay) .then(this.handleDismissOverlay)
}, }
handleSummonOverlayTechnologies(cell) { handleSummonOverlayTechnologies(cell) {
this.setState({selectedCell: cell}) this.setState({selectedCell: cell})
}, }
handleChooseTimeRange({lower}) { handleChooseTimeRange({lower}) {
this.props.dashboardActions.setTimeRange({lower, upper: null}) this.props.dashboardActions.setTimeRange({lower, upper: null})
}, }
handleUpdatePosition(cells) { handleUpdatePosition(cells) {
const newDashboard = {...this.getActiveDashboard(), cells} const newDashboard = {...this.getActiveDashboard(), cells}
this.props.dashboardActions.updateDashboard(newDashboard) this.props.dashboardActions.updateDashboard(newDashboard)
this.props.dashboardActions.putDashboard(newDashboard) this.props.dashboardActions.putDashboard(newDashboard)
}, }
handleAddCell() { handleAddCell() {
this.props.dashboardActions.addDashboardCellAsync(this.getActiveDashboard()) this.props.dashboardActions.addDashboardCellAsync(this.getActiveDashboard())
}, }
handleEditDashboard() { handleEditDashboard() {
this.setState({isEditMode: true}) this.setState({isEditMode: true})
}, }
handleCancelEditDashboard() { handleCancelEditDashboard() {
this.setState({isEditMode: false}) this.setState({isEditMode: false})
}, }
handleRenameDashboard(name) { handleRenameDashboard(name) {
this.setState({isEditMode: false}) this.setState({isEditMode: false})
const newDashboard = {...this.getActiveDashboard(), name} const newDashboard = {...this.getActiveDashboard(), name}
this.props.dashboardActions.updateDashboard(newDashboard) this.props.dashboardActions.updateDashboard(newDashboard)
this.props.dashboardActions.putDashboard(newDashboard) this.props.dashboardActions.putDashboard(newDashboard)
}, }
// Places cell into editing mode. // Places cell into editing mode.
handleEditDashboardCell(x, y, isEditing) { handleEditDashboardCell(x, y, isEditing) {
return () => { return () => {
this.props.dashboardActions.editDashboardCell(this.getActiveDashboard(), x, y, !isEditing) /* eslint-disable no-negated-condition */ this.props.dashboardActions.editDashboardCell(this.getActiveDashboard(), x, y, !isEditing) /* eslint-disable no-negated-condition */
} }
}, }
handleRenameDashboardCell(x, y) { handleRenameDashboardCell(x, y) {
return (evt) => { return (evt) => {
this.props.dashboardActions.renameDashboardCell(this.getActiveDashboard(), x, y, evt.target.value) this.props.dashboardActions.renameDashboardCell(this.getActiveDashboard(), x, y, evt.target.value)
} }
}, }
handleUpdateDashboardCell(newCell) { handleUpdateDashboardCell(newCell) {
return () => { return () => {
this.props.dashboardActions.editDashboardCell(this.getActiveDashboard(), newCell.x, newCell.y, false) this.props.dashboardActions.editDashboardCell(this.getActiveDashboard(), newCell.x, newCell.y, false)
this.props.dashboardActions.putDashboard(this.getActiveDashboard()) this.props.dashboardActions.putDashboard(this.getActiveDashboard())
} }
}, }
handleDeleteDashboardCell(cell) { handleDeleteDashboardCell(cell) {
this.props.dashboardActions.deleteDashboardCellAsync(cell) this.props.dashboardActions.deleteDashboardCellAsync(cell)
}, }
getActiveDashboard() { getActiveDashboard() {
const {params: {dashboardID}, dashboards} = this.props const {params: {dashboardID}, dashboards} = this.props
return dashboards.find(d => d.id === +dashboardID) return dashboards.find(d => d.id === +dashboardID)
}, }
render() { render() {
const { const {
@ -217,12 +176,12 @@ const DashboardPage = React.createClass({
<CellEditorOverlay <CellEditorOverlay
source={source} source={source}
cell={selectedCell} cell={selectedCell}
autoRefresh={autoRefresh}
timeRange={timeRange} timeRange={timeRange}
onCancel={this.handleDismissOverlay} autoRefresh={autoRefresh}
onSave={this.handleSaveEditedCell}
editQueryStatus={dashboardActions.editCellQueryStatus}
queryStatus={cellQueryStatus} queryStatus={cellQueryStatus}
onSave={this.handleSaveEditedCell}
onCancel={this.handleDismissOverlay}
editQueryStatus={dashboardActions.editCellQueryStatus}
/> : /> :
null null
} }
@ -283,8 +242,54 @@ const DashboardPage = React.createClass({
} }
</div> </div>
) )
}, }
}) }
const {
arrayOf,
bool,
func,
number,
shape,
string,
} = PropTypes
DashboardPage.propTypes = {
source: shape({
links: shape({
proxy: string,
self: string,
}),
}).isRequired,
params: shape({
sourceID: string.isRequired,
dashboardID: string.isRequired,
}).isRequired,
location: shape({
pathname: string.isRequired,
}).isRequired,
dashboardActions: shape({
putDashboard: func.isRequired,
getDashboardsAsync: func.isRequired,
setTimeRange: func.isRequired,
addDashboardCellAsync: func.isRequired,
editDashboardCell: func.isRequired,
renameDashboardCell: func.isRequired,
}).isRequired,
dashboards: arrayOf(shape({
id: number.isRequired,
cells: arrayOf(shape({})).isRequired,
})),
handleChooseAutoRefresh: func.isRequired,
autoRefresh: number.isRequired,
timeRange: shape({}).isRequired,
inPresentationMode: bool.isRequired,
handleClickPresentationButton: func,
cellQueryStatus: shape({
queryID: string,
status: shape(),
}).isRequired,
}
const mapStateToProps = (state) => { const mapStateToProps = (state) => {
const { const {