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

82 lines
2.2 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 { Segment, Card, Header, Icon } from "semantic-ui-react";
2020-04-10 15:25:52 +00:00
import Device from "./devices/Device";
2020-03-18 14:30:02 +00:00
import NewDevice from "./devices/NewDevice";
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);
2020-04-09 15:24:30 +00:00
this.getDevices();
2020-03-23 20:24:17 +00:00
}
2020-04-09 15:24:30 +00:00
getDevices() {
if (this.props.tab === "Devices") {
this.props
.fetchDevices()
.catch((err) => console.error(`error fetching devices:`, err));
}
2020-03-23 20:24:17 +00:00
}
render() {
return (
2020-05-01 18:05:39 +00:00
<Card.Group centered style={{ paddingTop: "3rem" }}>
{this.props.numbeOfRooms > 0 ? (
<React.Fragment>
{this.props.devices.map((e, i) => {
return <Device key={i} tab={this.props.tab} id={e.id} />;
})}
{!this.props.isActiveRoomHome ? (
<Card style={{ height: "27em" }}>
<Segment basic style={{ width: "100%", height: "100%" }}>
<NewDevice />
</Segment>
</Card>
) : null}
</React.Fragment>
) : (
<Segment placeholder>
<Header icon>
<Icon
name="exclamation triangle"
style={{ paddingBottom: "1rem" }}
/>
Please create a room on the left, and then add devices to the
same.
</Header>
</Segment>
)}
2020-05-01 14:42:42 +00:00
</Card.Group>
2020-03-23 20:24:17 +00:00
);
}
}
2020-04-09 15:24:30 +00:00
const mapStateToProps = (state, _) => ({
get devices() {
if (state.active.activeRoom === -1) {
return Object.values(state.devices);
} else {
2020-04-12 15:49:29 +00:00
const deviceArray = [
...state.rooms[state.active.activeRoom].devices,
].sort();
2020-04-10 15:52:02 +00:00
return deviceArray.map((id) => state.devices[id]);
2020-04-09 15:24:30 +00:00
}
},
get isActiveRoomHome() {
2020-04-10 15:25:52 +00:00
return state.active.activeRoom === -1;
2020-04-09 15:24:30 +00:00
},
activeRoom: state.active.activeRoom,
get numbeOfRooms() {
return Object.keys(state.rooms).length;
},
2020-04-09 15:24:30 +00:00
});
const DevicePanelContainer = connect(
mapStateToProps,
RemoteService
)(DevicePanel);
export default DevicePanelContainer;