frontend/smart-hut/src/views/Dashboard.js

240 lines
7.1 KiB
JavaScript
Raw Normal View History

2020-05-12 13:18:33 +00:00
import React, { Component } from 'react';
import {
Grid, Responsive, Button, Menu,
} from 'semantic-ui-react';
import { connect } from 'react-redux';
import DevicePanel from '../components/dashboard/DevicePanel';
import ScenesPanel from '../components/dashboard/ScenesPanel';
import AutomationsPanel from '../components/dashboard/AutomationsPanel';
import HostsPanel from '../components/dashboard/HostsPanel';
import Navbar from './Navbar';
import ScenesNavbar from './ScenesNavbar';
import HostsNavbar from './HostsNavbar';
import MyHeader from '../components/HeaderController';
import { mobilePanelStyle } from '../components/dashboard/devices/styleComponents';
2020-04-14 12:58:03 +00:00
2020-05-12 13:18:33 +00:00
import { RemoteService } from '../remote';
import { appActions } from '../storeActions';
2020-04-14 12:58:03 +00:00
class Dashboard extends Component {
2020-04-28 10:04:01 +00:00
constructor(props) {
super(props);
this.state = this.initialState;
2020-05-12 13:18:33 +00:00
this.activeTab = 'Devices';
2020-04-28 10:04:01 +00:00
this.selectTab = this.selectTab.bind(this);
}
2020-04-14 12:58:03 +00:00
2020-04-28 10:04:01 +00:00
get initialState() {
return {
activeTab: this.activeTab,
};
}
2020-04-18 14:26:12 +00:00
2020-04-28 10:04:01 +00:00
setInitialState() {
this.setState(this.initialState);
}
2020-04-18 14:26:12 +00:00
2020-04-28 10:04:01 +00:00
get activeTab() {
return this.props.activeTab;
}
2020-04-14 12:58:03 +00:00
2020-04-28 10:04:01 +00:00
set activeTab(tab) {
this.props.setActiveTab(tab);
}
2020-04-14 12:58:03 +00:00
2020-04-28 10:04:01 +00:00
selectTab(e, { name }) {
this.setState({ activeTab: name });
this.activeTab = name;
}
2020-04-14 12:58:03 +00:00
2020-04-28 10:04:01 +00:00
renderTab(tab) {
switch (tab) {
2020-05-12 13:18:33 +00:00
case 'Devices':
2020-04-28 10:04:01 +00:00
return <DevicePanel tab={this.state.activeTab} />;
2020-05-12 13:18:33 +00:00
case 'Scenes':
2020-04-28 10:04:01 +00:00
return <ScenesPanel tab={this.state.activeTab} />;
2020-05-12 13:18:33 +00:00
case 'Automations':
2020-04-28 10:04:01 +00:00
return <AutomationsPanel />;
2020-05-12 13:18:33 +00:00
case 'Hosts':
return <HostsPanel />;
2020-04-28 10:04:01 +00:00
default:
return <h1>ERROR</h1>;
2020-04-14 12:58:03 +00:00
}
2020-04-28 10:04:01 +00:00
}
2020-03-17 15:20:43 +00:00
2020-04-28 10:04:01 +00:00
renderNavbar(tab) {
switch (tab) {
2020-05-12 13:18:33 +00:00
case 'Devices':
2020-04-28 10:04:01 +00:00
return <Navbar />;
2020-05-12 13:18:33 +00:00
case 'Scenes':
2020-04-28 10:04:01 +00:00
return <ScenesNavbar />;
2020-05-12 13:18:33 +00:00
case 'Hosts':
return <HostsNavbar />;
2020-04-28 10:04:01 +00:00
default:
return <h1>ERROR</h1>;
}
2020-04-28 10:04:01 +00:00
}
2020-05-01 14:42:42 +00:00
get hasNavbar() {
2020-05-12 13:18:33 +00:00
return this.state.activeTab !== 'Automations';
2020-05-01 14:42:42 +00:00
}
2020-04-28 10:04:01 +00:00
render() {
2020-05-07 07:58:49 +00:00
// needed to correctly assign the background image
2020-05-12 13:18:33 +00:00
// in case a room has one.
2020-05-07 07:58:49 +00:00
let backgroundImageHelper;
2020-05-12 13:18:33 +00:00
if (this.activeTab === 'Devices') {
2020-05-08 13:37:01 +00:00
backgroundImageHelper = this.props.backgroundImage;
} else {
backgroundImageHelper = null;
2020-05-07 07:58:49 +00:00
}
2020-05-12 13:18:33 +00:00
// console.log("helper is",helper)
2020-04-28 10:04:01 +00:00
return (
2020-05-12 13:18:33 +00:00
<div style={{ background: '#1b1c1d' }}>
2020-04-28 10:04:01 +00:00
<Responsive minWidth={768}>
<Grid>
<Grid.Row color="black" style={{ paddingBottom: 0 }}>
2020-04-28 10:04:01 +00:00
<Grid.Column>
<MyHeader />
</Grid.Column>
</Grid.Row>
<Grid.Row color="black" style={{ paddingTop: 0 }}>
2020-05-01 14:42:42 +00:00
<Grid.Column textAlign="center" width={16}>
2020-05-02 14:40:29 +00:00
<Menu fluid widths={4} inverted color="grey">
2020-05-01 14:42:42 +00:00
<Menu.Item
name="Devices"
content="Devices"
2020-05-12 13:18:33 +00:00
active={this.activeTab === 'Devices'}
2020-05-01 14:42:42 +00:00
onClick={this.selectTab}
/>
<Menu.Item
name="Scenes"
content="Scenes"
2020-05-12 13:18:33 +00:00
active={this.activeTab === 'Scenes'}
2020-05-01 14:42:42 +00:00
onClick={this.selectTab}
/>
<Menu.Item
name="Automations"
content="Automations"
2020-05-12 13:18:33 +00:00
active={this.activeTab === 'Automations'}
2020-05-01 14:42:42 +00:00
onClick={this.selectTab}
/>
<Menu.Item
name="Hosts"
2020-05-02 18:50:34 +00:00
content="Hosts and Guests"
2020-05-12 13:18:33 +00:00
active={this.activeTab === 'Hosts'}
onClick={this.selectTab}
/>
2020-05-01 14:42:42 +00:00
</Menu>
2020-04-28 10:04:01 +00:00
</Grid.Column>
2020-03-22 16:58:27 +00:00
2020-05-01 14:42:42 +00:00
{this.hasNavbar && (
<Grid.Column width={3}>
{this.renderNavbar(this.activeTab)}
</Grid.Column>
)}
<Grid.Column width={this.hasNavbar ? 13 : 16}>
<div
style={{
2020-05-12 13:18:33 +00:00
backgroundImage: `url(${backgroundImageHelper})`,
backgroundColor: '#fafafa',
height: '85vh',
padding: '0rem 3rem',
color: '#000000',
overflow: 'auto',
maxHeight: '75vh',
}}
>
{this.renderTab(this.activeTab)}
</div>
2020-04-28 10:04:01 +00:00
</Grid.Column>
</Grid.Row>
</Grid>
</Responsive>
<Responsive maxWidth={768}>
<Grid inverted>
<Grid.Row color="black">
<Grid.Column>
<MyHeader />
</Grid.Column>
</Grid.Row>
<Grid.Row color="black">
<Grid.Column textAlign="center">
<Button
basic
name="Devices"
content="Devices"
2020-05-12 13:18:33 +00:00
active={this.activeTab === 'Devices'}
color={this.activeTab === 'Devices' ? 'yellow' : 'grey'}
2020-04-28 10:04:01 +00:00
onClick={this.selectTab}
/>
<Button
basic
name="Scenes"
content="Scenes"
2020-05-12 13:18:33 +00:00
active={this.activeTab === 'Scenes'}
color={this.activeTab === 'Scenes' ? 'yellow' : 'grey'}
2020-04-28 10:04:01 +00:00
onClick={this.selectTab}
/>
<Button
basic
name="Automations"
content="Automations"
2020-05-12 13:18:33 +00:00
active={this.activeTab === 'Automations'}
color={this.activeTab === 'Automations' ? 'yellow' : 'grey'}
2020-04-28 10:04:01 +00:00
onClick={this.selectTab}
/>
<Button
basic
name="Hosts"
content="Hosts"
2020-05-12 13:18:33 +00:00
active={this.activeTab === 'Hosts'}
2020-05-02 18:50:34 +00:00
color={
2020-05-12 13:18:33 +00:00
this.activeTab === 'Hosts and Guests' ? 'yellow' : 'grey'
2020-05-02 18:50:34 +00:00
}
onClick={this.selectTab}
/>
2020-04-28 10:04:01 +00:00
</Grid.Column>
</Grid.Row>
2020-05-01 14:42:42 +00:00
{this.hasNavbar && (
<Grid.Row color="black">
<Grid.Column color="black">
{this.renderNavbar(this.activeTab)}
</Grid.Column>
</Grid.Row>
)}
2020-04-28 10:04:01 +00:00
<Grid.Row>
<Grid.Column>
<div style={mobilePanelStyle}>
{this.renderTab(this.activeTab)}
</div>
</Grid.Column>
</Grid.Row>
</Grid>
</Responsive>
</div>
);
}
}
2020-04-14 12:58:03 +00:00
const mapStateToProps = (state, _) => ({
2020-04-28 10:04:01 +00:00
activeTab: state.active.activeTab,
get currentRoom() {
2020-05-07 07:58:49 +00:00
return state.active.activeRoom;
},
2020-05-08 13:37:01 +00:00
get backgroundImage() {
if (state.active.activeRoom === -1) {
2020-05-07 07:58:49 +00:00
return null;
}
2020-05-12 13:18:33 +00:00
return state.rooms[state.active.activeRoom].image;
2020-05-07 07:58:49 +00:00
},
2020-04-14 12:58:03 +00:00
});
2020-05-12 13:18:33 +00:00
const setActiveTab = (activeTab) => (dispatch) => dispatch(appActions.setActiveTab(activeTab));
2020-04-14 12:58:03 +00:00
const DashboardContainer = connect(mapStateToProps, {
2020-04-28 10:04:01 +00:00
...RemoteService,
setActiveTab,
2020-04-14 12:58:03 +00:00
})(Dashboard);
export default DashboardContainer;