frontend/smart-hut/src/components/dashboard/DevicePanel.js

76 lines
2.1 KiB
JavaScript
Raw Normal View History

import React, {Component} from 'react';
import {
2020-03-17 16:38:03 +00:00
Grid,
} from "semantic-ui-react";
import {editButtonStyle, panelStyle} from "./devices/styleComponents";
import {checkMaxLength, DEVICE_NAME_MAX_LENGTH} from "./devices/constants";
2020-03-18 14:30:02 +00:00
import DeviceType from './devices/DeviceTypeController';
import NewDevice from "./devices/NewDevice";
2020-03-17 15:20:43 +00:00
export default class DevicePanel extends Component {
2020-03-17 16:38:03 +00:00
constructor(props) {
super(props);
this.state = {
2020-03-18 14:30:02 +00:00
editMode : false,
devices : this.props.devices,
};
2020-03-17 16:38:03 +00:00
}
2020-03-18 14:30:02 +00:00
editModeController = (e) => this.setState((prevState) => ({ editMode: !prevState.editMode }));
2020-03-17 16:38:03 +00:00
changeDeviceData = (deviceId, newSettings) => {
2020-03-18 14:30:02 +00:00
console.log(newSettings.name, " <-- new name --> ", deviceId );
2020-03-17 16:38:03 +00:00
this.state.devices.map(device => {
2020-03-18 14:30:02 +00:00
if(device.id === deviceId){
for(let prop in newSettings){
if(device.hasOwnProperty(prop)){
if(prop==="name"){
if(checkMaxLength(newSettings[prop])){
2020-03-17 16:38:03 +00:00
device[prop] = newSettings[prop];
2020-03-18 14:30:02 +00:00
}
else{
2020-03-17 16:38:03 +00:00
alert("Name must be less than " + DEVICE_NAME_MAX_LENGTH + " characters.");
}
2020-03-18 14:30:02 +00:00
}else{
2020-03-17 16:38:03 +00:00
device[prop] = newSettings[prop];
}
2020-03-17 16:38:03 +00:00
}
}
}
return null;
});
this.forceUpdate();
};
render() {
return (
<div style={panelStyle}>
<button style={editButtonStyle} onClick={this.editModeController}>Edit</button>
2020-03-18 14:30:02 +00:00
<Grid doubling columns={this.state.devices.length > 0 ? this.state.devices.length : 1} divided="vertically">
{
this.state.devices ?
this.state.devices.map((e, i) => {
return (
<Grid.Column key={i}>
<DeviceType type={e.kind} onChangeData={this.changeDeviceData} device={e} edit={this.state.editMode}/>
</Grid.Column>
)
})
:
null
}
2020-03-17 16:38:03 +00:00
<Grid.Column>
2020-03-18 14:30:02 +00:00
<NewDevice devices={this.state.devices}/>
2020-03-17 16:38:03 +00:00
</Grid.Column>
</Grid>
</div>
)
}
}