frontend/smart-hut/src/components/dashboard/devices/SettingsModal.js

112 lines
2.7 KiB
JavaScript
Raw Normal View History

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
);
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-23 20:24:17 +00:00
const handleCheckboxChange = (e, d) => {
const { name, checked } = d;
setValues({ ...values, [name]: checked });
};
2020-03-23 20:24:17 +00:00
const [values, setValues] = useState({ name: "" });
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"
/>
</Form.Field>
2020-03-23 20:24:17 +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"
/>
</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>
</Form>
2020-03-23 20:24:17 +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 });
};
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);
}
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}
>
<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}
/>
</Modal.Content>
</Modal>
);
2020-03-23 20:24:17 +00:00
return <SettingsModal />;
}
}