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

118 lines
3.2 KiB
JavaScript
Raw Normal View History

2020-05-12 13:18:33 +00:00
import React, { Component } from 'react';
import {
Menu, Grid, Responsive, Dropdown,
} from 'semantic-ui-react';
import { connect } from 'react-redux';
import HostModal from '../components/HostModal';
import { RemoteService } from '../remote';
import { appActions } from '../storeActions';
class HostsNavbar extends Component {
constructor(props) {
super(props);
this.state = {
editMode: false,
};
2020-05-02 14:40:29 +00:00
this.getHosts();
2020-05-02 20:37:20 +00:00
this.selectHosts = this.selectHosts.bind(this);
2020-05-02 14:40:29 +00:00
}
getHosts() {
2020-05-02 20:37:20 +00:00
this.props.fetchHosts().catch(console.error);
}
2020-05-02 20:37:20 +00:00
selectHosts(_, { id }) {
this.props.setActiveHost(id || -1);
}
2020-05-02 20:37:20 +00:00
get activeItem() {
return this.props.activeHost;
}
get activeItemHostsName() {
2020-05-12 13:18:33 +00:00
if (this.props.activeItem === -1) return 'Home';
return this.props.hosts[this.props.activeHost].name;
}
render() {
return (
2020-05-02 20:37:20 +00:00
<div>
<Responsive minWidth={768}>
2020-05-12 13:18:33 +00:00
<Grid style={{ margin: '1em -1em 0 1em' }}>
<Grid.Row>
<Menu inverted fluid vertical>
<Menu.Item
key={-1}
id={null}
name="hosts"
2020-05-02 20:37:20 +00:00
active={this.activeItem === -1}
onClick={this.selectHosts}
>
2020-05-02 14:40:29 +00:00
<strong>Hosts</strong>
</Menu.Item>
2020-05-12 13:18:33 +00:00
{Object.values(this.props.hosts).map((e, i) => (
<Menu.Item
id={e.id}
key={i}
name={e.name}
active={this.activeItem === e.id}
onClick={this.selectHosts}
>
{e.name}
</Menu.Item>
))}
<Menu.Item name="newM">
2020-05-02 14:40:29 +00:00
<HostModal />
</Menu.Item>
</Menu>
</Grid.Row>
</Grid>
</Responsive>
<Responsive maxWidth={768}>
<Menu>
<Dropdown item fluid text={this.activeItemHostName}>
<Dropdown.Menu>
<Dropdown.Item
key={-1}
id={null}
name="scene"
2020-05-02 20:37:20 +00:00
active={this.activeItem === -1}
onClick={this.selectHosts}
>
2020-05-02 14:40:29 +00:00
<strong>Hosts</strong>
</Dropdown.Item>
2020-05-12 13:18:33 +00:00
{Object.values(this.props.hosts).map((e, i) => (
<Menu.Item
id={e.id}
key={i}
name={e.name}
active={this.activeItem === e.id}
onClick={this.selectHosts}
>
{e.name}
</Menu.Item>
))}
</Dropdown.Menu>
</Dropdown>
</Menu>
</Responsive>
</div>
);
}
}
2020-05-12 13:18:33 +00:00
const setActiveHost = (activeHost) => (dispatch) => dispatch(appActions.setActiveHost(activeHost));
const mapStateToProps = (state, _) => ({
hosts: state.hosts,
activeHost: state.active.activeHost,
});
const HostsNavbarContainer = connect(mapStateToProps, {
...RemoteService,
setActiveHost,
})(HostsNavbar);
export default HostsNavbarContainer;