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

172 lines
4.8 KiB
JavaScript
Raw Normal View History

2020-05-12 13:18:33 +00:00
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { RemoteService } from '../../remote';
import './Automations.css';
import {
2020-05-08 13:45:08 +00:00
Segment,
Grid,
Header,
Button,
List,
Divider,
Menu,
2020-05-12 13:18:33 +00:00
} from 'semantic-ui-react';
import CreateAutomation, { operands } from './AutomationCreationModal';
2020-04-28 09:39:38 +00:00
2020-05-12 13:18:33 +00:00
const Automation = ({
automation, devices, scenes, removeAutomation,
}) => {
2020-05-08 13:45:08 +00:00
const { triggers } = automation;
const scenePriorities = automation.scenes;
2020-05-12 13:18:33 +00:00
const getOperator = (operand) => operands.filter((o) => o.key === operand)[0].text;
2020-04-28 09:56:34 +00:00
2020-05-08 13:45:08 +00:00
return (
2020-05-12 13:18:33 +00:00
<>
<Header style={{ display: 'inline', marginRight: '1rem' }}>
2020-05-08 13:45:08 +00:00
{automation.name}
</Header>
<CreateAutomation id={automation.id} />
<Button
2020-05-12 13:18:33 +00:00
style={{ display: 'inline' }}
2020-05-08 13:45:08 +00:00
circular
onClick={() => removeAutomation(automation.id)}
color="red"
size="small"
2020-05-12 13:18:33 +00:00
icon="trash alternate outline"
2020-05-08 13:45:08 +00:00
/>
<Segment placeholder>
<Grid columns={2} stackable textAlign="center">
2020-05-12 13:18:33 +00:00
<Divider vertical />
2020-05-08 13:45:08 +00:00
<Grid.Row verticalAlign="middle">
<Grid.Column>
<Header>Triggers</Header>
<List divided relaxed>
2020-05-12 13:18:33 +00:00
{triggers !== undefined
&& triggers.map((trigger) => {
2020-05-08 13:45:08 +00:00
const device = devices.filter(
2020-05-12 13:18:33 +00:00
(d) => d.id === trigger.deviceId,
2020-05-08 13:45:08 +00:00
)[0];
return (
<Menu key={trigger.id} compact>
<Menu.Item as="span">
2020-05-12 13:18:33 +00:00
{device.name}
{' '}
2020-05-08 13:45:08 +00:00
{trigger.operator
2020-05-12 13:18:33 +00:00
? `${getOperator(trigger.operator)
} ${
trigger.range}`
2020-05-08 13:45:08 +00:00
: trigger.on
2020-05-12 13:18:33 +00:00
? ' - on'
: ' - off'}
2020-05-08 13:45:08 +00:00
</Menu.Item>
</Menu>
);
})}
</List>
</Grid.Column>
<Grid.Column>
<Header>Scenes</Header>
<List divided relaxed>
2020-05-12 13:18:33 +00:00
{scenePriorities !== undefined
&& scenePriorities.map((sp) => {
2020-05-08 13:45:08 +00:00
const sceneData = scenes.filter(
2020-05-12 13:18:33 +00:00
(s) => s.id === sp.sceneId,
2020-05-08 13:45:08 +00:00
)[0];
2020-05-12 13:18:33 +00:00
if (!sceneData) return '';
2020-05-08 13:45:08 +00:00
return (
<Menu key={sceneData.id} compact>
<Menu.Item as="span">{sceneData.name}</Menu.Item>
</Menu>
);
})}
</List>
</Grid.Column>
</Grid.Row>
</Grid>
</Segment>
2020-05-12 13:18:33 +00:00
</>
2020-05-08 13:45:08 +00:00
);
2020-04-28 09:56:34 +00:00
};
class AutomationsPanel extends Component {
2020-05-08 13:45:08 +00:00
constructor(props) {
super(props);
this.state = { openModal: false };
this.getDevices();
this.getScenes();
this.getAutomations();
}
2020-04-28 09:56:34 +00:00
2020-05-08 13:45:08 +00:00
getScenes() {
this.props.fetchAllScenes().catch(console.error);
}
2020-04-28 09:56:34 +00:00
2020-05-08 13:45:08 +00:00
getDevices() {
this.props
.fetchDevices()
2020-05-12 13:18:33 +00:00
.catch((err) => console.error('error fetching devices:', err));
2020-05-08 13:45:08 +00:00
}
2020-04-28 09:56:34 +00:00
2020-05-08 13:45:08 +00:00
getAutomations() {
this.props
.fetchAutomations()
2020-05-12 13:18:33 +00:00
.catch((err) => console.error('error fetching automations:', err));
2020-05-08 13:45:08 +00:00
}
2020-04-28 09:56:34 +00:00
2020-05-08 13:45:08 +00:00
removeAutomation = (id) => {
this.props
.deleteAutomation(id)
.catch((err) => console.error(`error removing automation ${id}:`, err));
};
2020-05-08 13:45:08 +00:00
render() {
return (
2020-05-12 13:18:33 +00:00
<Grid style={{ marginTop: '3rem' }}>
2020-05-08 13:45:08 +00:00
<Grid.Row>
<Grid.Column textAlign="center" width={16}>
{!this.state.openModal ? (
<List>
<CreateAutomation />
</List>
) : (
<Button color="green">CREATE AUTOMATION</Button>
)}
</Grid.Column>
</Grid.Row>
<Grid.Row>
2020-05-12 13:18:33 +00:00
{this.props.automations.map((automation, i) => (
<Grid.Column key={i} width={8} style={{ margin: '2rem 0' }}>
<Automation
removeAutomation={this.removeAutomation}
scenes={this.props.scenes}
devices={this.props.devices}
automation={automation}
/>
</Grid.Column>
))}
2020-05-08 13:45:08 +00:00
</Grid.Row>
</Grid>
);
}
2020-04-14 12:58:03 +00:00
}
const mapStateToProps = (state, _) => ({
2020-05-08 13:45:08 +00:00
activeRoom: state.active.activeRoom,
activeTab: state.active.activeTab,
get scenes() {
return Object.values(state.scenes);
},
get devices() {
return Object.values(state.devices);
},
get automations() {
return Object.values(state.automations);
},
2020-04-14 12:58:03 +00:00
});
const AutomationsPanelContainer = connect(
2020-05-08 13:45:08 +00:00
mapStateToProps,
2020-05-12 13:18:33 +00:00
RemoteService,
2020-04-14 12:58:03 +00:00
)(AutomationsPanel);
export default AutomationsPanelContainer;