Cleanup errors

pull/2910/head
Andrew Watkins 2018-03-14 14:49:01 -07:00
parent cac7180ce3
commit 466a6e47c6
7 changed files with 70 additions and 33 deletions

View File

@ -4,21 +4,40 @@ import AlertTabs from 'src/kapacitor/components/AlertTabs'
import {Kapacitor, Source} from 'src/types'
export interface Notification {
id?: string
type: string
icon: string
duration: number
message: string
}
type NotificationFunc = () => Notification
interface AlertOutputProps {
exists: boolean
kapacitor: Kapacitor
source: Source
hash: string
publishNotification: (message: Notification | NotificationFunc) => void
}
const AlertOutputs: SFC<AlertOutputProps> = ({
exists,
kapacitor,
source,
hash,
exists,
source,
kapacitor,
publishNotification,
}) => {
if (exists) {
return <AlertTabs source={source} kapacitor={kapacitor} hash={hash} />
return (
<AlertTabs
hash={hash}
source={source}
kapacitor={kapacitor}
publishNotification={publishNotification}
/>
)
}
return (

View File

@ -1,10 +1,7 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import {bindActionCreators} from 'redux'
import _ from 'lodash'
import {publishNotification as publishNotificationAction} from 'shared/actions/notifications'
import {Tab, Tabs, TabPanel, TabPanels, TabList} from 'shared/components/Tabs'
import {
@ -331,8 +328,4 @@ AlertTabs.propTypes = {
hash: string.isRequired,
}
const mapDispatchToProps = dispatch => ({
publishNotification: bindActionCreators(publishNotificationAction, dispatch),
})
export default connect(null, mapDispatchToProps)(AlertTabs)
export default AlertTabs

View File

@ -6,6 +6,16 @@ import Input from 'src/kapacitor/components/KapacitorFormInput'
import {Kapacitor, Source} from 'src/types'
export interface Notification {
id?: string
type: string
icon: string
duration: number
message: string
}
type NotificationFunc = () => Notification
interface Props {
kapacitor: Kapacitor
exists: boolean
@ -15,6 +25,7 @@ interface Props {
onChangeUrl: (e: ChangeEvent<HTMLInputElement>) => void
source: Source
hash: string
publishNotification: (message: Notification | NotificationFunc) => void
}
const KapacitorForm: SFC<Props> = ({
@ -27,6 +38,7 @@ const KapacitorForm: SFC<Props> = ({
onInputChange,
source,
hash,
publishNotification,
}) =>
<div className="page">
<div className="page-header">
@ -102,14 +114,13 @@ const KapacitorForm: SFC<Props> = ({
</div>
</div>
<div className="col-md-9">
{
<AlertOutputs
exists={exists}
kapacitor={kapacitor}
source={source}
hash={hash}
/>
}
<AlertOutputs
hash={hash}
exists={exists}
source={source}
kapacitor={kapacitor}
publishNotification={publishNotification}
/>
</div>
</div>
</div>

View File

@ -28,7 +28,7 @@ import {
export const defaultName = 'My Kapacitor'
export const kapacitorPort = '9092'
type Notification = {
export interface Notification {
id?: string
type: string
icon: string
@ -36,7 +36,7 @@ type Notification = {
message: string
}
type NotificationFunc = () => Notification
export type NotificationFunc = () => Notification
interface Kapacitor {
url: string
@ -93,8 +93,8 @@ export class KapacitorPage extends PureComponent<Props, State> {
const kapacitor = await getKapacitor(source, id)
this.setState({kapacitor})
await this.checkKapacitorConnection(kapacitor)
} catch (err) {
console.error('Could not get kapacitor: ', err)
} catch (error) {
console.error('Could not get kapacitor: ', error)
publishNotification(NOTIFY_KAPACITOR_CONNECTION_FAILED)
}
}
@ -178,6 +178,7 @@ export class KapacitorPage extends PureComponent<Props, State> {
await pingKapacitor(kapacitor)
this.setState({exists: true})
} catch (error) {
console.error(error)
this.setState({exists: false})
this.props.publishNotification(NOTIFY_KAPACITOR_CONNECTION_FAILED)
}
@ -191,7 +192,7 @@ export class KapacitorPage extends PureComponent<Props, State> {
}
render() {
const {source, location, params} = this.props
const {source, location, params, publishNotification} = this.props
const hash = (location && location.hash) || (params && params.hash) || ''
const {kapacitor, exists} = this.state
@ -205,6 +206,7 @@ export class KapacitorPage extends PureComponent<Props, State> {
onChangeUrl={this.handleChangeUrl}
onReset={this.handleResetToDefaults}
onInputChange={this.handleInputChange}
publishNotification={publishNotification}
/>
)
}

View File

@ -36,16 +36,22 @@ export function deleteSource(source) {
})
}
export function pingKapacitor(kapacitor) {
return AJAX({
method: 'GET',
url: kapacitor.links.ping,
})
export const pingKapacitor = async kapacitor => {
try {
const data = await AJAX({
method: 'GET',
url: kapacitor.links.ping,
})
return data
} catch (error) {
console.error(error)
throw error
}
}
export const getKapacitor = async (source, kapacitorID) => {
try {
const {data} = AJAX({
const {data} = await AJAX({
url: `${source.links.kapacitors}/${kapacitorID}`,
method: 'GET',
})
@ -134,7 +140,12 @@ export function updateKapacitor({
}
export const getKapacitorConfig = async kapacitor => {
return await kapacitorProxy(kapacitor, 'GET', '/kapacitor/v1/config', '')
try {
return await kapacitorProxy(kapacitor, 'GET', '/kapacitor/v1/config', '')
} catch (error) {
console.error(error)
throw error
}
}
export const getKapacitorConfigSection = (kapacitor, section) => {

View File

@ -456,7 +456,7 @@ export const NOTIFY_ALERT_ENDPOINT_SAVED = endpoint => ({
})
export const NOTIFY_ALERT_ENDPOINT_SAVE_FAILED = (endpoint, errorMessage) => ({
...defaultSuccessNotification,
...defaultErrorNotification,
message: `There was an error saving the alert configuration for ${endpoint}: ${errorMessage}`,
})

View File

@ -199,6 +199,7 @@ describe('Kapacitor.Containers.KapacitorPage', () => {
await wrapper.instance().componentDidMount()
expect(wrapper.state().kapacitor).toEqual(mocks.kapacitor)
expect(wrapper.state().exists).toBe(true)
})
})
})