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

165 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-05-12 13:18:33 +00:00
import React, { Component, useState } from 'react';
import {
Button, Form, Icon, Header, Modal, Input,
} from 'semantic-ui-react';
import { connect } from 'react-redux';
import { RemoteService } from '../../../remote';
2020-03-20 17:42:38 +00:00
const DeleteModal = (props) => (
2020-04-17 12:25:22 +00:00
<Modal
2020-05-12 13:18:33 +00:00
trigger={(
2020-04-17 12:25:22 +00:00
<Button icon labelPosition="left" inverted color="red">
<Icon name="trash alternate" />
Delete device
</Button>
2020-05-12 13:18:33 +00:00
)}
2020-04-17 12:25:22 +00:00
closeIcon
>
2020-03-23 20:24:17 +00:00
<Header icon="archive" content="Are you sure ?" />
2020-03-20 17:42:38 +00:00
<Modal.Actions>
2020-04-17 12:15:22 +00:00
<Button color="red">
2020-05-12 13:18:33 +00:00
<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">
2020-05-12 13:18:33 +00:00
<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) => {
2020-05-01 09:18:25 +00:00
const { name, value } = e.target;
2020-03-23 20:24:17 +00:00
setValues({ ...values, [name]: value });
};
2020-05-12 13:18:33 +00:00
const [values, setValues] = useState({ name: '' });
2020-05-01 09:18:25 +00:00
return (
<Form>
<Form.Field>
2020-05-01 09:18:25 +00:00
<label>Edit Name: </label>
<Input
2020-03-23 20:24:17 +00:00
autoComplete="off"
name="name"
onChange={handleInputChange}
2020-05-01 09:18:25 +00:00
placeholder={props.name}
2020-03-23 20:24:17 +00:00
/>
</Form.Field>
2020-03-20 17:42:38 +00:00
<Form.Field>
<DeleteModal removeDevice={() => props.removeDevice(values)} />
</Form.Field>
2020-04-17 12:15:22 +00:00
<Button
onClick={() => props.saveFunction(values)}
2020-04-17 12:25:22 +00:00
color="green"
2020-04-17 12:15:22 +00:00
type="submit"
>
2020-04-17 12:25:22 +00:00
<Icon name="checkmark" />
Save changes
2020-04-17 12:15:22 +00:00
</Button>
</Form>
2020-03-23 20:24:17 +00:00
);
};
2020-04-10 15:25:52 +00:00
class DeviceSettingsModal extends Component {
constructor(props) {
super(props);
this.state = {
2020-04-10 15:25:52 +00:00
open: false,
2020-04-27 13:47:59 +00:00
name: this.props.device.name,
};
2020-04-10 15:25:52 +00:00
this.updateDevice = this.updateDevice.bind(this);
this.deleteDevice = this.deleteDevice.bind(this);
2020-05-12 13:18:33 +00:00
// this.useExternalTempSensor = this.useExternalTempSensor.bind(this);
}
closeModal = (e) => {
this.setState({ openModal: false });
};
openModal = (e) => {
this.setState({ openModal: true });
};
2020-04-10 15:25:52 +00:00
updateDevice(values) {
console.log(values, this.external);
2020-05-12 13:18:33 +00:00
let { name } = values;
if (values.name.length === 0) {
name = this.props.device.name;
}
2020-05-12 13:18:33 +00:00
const data = {
...this.props.device,
2020-05-12 13:18:33 +00:00
name,
};
2020-05-12 13:18:33 +00:00
if (this.props.device.kind === 'thermostat') {
const external = values.external
? values.external
: this.props.device.useExternalSensors;
console.log(external);
data.useExternalSensors = external;
}
console.log(data.useExternalSensors);
2020-04-10 15:25:52 +00:00
this.props
.saveDevice(data)
2020-04-21 14:18:05 +00:00
.then(() => this.setState({ openModal: false }))
2020-05-12 13:18:33 +00:00
.catch((err) => console.error(
2020-04-10 15:25:52 +00:00
`settings modal for device ${this.props.id} deletion error`,
2020-05-12 13:18:33 +00:00
err,
));
2020-04-10 15:25:52 +00:00
}
2020-04-10 15:25:52 +00:00
deleteDevice() {
this.props
.deleteDevice(this.props.device)
2020-05-02 15:02:50 +00:00
.then(() => this.setState({ openModal: false }))
2020-05-12 13:18:33 +00:00
.catch((err) => console.error(
2020-04-10 15:25:52 +00:00
`settings modal for device ${this.props.id} deletion error`,
2020-05-12 13:18:33 +00:00
err,
));
2020-04-10 15:25:52 +00:00
}
2020-05-01 09:18:25 +00:00
_editForm = null;
2020-05-12 13:18:33 +00:00
2020-05-01 09:18:25 +00:00
get editForm() {
this._editForm = this._editForm || (
<SettingsForm
name={this.state.name}
removeDevice={this.deleteDevice}
saveFunction={this.updateDevice}
tempSensor={this.useExternalTempSensor}
2020-05-01 09:18:25 +00:00
/>
);
return this._editForm;
}
render() {
2020-05-01 09:18:25 +00:00
return (
<Modal closeIcon onClose={this.closeModal} open={this.state.openModal}>
2020-05-12 13:18:33 +00:00
<Modal.Header>
Settings of
{this.props.device.name}
</Modal.Header>
2020-05-01 09:18:25 +00:00
<Modal.Content>{this.editForm}</Modal.Content>
</Modal>
);
}
}
2020-04-10 15:25:52 +00:00
const mapStateToProps = (state, ownProps) => ({
device: state.devices[ownProps.id],
});
const DeviceSettingsModalContainer = connect(
mapStateToProps,
RemoteService,
null,
2020-05-12 13:18:33 +00:00
{ forwardRef: true },
2020-04-10 15:25:52 +00:00
)(DeviceSettingsModal);
export default DeviceSettingsModalContainer;