2020-03-09 12:52:27 +00:00
|
|
|
import React, {Component} from 'react';
|
|
|
|
import {
|
2020-03-17 16:38:03 +00:00
|
|
|
Grid,
|
2020-03-09 12:52:27 +00:00
|
|
|
} from "semantic-ui-react";
|
|
|
|
import NewDevice from "./devices/NewDevice";
|
2020-03-15 14:50:25 +00:00
|
|
|
import {LightDevice, SmartPlugDevice} from "./devices/TypesOfDevices";
|
2020-03-09 12:52:27 +00:00
|
|
|
import {editButtonStyle, panelStyle} from "./devices/styleComponents";
|
2020-03-15 14:50:25 +00:00
|
|
|
import {checkMaxLength, DEVICE_NAME_MAX_LENGTH} from "./devices/constants";
|
|
|
|
import Light from "./devices/Light";
|
|
|
|
import SmartPlug from "./devices/SmartPlug";
|
|
|
|
import Sensor from "./devices/Sensor";
|
2020-03-17 16:38:03 +00:00
|
|
|
import DigitalSensor from "./devices/DigitalSensor";
|
|
|
|
import Switch from "./devices/Switch";
|
2020-03-17 22:24:40 +00:00
|
|
|
import SettingsModal from "./devices/SettingsModal";
|
2020-03-09 12:52:27 +00:00
|
|
|
|
|
|
|
|
2020-03-15 14:50:25 +00:00
|
|
|
const devices = [
|
2020-03-17 16:38:03 +00:00
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"name": "Bedroom Light",
|
|
|
|
"type": "light",
|
|
|
|
"hasIntensity": true,
|
|
|
|
"intensityLevel": 0.20,
|
|
|
|
...LightDevice
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 2,
|
|
|
|
"name": "Bathroom Light",
|
|
|
|
"type": "light",
|
|
|
|
...LightDevice
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 3,
|
|
|
|
"name": "Desktop Light",
|
|
|
|
"type": "light",
|
|
|
|
...LightDevice
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 4,
|
|
|
|
"name": "Entrance Light",
|
|
|
|
"type": "light",
|
|
|
|
...LightDevice
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 5,
|
|
|
|
"name": "Smart Plug",
|
2020-03-17 22:24:40 +00:00
|
|
|
"type": "smart-plug",
|
2020-03-17 16:38:03 +00:00
|
|
|
...SmartPlugDevice
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 6,
|
|
|
|
"name": "Bedroom Thermometer",
|
|
|
|
"type": "temperature-sensor",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 7,
|
|
|
|
"name": "Bedroom Alarm",
|
|
|
|
},
|
|
|
|
|
2020-03-15 14:50:25 +00:00
|
|
|
];
|
|
|
|
|
2020-03-17 15:20:43 +00:00
|
|
|
export default class DevicePanel extends Component {
|
2020-03-15 14:50:25 +00:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
editMode : false,
|
2020-03-17 15:20:43 +00:00
|
|
|
devices : this.props.devices,
|
2020-03-15 14:50:25 +00:00
|
|
|
};
|
|
|
|
}
|
2020-03-17 22:24:40 +00:00
|
|
|
|
2020-03-15 14:50:25 +00:00
|
|
|
editModeController = (e) => this.setState((prevState) => ({ editMode: !prevState.editMode }));
|
|
|
|
|
|
|
|
changeDeviceData = (deviceId, newSettings) => {
|
2020-03-18 11:46:28 +00:00
|
|
|
|
2020-03-15 14:50:25 +00:00
|
|
|
console.log(newSettings.name, " <-- new name --> ", deviceId );
|
|
|
|
this.state.devices.map(device => {
|
|
|
|
if(device.id === deviceId){
|
|
|
|
for(let prop in newSettings){
|
|
|
|
if(device.hasOwnProperty(prop)){
|
|
|
|
if(prop==="name"){
|
|
|
|
if(checkMaxLength(newSettings[prop])){
|
|
|
|
device[prop] = newSettings[prop];
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
alert("Name must be less than " + DEVICE_NAME_MAX_LENGTH + " characters.");
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
device[prop] = newSettings[prop];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2020-03-18 12:20:01 +00:00
|
|
|
});
|
2020-03-15 14:50:25 +00:00
|
|
|
this.forceUpdate();
|
|
|
|
};
|
2020-03-17 16:38:03 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 22:24:40 +00:00
|
|
|
editModeController = (e) => {
|
|
|
|
this.setState((prevState) => ({editMode: !prevState.editMode}));
|
|
|
|
};
|
2020-03-17 16:38:03 +00:00
|
|
|
|
|
|
|
changeDeviceData = (deviceId, newSettings) => {
|
|
|
|
console.log(newSettings.name, " <-- new name --> ", deviceId);
|
|
|
|
this.state.devices.map(device => {
|
|
|
|
if (device.id === deviceId) {
|
|
|
|
for (let prop in newSettings) {
|
|
|
|
if (device.hasOwnProperty(prop)) {
|
|
|
|
if (prop === "name") {
|
|
|
|
if (checkMaxLength(newSettings[prop])) {
|
|
|
|
device[prop] = newSettings[prop];
|
|
|
|
} else {
|
|
|
|
alert("Name must be less than " + DEVICE_NAME_MAX_LENGTH + " characters.");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
device[prop] = newSettings[prop];
|
|
|
|
}
|
2020-03-15 14:50:25 +00:00
|
|
|
|
2020-03-09 12:52:27 +00:00
|
|
|
|
2020-03-15 14:50:25 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div style={panelStyle}>
|
|
|
|
<button style={editButtonStyle} onClick={this.editModeController}>Edit</button>
|
2020-03-18 11:46:28 +00:00
|
|
|
<Grid doubling columns={this.props.devices.length > 0 ? this.props.devices.length : 1} divided="vertically">
|
2020-03-17 15:20:43 +00:00
|
|
|
{
|
|
|
|
this.props.devices ?
|
|
|
|
this.props.devices.map((e, i) => {
|
|
|
|
return (
|
2020-03-18 11:46:28 +00:00
|
|
|
<Grid.Column key={i}>
|
|
|
|
<DeviceType type={e.kind} onChangeData={this.changeDeviceData} device={e} edit={this.state.editMode}/>
|
2020-03-17 15:20:43 +00:00
|
|
|
</Grid.Column>
|
|
|
|
)
|
|
|
|
})
|
|
|
|
:
|
|
|
|
null
|
|
|
|
}
|
2020-03-15 14:50:25 +00:00
|
|
|
<Grid.Column>
|
|
|
|
<NewDevice/>
|
|
|
|
</Grid.Column>
|
|
|
|
</Grid>
|
|
|
|
</div>
|
|
|
|
)
|
2020-03-09 12:52:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 15:20:43 +00:00
|
|
|
}
|
2020-03-09 12:52:27 +00:00
|
|
|
|