From 5123207ea1db87ef9ed3dd60a84d9034110a956f Mon Sep 17 00:00:00 2001 From: Thomas Nordquist Date: Mon, 7 Jan 2019 13:54:13 +0100 Subject: [PATCH] Add error notification --- .../components/ConnectionSetup/Connection.tsx | 12 ++-- .../ConnectionSetup/Notification.tsx | 57 +++++++++++++++ app/src/components/Copy.tsx | 69 +++++++++++++++++++ 3 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 app/src/components/ConnectionSetup/Notification.tsx create mode 100644 app/src/components/Copy.tsx diff --git a/app/src/components/ConnectionSetup/Connection.tsx b/app/src/components/ConnectionSetup/Connection.tsx index e8cd91e..0554471 100644 --- a/app/src/components/ConnectionSetup/Connection.tsx +++ b/app/src/components/ConnectionSetup/Connection.tsx @@ -1,6 +1,7 @@ import * as React from 'react' import { Typography, Toolbar, Modal, MenuItem, Button, Grid, Paper, TextField, Switch, FormControlLabel } from '@material-ui/core' import { withStyles, Theme, StyleRulesCallback } from '@material-ui/core/styles' +import Notification from './Notification' import { MqttOptions, DataSourceState } from '../../../../backend/src/DataSource' import { addMqttConnectionEvent, makeConnectionStateEvent, rendererEvents } from '../../../../events' import sha1 = require('sha1') @@ -18,6 +19,7 @@ const protocols = [ ] interface State { + error?: Error visible: boolean host: string protocol: string @@ -79,10 +81,11 @@ class Connection extends React.Component { const connectionId = (sha1(Math.random() + JSON.stringify(options)).slice(0, 8)) as string rendererEvents.emit(addMqttConnectionEvent, { options, id: connectionId }) rendererEvents.subscribe(makeConnectionStateEvent(connectionId), (state: DataSourceState) => { - console.log(state) if (state.connected) { this.props.onConnection(connectionId) this.setState({ visible: false }) + } else if (state.error) { + this.setState({ error: state.error }) } }) } @@ -132,7 +135,6 @@ class Connection extends React.Component { MQTT Connection
- {
- + // diff --git a/app/src/components/ConnectionSetup/Notification.tsx b/app/src/components/ConnectionSetup/Notification.tsx new file mode 100644 index 0000000..44b9a55 --- /dev/null +++ b/app/src/components/ConnectionSetup/Notification.tsx @@ -0,0 +1,57 @@ +import * as React from 'react' +import { Snackbar, SnackbarContent } from '@material-ui/core' +import { red, green } from '@material-ui/core/colors' +import { withStyles, Theme } from '@material-ui/core/styles' + +enum MessageType { + success = 'success', + error = 'error', +} + +interface Props { + message: string + type: MessageType + onClose: () => void + classes: any +} + +interface State { + snackBarOpen: boolean +} + +class Notification extends React.Component { + constructor(props: Props) { + super(props) + this.state = { snackBarOpen: false } + } + + public static styles = (theme: Theme) => ({ + success: { + backgroundColor: green[600], + color: theme.typography.button.color, + }, + error: { + backgroundColor: red[600], + color: theme.typography.button.color, + }, + }) + + public render() { + return { this.setState({ snackBarOpen: false }) }} + > + + + } +} + +export default withStyles(Notification.styles)(Notification) diff --git a/app/src/components/Copy.tsx b/app/src/components/Copy.tsx new file mode 100644 index 0000000..024261e --- /dev/null +++ b/app/src/components/Copy.tsx @@ -0,0 +1,69 @@ +import * as React from 'react' +import { Snackbar, SnackbarContent } from '@material-ui/core' +import FileCopy from '@material-ui/icons/FileCopy' +import Check from '@material-ui/icons/Check' +import green from '@material-ui/core/colors/green' +import { withStyles, Theme } from '@material-ui/core/styles' +const copy = require('copy-text-to-clipboard') + +interface Props { + value: string + classes: any +} + +interface State { + didCopy: boolean + snackBarOpen: boolean +} + +class Copy extends React.Component { + constructor(props: Props) { + super(props) + this.state = { didCopy: false, snackBarOpen: false } + } + + public static styles = (theme: Theme) => ({ + snackbar: { + backgroundColor: green[600], + color: theme.typography.button.color, + }, + }) + + public render() { + const icon = !this.state.didCopy + ? + : + + return + {icon} + + { this.setState({ snackBarOpen: false }) }} + > + + + + + } + + private handleClick = (event: React.MouseEvent) => { + event.stopPropagation() + + copy(this.props.value) + this.setState({ didCopy: true, snackBarOpen: true }) + setTimeout(() => { + this.setState({ didCopy: false }) + }, 1500) + } +} + +export default withStyles(Copy.styles)(Copy)