import ConnectButton from './ConnectButton' import Delete from '@material-ui/icons/Delete' import React, { useCallback, useState } from 'react' import Save from '@material-ui/icons/Save' import Settings from '@material-ui/icons/Settings' import Visibility from '@material-ui/icons/Visibility' import VisibilityOff from '@material-ui/icons/VisibilityOff' import { AppState } from '../../reducers' import { bindActionCreators } from 'redux' import { connect } from 'react-redux' import { connectionActions, connectionManagerActions } from '../../actions' import { ConnectionOptions, toMqttConnection } from '../../model/ConnectionOptions' import { KeyCodes } from '../../utils/KeyCodes' import { Theme, withStyles } from '@material-ui/core/styles' import { ToggleSwitch } from './ToggleSwitch' import { useGlobalKeyEventHandler } from '../../effects/useGlobalKeyEventHandler' import { Button, FormControl, Grid, IconButton, Input, InputAdornment, InputLabel, MenuItem, TextField, } from '@material-ui/core' interface Props { connection: ConnectionOptions classes: { [s: string]: string } actions: typeof connectionActions managerActions: typeof connectionManagerActions connected: boolean connecting: boolean } const protocols = ['mqtt', 'ws'] function ConnectionSettings(props: Props) { const [showPassword, setShowPassword] = useState(false) const toggleConnect = useCallback(() => { if (props.connecting) { props.actions.disconnect() return } if (!props.connection) { return } const mqttOptions = toMqttConnection(props.connection) if (mqttOptions) { props.actions.connect(mqttOptions, props.connection.id) } }, [props.connection, props.connecting]) useGlobalKeyEventHandler(KeyCodes.escape, props.actions.disconnect) useGlobalKeyEventHandler(KeyCodes.enter, toggleConnect, [props.connecting]) const handleClickShowPassword = useCallback(() => { setShowPassword(!showPassword) }, [showPassword]) function requiresBasePath() { return props.connection.protocol !== 'mqtt' } function renderBasePathInput() { return ( ) } const handleChange = (name: string) => (event: any) => { if (!props.connection) { return } updateConnection(name, event.target.value) } const updateConnection = (name: string, value: any) => { props.managerActions.updateConnection(props.connection.id, { [name]: value, }) } const renderProtocols = () => { const { classes, connection } = props const protocolItems = protocols.map((value: string) => ( {value}:// )) return ( {protocolItems} ) } const updateProtocol = (event: React.ChangeEvent) => { const value = event.target.value updateConnection('protocol', value) if (event.target.value === 'mqtt') { updateConnection('basePath', undefined) } else { updateConnection('basePath', 'ws') } } const toggleCertValidation = () => { props.managerActions.updateConnection(props.connection.id, { certValidation: !props.connection.certValidation, }) } const toggleTls = () => { props.managerActions.updateConnection(props.connection.id, { encryption: !props.connection.encryption, }) } function PasswordVisibilityButton(props: { showPassword: boolean; toggle: () => void }) { return ( {props.showPassword ? : } ) } const { classes, connection } = props return (
{renderProtocols()} {requiresBasePath() ? renderBasePathInput() : null} Password } />
) } const mapStateToProps = (state: AppState) => { return { connected: state.connection.connected, connecting: state.connection.connecting, } } const mapDispatchToProps = (dispatch: any) => { return { actions: bindActionCreators(connectionActions, dispatch), managerActions: bindActionCreators(connectionManagerActions, dispatch), } } const styles = (theme: Theme) => ({ textField: { width: '100%', }, switch: { marginTop: 0, }, button: { margin: theme.spacing(1), }, inputFormControl: { marginTop: '16px', }, }) export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(ConnectionSettings))