diff --git a/ui/src/sources/components/SourceForm.js b/ui/src/sources/components/SourceForm.js index 30429799f5..28464c3486 100644 --- a/ui/src/sources/components/SourceForm.js +++ b/ui/src/sources/components/SourceForm.js @@ -11,9 +11,6 @@ const { export const SourceForm = React.createClass({ propTypes: { - addFlashMessage: func.isRequired, - addSourceAction: func, - updateSourceAction: func, source: shape({}).isRequired, editMode: bool.isRequired, onInputChange: func.isRequired, diff --git a/ui/src/sources/containers/CreateSource.js b/ui/src/sources/containers/CreateSource.js index 5d7fa96163..d7a9277127 100644 --- a/ui/src/sources/containers/CreateSource.js +++ b/ui/src/sources/containers/CreateSource.js @@ -1,28 +1,34 @@ import React, {PropTypes} from 'react' import {withRouter} from 'react-router' -import {createSource} from 'shared/apis' import {connect} from 'react-redux' -import {insecureSkipVerifyText} from 'src/shared/copy/tooltipText' +import {createSource, updateSource} from 'shared/apis' +import SourceForm from 'src/sources/components/SourceForm' import {addSource as addSourceAction} from 'src/shared/actions/sources' +const { + func, + shape, + string, +} = PropTypes + export const CreateSource = React.createClass({ propTypes: { - router: PropTypes.shape({ - push: PropTypes.func.isRequired, + router: shape({ + push: func.isRequired, }).isRequired, - location: PropTypes.shape({ - query: PropTypes.shape({ - redirectPath: PropTypes.string, + location: shape({ + query: shape({ + redirectPath: string, }).isRequired, }).isRequired, - addSourceAction: PropTypes.func, + addSourceAction: func, + updateSourceAction: func, }, getInitialState() { return { - showSSL: false, - showMeta: false, + source: {}, } }, @@ -54,62 +60,117 @@ export const CreateSource = React.createClass({ return this.props.router.push(fixedPath) }, - onInputChange() { - const showSSL = !!this.sourceURL.value.startsWith("https") - this.setState(showSSL) + handleInputChange(e) { + const val = e.target.value + 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 (this.state.editMode) { + return + } + + if (!newSource.url) { + return + } + + // if there is a type on source it has already been created + if (newSource.type) { + return + } + + createSource(newSource).then(({data: sourceFromServer}) => { + this.props.addSourceAction(sourceFromServer) + this.setState({source: sourceFromServer}) + }).catch(({data: error}) => { + // dont want to flash this until they submit + this.setState({error: error.message}) + }) + }, + + handleSubmit(newSource) { + const {error} = this.state + + if (error) { + // useful error message + // return addFlashMessage({type: 'error', text: error}) + } + + updateSource(newSource).then(({data: sourceFromServer}) => { + this.props.updateSourceAction(sourceFromServer) + this.redirectToApp(newSource) + }).catch(() => { + // give a useful error message to the user + }) + }, + + //