import { Box, FormControlLabel, Grid, IconButton, Paper, TextField, Typography } from '@material-ui/core'; import Accordion from '@material-ui/core/Accordion'; import AccordionDetails from '@material-ui/core/AccordionDetails'; import AccordionSummary from '@material-ui/core/AccordionSummary'; import { blueGrey, green, red } from '@material-ui/core/colors'; import { CancelRounded, CheckCircleRounded, Delete, Edit } from '@material-ui/icons/'; import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; import { withStyles } from '@material-ui/styles'; import React, { Component } from 'react'; import DeleteDialog from './DeleteDialog'; const styles = theme => ({ root: { flexGrow: 1, padding: '64px', backgroundColor: theme.palette.primary.dark, }, acc_summary: { backgroundColor: blueGrey[50], padding: theme.spacing(2), textAlign: 'center', height: '75px', }, acc_details: { backgroundColor: blueGrey[100], }, grid_typo: { fontSize: theme.typography.pxToRem(20), fontWeight: theme.typography.fontWeightRegular, }, grid_typo_2: { marginLeft: '5px', fontSize: theme.typography.pxToRem(15), fontWeight: theme.typography.fontWeightRegular, color: theme.palette.text.secondary, }, typo: { fontSize: theme.typography.pxToRem(20), fontWeight: theme.typography.fontWeightRegular, }, icon_box: { marginLeft: '30px', }, paper: { backgroundColor: blueGrey[50], padding: theme.spacing(2), textAlign: 'center', height: '75px', } }); class ServiceInfoComponent extends Component { constructor(props) { super(props); this.state = { isDialogOpen: false, isEditing: false, name: "", url: "", } this.handleDialogClose = this.handleDialogClose.bind(this); this.onNameChange = this.onNameChange.bind(this); this.onUrlChange = this.onUrlChange.bind(this); this.handleSaveCancel = this.handleSaveCancel.bind(this); } componentDidMount() { this.setState({ name: this.props.info.service.name, url: this.props.info.service.uri }); if (this.props.isEditing !== undefined) { this.setState({ isEditing: this.props.isEditing }); } } onNameChange(event) { this.setState({ name: event.target.value }); } onUrlChange(event) { this.setState({ url: event.target.value }); } getColor(status) { if (status === "OK") return { color: green[600] }; else return { color: red[600] }; } handleDialogClose(result) { this.setState({ isDialogOpen: false }); if (result === true) { this.props.service.delete(this.props.info.service.id); } } handleEditCancel(value) { this.setState({ isEditing: value }); } handleSaveCancel() { let request = { ...this.props.info.service }; request.name = this.state.name; request.uri = this.state.url; if (request.id > 0) { this.props.service.put(request).catch(ex => { console.log(ex); }); } else { this.props.service.post(request).catch(ex => { console.log(ex); }); } this.setState({ isEditing: false }); } renderSaveCancel() { return ( this.setState({ isEditing: false })}> ); } renderButtons() { const renderEditDelete = () => { return ( this.handleEditCancel(true)}> this.setState({ isDialogOpen: true })}> ); } const { classes } = this.props; return ( {this.props.isAdmin && this.props.info.service.name !== "Mqtt Client Service" ? renderEditDelete() : null} ); } render() { const { classes } = this.props; const renderAccordion = () => { return ( } aria-controls={"device-panel-/" + this.props.info.service.name} id={"device-panel-/" + this.props.info.service.name}> {this.props.info.service.name} {this.props.info.service.uri} event.stopPropagation()} onFocus={(event) => event.stopPropagation()} control={this.renderButtons()} /> Status: {this.props.info.statusCode} {this.props.info.response} ); }; const renderTextFields = () => { return ( {this.renderSaveCancel()} ); }; return this.state.isEditing ? renderTextFields() : renderAccordion(); } } export default withStyles(styles)(ServiceInfoComponent);