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

122 lines
3.3 KiB
JavaScript
Raw Normal View History

// vim: set ts=2 sw=2 et tw=80:
2020-03-23 20:24:17 +00:00
import React, { Component } from "react";
import { Grid } from "semantic-ui-react";
import { editButtonStyle, panelStyle } from "./devices/styleComponents";
import { checkMaxLength, DEVICE_NAME_MAX_LENGTH } from "./devices/constants";
import DeviceType from "./devices/DeviceTypeController";
2020-03-18 14:30:02 +00:00
import NewDevice from "./devices/NewDevice";
2020-03-20 17:42:38 +00:00
import SettingsModal from "./devices/SettingsModal";
2020-04-09 15:24:30 +00:00
import { connect } from "react-redux";
import { RemoteService } from "../../remote";
2020-04-09 15:24:30 +00:00
class DevicePanel extends Component {
2020-03-23 20:24:17 +00:00
constructor(props) {
super(props);
this.state = {
editMode: false,
2020-03-19 10:52:13 +00:00
};
2020-03-23 20:24:17 +00:00
this.addDevice = this.addDevice.bind(this);
2020-04-09 15:24:30 +00:00
this.toggleEditMode = this.toggleEditMode.bind(this);
this.getDevices();
2020-03-23 20:24:17 +00:00
}
2020-04-09 15:24:30 +00:00
toggleEditMode(e) {
2020-03-23 20:24:17 +00:00
this.setState((prevState) => ({ editMode: !prevState.editMode }));
2020-04-09 15:24:30 +00:00
}
2020-03-23 20:24:17 +00:00
openModal = (settingsDeviceId) => {
this.setState((prevState) => ({
openSettingsModal: !prevState.openSettingsModal,
settingsDeviceId: settingsDeviceId,
}));
};
2020-04-09 15:24:30 +00:00
/*changeDeviceData = (deviceId, newSettings) => {
2020-03-23 20:24:17 +00:00
console.log(newSettings.name, " <-- new name --> ", deviceId);
2020-04-09 15:24:30 +00:00
for (let prop in this.props.devices[deviceId]) {
if (prop === "name") {
if (checkMaxLength(newSettings[prop])) {
device[prop] = newSettings[prop];
} else {
alert(
"Name must be less than " +
DEVICE_NAME_MAX_LENGTH +
" characters."
);
2020-03-20 17:42:38 +00:00
}
2020-04-09 15:24:30 +00:00
} else {
device[prop] = newSettings[prop];
2020-03-23 20:24:17 +00:00
}
2020-03-19 10:52:13 +00:00
}
2020-04-09 15:24:30 +00:00
};*/
2020-03-23 20:24:17 +00:00
2020-04-09 15:24:30 +00:00
getDevices() {
this.props.fetchDevices().then();
2020-03-23 20:24:17 +00:00
}
render() {
const edit = {
mode: this.state.editMode,
openModal: this.openModal,
};
2020-03-25 16:20:53 +00:00
/*var backGroundImg =
this.props.activeItem === -1 ? "" : this.props.room.image;*/
2020-03-23 20:24:17 +00:00
const ds = this.state.devices ? this.state.devices : this.props.devices;
2020-03-23 20:24:17 +00:00
return (
<div style={panelStyle}>
<button style={editButtonStyle} onClick={this.editModeController}>
Edit
</button>
<Grid doubling columns={4} divided="vertically">
{this.state.openSettingsModal ? (
<SettingsModal
openModal={this.openModal}
device={ds.filter((d) => d.id === this.state.settingsDeviceId)[0]}
/>
) : (
""
)}
2020-04-09 15:24:30 +00:00
{this.props.devices.map((e, i) => {
return (
<Grid.Column key={i}>
<DeviceType type={e.kind} device={e} edit={edit} />
</Grid.Column>
);
})}
{!this.props.isActiveRoomHome ? (
2020-03-23 20:24:17 +00:00
<Grid.Column>
2020-04-09 15:24:30 +00:00
<NewDevice />
2020-03-23 20:24:17 +00:00
</Grid.Column>
) : null}
</Grid>
</div>
);
}
}
2020-04-09 15:24:30 +00:00
const mapStateToProps = (state, _) => ({
get devices() {
if (state.active.activeRoom === -1) {
return Object.values(state.devices);
} else {
return state.rooms[state.active.activeRoom].devices.map(
(id) => state.devices[id]
);
}
},
get isActiveRoomHome() {
return this.props.activeRoom === -1;
},
activeRoom: state.active.activeRoom,
});
const DevicePanelContainer = connect(
mapStateToProps,
RemoteService
)(DevicePanel);
export default DevicePanelContainer;