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

81 lines
2.1 KiB
JavaScript
Raw Normal View History

// vim: set ts=2 sw=2 et tw=80:
2020-05-12 13:18:33 +00:00
import React, { Component } from 'react';
import {
Segment, Card, Header, Icon,
} from 'semantic-ui-react';
import { connect } from 'react-redux';
import Device from './devices/Device';
import NewDevice from './devices/NewDevice';
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() {
2020-05-12 13:18:33 +00:00
if (this.props.tab === 'Devices') {
this.props
.fetchDevices()
2020-05-12 13:18:33 +00:00
.catch((err) => console.error('error fetching devices:', err));
}
2020-03-23 20:24:17 +00:00
}
render() {
return (
2020-05-12 13:18:33 +00:00
<Card.Group centered style={{ paddingTop: '3rem' }}>
{this.props.numbeOfRooms > 0 ? (
2020-05-12 13:18:33 +00:00
<>
{this.props.devices.map((e, i) => <Device key={i} tab={this.props.tab} id={e.id} />)}
{!this.props.isActiveRoomHome ? (
2020-05-12 13:18:33 +00:00
<Card style={{ height: '27em' }}>
<Segment basic style={{ width: '100%', height: '100%' }}>
<NewDevice />
</Segment>
</Card>
) : null}
2020-05-12 13:18:33 +00:00
</>
) : (
<Segment placeholder>
<Header icon>
<Icon
name="exclamation triangle"
2020-05-12 13:18:33 +00:00
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);
2020-05-12 13:18:33 +00:00
}
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,
2020-05-12 13:18:33 +00:00
RemoteService,
2020-04-09 15:24:30 +00:00
)(DevicePanel);
export default DevicePanelContainer;