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

66 lines
1.6 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";
2020-05-01 14:42:42 +00:00
import { Grid, Card } 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 14:42:42 +00:00
<Card.Group style={{ paddingTop: "3rem" }}>
2020-04-14 12:58:03 +00:00
{this.props.devices.map((e, i) => {
return (
<Grid.Column key={i}>
2020-04-18 14:26:12 +00:00
<Device tab={this.props.tab} id={e.id} />
2020-03-23 20:24:17 +00:00
</Grid.Column>
2020-04-14 12:58:03 +00:00
);
})}
{!this.props.isActiveRoomHome ? (
<Grid.Column>
<NewDevice />
</Grid.Column>
) : null}
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,
});
const DevicePanelContainer = connect(
mapStateToProps,
RemoteService
)(DevicePanel);
export default DevicePanelContainer;