move error handling to thunk

pull/10616/head
Jade McGough 2017-05-25 16:09:41 -07:00
parent f64f7cb785
commit ae0138ac38
3 changed files with 34 additions and 31 deletions

View File

@ -1,8 +1,10 @@
import uuid from 'node-uuid' import uuid from 'node-uuid'
import {getQueryConfig} from 'shared/apis' import {getQueryConfig} from 'shared/apis'
import {writeData} from 'src/data_explorer/apis'
import {errorThrown} from 'shared/actions/errors' import {errorThrown} from 'shared/actions/errors'
import {publishAutoDismissingNotification} from 'shared/dispatchers'
export function addQuery(options = {}) { export function addQuery(options = {}) {
return { return {
@ -160,3 +162,18 @@ export const editRawTextAsync = (url, id, text) => async dispatch => {
dispatch(errorThrown(error)) dispatch(errorThrown(error))
} }
} }
export const writeDataAsync = (source, db, data) => async dispatch => {
try {
await writeData(source, db, data)
dispatch(
publishAutoDismissingNotification(
'success',
'Data was written successfully'
)
)
} catch (response) {
dispatch(errorThrown(response, response.data.error))
throw response
}
}

View File

@ -1,14 +1,8 @@
import React, {Component, PropTypes} from 'react' import React, {Component, PropTypes} from 'react'
import {connect} from 'react-redux'
import {bindActionCreators} from 'redux'
import DatabaseDropdown from 'shared/components/DatabaseDropdown' import DatabaseDropdown from 'shared/components/DatabaseDropdown'
import OnClickOutside from 'shared/components/OnClickOutside' import OnClickOutside from 'shared/components/OnClickOutside'
import {writeData} from 'src/data_explorer/apis'
import {publishAutoDismissingNotification} from 'shared/dispatchers'
import {errorThrown as errorThrownAction} from 'shared/actions/errors'
class WriteDataForm extends Component { class WriteDataForm extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
@ -17,7 +11,6 @@ class WriteDataForm extends Component {
} }
this.handleSelectDatabase = ::this.handleSelectDatabase this.handleSelectDatabase = ::this.handleSelectDatabase
this.handleError = ::this.handleError
this.handleWrite = ::this.handleWrite this.handleWrite = ::this.handleWrite
this.handleClickOutside = ::this.handleClickOutside this.handleClickOutside = ::this.handleClickOutside
} }
@ -26,30 +19,19 @@ class WriteDataForm extends Component {
this.setState({selectedDatabase: item.text}) this.setState({selectedDatabase: item.text})
} }
handleError(error) {
const {errorThrown} = this.props
errorThrown(error)
}
handleClickOutside() { handleClickOutside() {
const {onClose} = this.props const {onClose} = this.props
onClose() onClose()
} }
async handleWrite() { handleWrite() {
const {onClose, source, notify, errorThrown} = this.props const {onClose, source, writeData} = this.props
const {selectedDatabase} = this.state const {selectedDatabase} = this.state
try { writeData(source, selectedDatabase, this.editor.value).then(() => onClose())
await writeData(source, selectedDatabase, this.editor.value)
notify('success', 'Data was written successfully')
onClose()
} catch (response) {
errorThrown(response, response.data.error)
}
} }
render() { render() {
const {onClose} = this.props const {onClose, errorThrown} = this.props
const {selectedDatabase} = this.state const {selectedDatabase} = this.state
return ( return (
@ -60,7 +42,7 @@ class WriteDataForm extends Component {
<DatabaseDropdown <DatabaseDropdown
onSelectDatabase={this.handleSelectDatabase} onSelectDatabase={this.handleSelectDatabase}
database={selectedDatabase} database={selectedDatabase}
onErrorThrown={this.handleError} onErrorThrown={errorThrown}
/> />
</div> </div>
<div className="page-header__right"> <div className="page-header__right">
@ -105,14 +87,9 @@ WriteDataForm.propTypes = {
queries: string.isRequired, queries: string.isRequired,
}).isRequired, }).isRequired,
}).isRequired, }).isRequired,
notify: func.isRequired,
errorThrown: func.isRequired,
onClose: func.isRequired, onClose: func.isRequired,
writeData: func.isRequired,
errorThrown: func.isRequired,
} }
const mapDispatchToProps = dispatch => ({ export default OnClickOutside(WriteDataForm)
notify: bindActionCreators(publishAutoDismissingNotification, dispatch),
errorThrown: bindActionCreators(errorThrownAction, dispatch),
})
export default connect(null, mapDispatchToProps)(OnClickOutside(WriteDataForm))

View File

@ -13,6 +13,7 @@ import OverlayTechnologies from 'shared/components/OverlayTechnologies'
import {VIS_VIEWS} from 'shared/constants' import {VIS_VIEWS} from 'shared/constants'
import {MINIMUM_HEIGHTS, INITIAL_HEIGHTS} from '../constants' import {MINIMUM_HEIGHTS, INITIAL_HEIGHTS} from '../constants'
import {errorThrown} from 'shared/actions/errors'
import {setAutoRefresh} from 'shared/actions/app' import {setAutoRefresh} from 'shared/actions/app'
import * as viewActions from 'src/data_explorer/actions/view' import * as viewActions from 'src/data_explorer/actions/view'
@ -42,6 +43,8 @@ const DataExplorer = React.createClass({
showWriteForm: bool.isRequired, showWriteForm: bool.isRequired,
queryIDs: arrayOf(string).isRequired, queryIDs: arrayOf(string).isRequired,
}).isRequired, }).isRequired,
writeData: func.isRequired,
errorThrownAction: func.isRequired,
}, },
childContextTypes: { childContextTypes: {
@ -77,12 +80,14 @@ const DataExplorer = React.createClass({
render() { render() {
const { const {
autoRefresh, autoRefresh,
errorThrownAction,
handleChooseAutoRefresh, handleChooseAutoRefresh,
timeRange, timeRange,
setTimeRange, setTimeRange,
queryConfigs, queryConfigs,
queryConfigActions, queryConfigActions,
source, source,
writeData,
} = this.props } = this.props
const {activeQueryIndex, showWriteForm} = this.state const {activeQueryIndex, showWriteForm} = this.state
@ -91,8 +96,10 @@ const DataExplorer = React.createClass({
{showWriteForm {showWriteForm
? <OverlayTechnologies> ? <OverlayTechnologies>
<WriteDataForm <WriteDataForm
errorThrown={errorThrownAction}
onClose={() => this.setState({showWriteForm: false})} onClose={() => this.setState({showWriteForm: false})}
source={source} source={source}
writeData={writeData}
/> />
</OverlayTechnologies> </OverlayTechnologies>
: null} : null}
@ -155,7 +162,9 @@ function mapStateToProps(state) {
function mapDispatchToProps(dispatch) { function mapDispatchToProps(dispatch) {
return { return {
handleChooseAutoRefresh: bindActionCreators(setAutoRefresh, dispatch), handleChooseAutoRefresh: bindActionCreators(setAutoRefresh, dispatch),
errorThrownAction: bindActionCreators(errorThrown, dispatch),
setTimeRange: bindActionCreators(viewActions.setTimeRange, dispatch), setTimeRange: bindActionCreators(viewActions.setTimeRange, dispatch),
writeData: bindActionCreators(viewActions.writeDataAsync, dispatch),
queryConfigActions: bindActionCreators(viewActions, dispatch), queryConfigActions: bindActionCreators(viewActions, dispatch),
} }
} }