Update SourcePage to use new SourceForm format

pull/2122/head
Andrew Watkins 2017-10-16 14:07:56 -07:00
parent 5215f21782
commit 0dfc71195f
2 changed files with 91 additions and 59 deletions

View File

@ -84,6 +84,7 @@ class CreateSource extends Component {
this.setState({source: sourceFromServer, isCreated: true}) this.setState({source: sourceFromServer, isCreated: true})
}) })
.catch(({data: error}) => { .catch(({data: error}) => {
// dont want to flash this until they submit
this.setState({error: error.message}) this.setState({error: error.message})
}) })
} }

View File

@ -1,4 +1,4 @@
import React, {PropTypes} from 'react' import React, {PropTypes, Component} from 'react'
import {withRouter} from 'react-router' import {withRouter} from 'react-router'
import {getSource} from 'shared/apis' import {getSource} from 'shared/apis'
import {createSource, updateSource} from 'shared/apis' import {createSource, updateSource} from 'shared/apis'
@ -6,62 +6,62 @@ import {
addSource as addSourceAction, addSource as addSourceAction,
updateSource as updateSourceAction, updateSource as updateSourceAction,
} from 'shared/actions/sources' } from 'shared/actions/sources'
import {publishNotification} from 'shared/actions/notifications'
import {connect} from 'react-redux' import {connect} from 'react-redux'
import SourceForm from 'src/sources/components/SourceForm' import SourceForm from 'src/sources/components/SourceForm'
import FancyScrollbar from 'shared/components/FancyScrollbar' import FancyScrollbar from 'shared/components/FancyScrollbar'
import SourceIndicator from 'shared/components/SourceIndicator' import SourceIndicator from 'shared/components/SourceIndicator'
const {func, shape, string} = PropTypes class SourcePage extends Component {
constructor(props) {
super(props)
export const SourcePage = React.createClass({ this.state = {
propTypes: { source: {
params: shape({ url: 'http://localhost:8086',
id: string, name: 'Influx 1',
sourceID: string, username: '',
}), password: '',
router: shape({ default: true,
push: func.isRequired, telegraf: 'telegraf',
}).isRequired, insecureSkipVerify: false,
location: shape({ metaUrl: '',
query: shape({
redirectPath: string,
}).isRequired,
}).isRequired,
addFlashMessage: func.isRequired,
addSourceAction: func,
updateSourceAction: func,
}, },
getInitialState() {
return {
source: {},
editMode: this.props.params.id !== undefined, editMode: this.props.params.id !== undefined,
error: '', error: '',
} }
}, }
componentDidMount() { componentDidMount() {
if (!this.state.editMode) { if (!this.state.editMode) {
return return
} }
getSource(this.props.params.id).then(({data: source}) => { getSource(this.props.params.id).then(({data: source}) => {
this.setState({source}) this.setState({source})
}) })
}, }
handleInputChange(e) { handleInputChange = e => {
const val = e.target.value let val = e.target.value
const name = e.target.name const name = e.target.name
this.setState(prevState => {
const newSource = Object.assign({}, prevState.source, {
[name]: val,
})
return Object.assign({}, prevState, {source: newSource})
})
},
handleBlurSourceURL(newSource) { if (e.target.type === 'checkbox') {
val = e.target.checked
}
this.setState(prevState => {
const source = {
...prevState.source,
[name]: val,
}
return {...prevState, source}
})
}
handleBlurSourceURL = newSource => {
if (this.state.editMode) { if (this.state.editMode) {
return return
} }
@ -78,35 +78,46 @@ export const SourcePage = React.createClass({
createSource(newSource) createSource(newSource)
.then(({data: sourceFromServer}) => { .then(({data: sourceFromServer}) => {
this.props.addSourceAction(sourceFromServer) this.props.addSourceAction(sourceFromServer)
this.setState({source: sourceFromServer, error: null}) this.setState({source: sourceFromServer, isCreated: true})
}) })
.catch(({data: error}) => { .catch(({data: error}) => {
// dont want to flash this until they submit // dont want to flash this until they submit
this.setState({error: error.message}) this.setState({error: error.message})
}) })
},
handleSubmit(newSource) {
const {router, params, addFlashMessage} = this.props
const {error} = this.state
if (error) {
return addFlashMessage({type: 'error', text: error})
} }
updateSource(newSource) handleSubmit = e => {
e.preventDefault()
const {router, params, notify} = this.props
const {isCreated, source, editMode} = this.state
const isNewSource = !editMode
if (!isCreated && isNewSource) {
return createSource(source)
.then(({data: sourceFromServer}) => {
this.props.addSourceAction(sourceFromServer)
router.push(`/sources/${params.sourceID}/manage-sources`)
})
.catch(({data: error}) => {
console.error('Error on source creation: ', error.message)
notify(
'error',
`There was a problem creating source: ${error.message}`
)
})
}
updateSource(source)
.then(({data: sourceFromServer}) => { .then(({data: sourceFromServer}) => {
this.props.updateSourceAction(sourceFromServer) this.props.updateSourceAction(sourceFromServer)
router.push(`/sources/${params.sourceID}/manage-sources`) router.push(`/sources/${params.sourceID}/manage-sources`)
addFlashMessage({type: 'success', text: 'The source info saved'}) notify('success', 'The source info saved')
}) })
.catch(() => { .catch(({data: error}) => {
addFlashMessage({ console.error('Error on source update', error.message)
type: 'error', notify('error', `There was a problem: ${error.message}`)
text: 'There was a problem updating the source. Check the settings',
}) })
}) }
},
render() { render() {
const {source, editMode} = this.state const {source, editMode} = this.state
@ -148,13 +159,33 @@ export const SourcePage = React.createClass({
</FancyScrollbar> </FancyScrollbar>
</div> </div>
) )
}, }
})
function mapStateToProps(_) {
return {}
} }
export default connect(mapStateToProps, {addSourceAction, updateSourceAction})( const {func, shape, string} = PropTypes
withRouter(SourcePage)
) SourcePage.propTypes = {
params: shape({
id: string,
sourceID: string,
}),
router: shape({
push: func.isRequired,
}).isRequired,
location: shape({
query: shape({
redirectPath: string,
}).isRequired,
}).isRequired,
notify: func,
addSourceAction: func,
updateSourceAction: func,
}
const mapStateToProps = () => ({})
export default connect(mapStateToProps, {
notify: publishNotification,
addSourceAction,
updateSourceAction,
})(withRouter(SourcePage))