2020-03-23 20:24:17 +00:00
|
|
|
import React, { Component, useState } from "react";
|
|
|
|
import { Button, Checkbox, Form, Icon, Header, Modal } from "semantic-ui-react";
|
2020-03-20 17:42:38 +00:00
|
|
|
|
|
|
|
const DeleteModal = (props) => (
|
2020-03-23 20:24:17 +00:00
|
|
|
<Modal trigger={<Button color="red">Remove</Button>} closeIcon>
|
|
|
|
<Header icon="archive" content="Are you sure ?" />
|
2020-03-20 17:42:38 +00:00
|
|
|
<Modal.Actions>
|
2020-03-23 20:24:17 +00:00
|
|
|
<Button color="red">
|
|
|
|
<Icon name="remove" /> No
|
2020-03-20 17:42:38 +00:00
|
|
|
</Button>
|
2020-03-23 20:24:17 +00:00
|
|
|
<Button onClick={() => props.removeDevice()} color="green">
|
|
|
|
<Icon name="checkmark" /> Yes
|
2020-03-20 17:42:38 +00:00
|
|
|
</Button>
|
|
|
|
</Modal.Actions>
|
|
|
|
</Modal>
|
2020-03-23 20:24:17 +00:00
|
|
|
);
|
2020-03-17 22:24:40 +00:00
|
|
|
|
|
|
|
const SettingsForm = (props) => {
|
2020-03-23 20:24:17 +00:00
|
|
|
const handleInputChange = (e) => {
|
|
|
|
const { name, value } = e.target;
|
|
|
|
setValues({ ...values, [name]: value });
|
2020-03-17 22:24:40 +00:00
|
|
|
};
|
|
|
|
|
2020-03-23 20:24:17 +00:00
|
|
|
const handleCheckboxChange = (e, d) => {
|
|
|
|
const { name, checked } = d;
|
|
|
|
setValues({ ...values, [name]: checked });
|
2020-03-17 22:24:40 +00:00
|
|
|
};
|
|
|
|
|
2020-03-23 20:24:17 +00:00
|
|
|
const [values, setValues] = useState({ name: "" });
|
2020-03-17 22:24:40 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Form>
|
|
|
|
<Form.Field>
|
|
|
|
<label>New Name: </label>
|
2020-03-23 20:24:17 +00:00
|
|
|
<input
|
|
|
|
autoComplete="off"
|
|
|
|
name="name"
|
|
|
|
onChange={handleInputChange}
|
|
|
|
value={values.name}
|
|
|
|
placeholder="Device name"
|
|
|
|
/>
|
2020-03-17 22:24:40 +00:00
|
|
|
</Form.Field>
|
2020-03-23 20:24:17 +00:00
|
|
|
|
2020-03-17 22:24:40 +00:00
|
|
|
{props.type === "smart-plug" ? (
|
|
|
|
<Form.Field>
|
2020-03-23 20:24:17 +00:00
|
|
|
<Checkbox
|
|
|
|
slider
|
|
|
|
name={"reset"}
|
|
|
|
onClick={handleCheckboxChange}
|
|
|
|
label="Reset Energy Consumption"
|
|
|
|
/>
|
2020-03-17 22:24:40 +00:00
|
|
|
</Form.Field>
|
2020-03-23 20:24:17 +00:00
|
|
|
) : (
|
|
|
|
""
|
|
|
|
)}
|
2020-03-20 17:42:38 +00:00
|
|
|
<Form.Field>
|
|
|
|
<DeleteModal removeDevice={() => props.removeDevice(values)} />
|
|
|
|
</Form.Field>
|
2020-03-23 20:24:17 +00:00
|
|
|
<Button
|
|
|
|
onClick={() => props.saveFunction(values)}
|
|
|
|
color="blue"
|
|
|
|
type="submit"
|
|
|
|
>
|
|
|
|
Save
|
|
|
|
</Button>
|
2020-03-17 22:24:40 +00:00
|
|
|
</Form>
|
2020-03-23 20:24:17 +00:00
|
|
|
);
|
|
|
|
};
|
2020-03-17 22:24:40 +00:00
|
|
|
|
|
|
|
export default class SettingsModal extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
open: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClose = () => {
|
2020-03-23 20:24:17 +00:00
|
|
|
this.setState({ open: false });
|
2020-03-17 22:24:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
saveSettings = (device) => {
|
|
|
|
// TODO Here there should be all the connections to save the data in the backend
|
|
|
|
console.log("SAVED: ", device);
|
2020-03-20 17:42:38 +00:00
|
|
|
if (device.name.length > 0) {
|
|
|
|
this.props.updateDevice(device);
|
|
|
|
}
|
2020-03-17 22:24:40 +00:00
|
|
|
|
|
|
|
this.props.openModal();
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const SettingsModal = () => (
|
2020-03-23 20:24:17 +00:00
|
|
|
<Modal
|
|
|
|
open={true}
|
|
|
|
onOpen={this.props.openModal}
|
|
|
|
onClose={this.props.openModal}
|
|
|
|
>
|
2020-03-17 22:24:40 +00:00
|
|
|
<Modal.Header>Settings of {this.props.device.name}</Modal.Header>
|
|
|
|
<Modal.Content>
|
2020-03-23 20:24:17 +00:00
|
|
|
<SettingsForm
|
|
|
|
type={this.props.device.type}
|
|
|
|
removeDevice={this.props.removeDevice}
|
|
|
|
saveFunction={this.saveSettings}
|
|
|
|
/>
|
2020-03-17 22:24:40 +00:00
|
|
|
</Modal.Content>
|
|
|
|
</Modal>
|
|
|
|
);
|
2020-03-23 20:24:17 +00:00
|
|
|
return <SettingsModal />;
|
2020-03-17 22:24:40 +00:00
|
|
|
}
|
|
|
|
}
|