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

189 lines
4.7 KiB
JavaScript
Raw Normal View History

2020-05-12 13:18:33 +00:00
import React, { Component } from 'react';
import {
Button, Modal, Icon, Image, Form, Dropdown,
} from 'semantic-ui-react';
import { connect } from 'react-redux';
import styled from 'styled-components';
import { RemoteService } from '../../remote';
// import { appActions } from "../../storeActions";
2020-04-16 13:15:33 +00:00
const StyledDiv = styled.div`
background-color: #505bda;
padding: 3rem;
width: 10rem;
height: 10rem;
border-radius: 100%;
border: none;
position: relative;
box-shadow: 3px 2px 10px 5px #ccc;
transition: all 0.3s ease-out;
:hover {
background-color: #4345d9;
}
:active {
transform: translate(0.3px, 0.8px);
box-shadow: 0.5px 0.5px 7px 3.5px #ccc;
}
`;
class NewSceneDevice extends Component {
constructor(props) {
super(props);
this.state = {
openModal: false,
sceneDevices: this.props.scene ? this.props.scene.sceneStates : {},
2020-05-12 13:18:33 +00:00
deviceName: '',
availableDevices: [],
2020-04-16 13:15:33 +00:00
};
2020-04-16 15:07:56 +00:00
this.getDevices();
2020-05-12 13:18:33 +00:00
// this.getSceneStates();
this.availableDevices();
2020-05-12 13:18:33 +00:00
// console.log(this.state);
2020-04-16 15:07:56 +00:00
this.setSceneState = this.setSceneState.bind(this);
this.createState = this.createState.bind(this);
this.availableDevices = this.availableDevices.bind(this);
2020-04-16 15:07:56 +00:00
}
getDevices() {
this.props
.fetchDevices()
2020-05-12 13:18:33 +00:00
.catch((err) => console.error('error fetching devices:', err));
2020-04-16 13:15:33 +00:00
}
// getSceneStates() {
// this.props
// .fetchStates(this.props.activeScene)
// .catch((err) => console.error(`error fetching states`, err));
// }
2020-04-16 13:15:33 +00:00
handleOpen = () => {
this.setState({ openModal: true });
};
2020-05-02 20:37:20 +00:00
2020-04-16 13:15:33 +00:00
handleClose = () => {
this.setState({ openModal: false });
};
availableDevices() {
2020-05-12 13:18:33 +00:00
const availableDevices = [];
this.props.devices.forEach((e) => {
if (
Object.values(this.props.sceneStates).filter((d) => e.id === d.deviceId)
.length < 1
) {
2020-05-12 13:18:33 +00:00
if (e.flowType === 'OUTPUT') {
availableDevices.push({
key: e.id,
text: e.name,
value: e.id,
});
}
} else {
2020-05-12 13:18:33 +00:00
// console.log("NOT FOUND", e);
}
});
2020-05-12 13:18:33 +00:00
this.setState({ availableDevices });
// return availableDevices;
}
2020-04-16 13:15:33 +00:00
resetState = () => {
this.setState(this.baseState);
this.handleClose();
};
setSceneState(e, d) {
2020-04-16 15:07:56 +00:00
this.setState({ devicesAttached: d.value });
}
createState() {
2020-05-01 17:55:08 +00:00
for (let i = 0; i < this.state.devicesAttached.length; i++) {
2020-05-12 13:18:33 +00:00
const device = this.props.devices.filter(
(e) => this.state.devicesAttached[i] === e.id,
2020-05-01 17:55:08 +00:00
);
2020-05-12 13:18:33 +00:00
const data = {
2020-05-01 17:55:08 +00:00
sceneId: this.props.activeScene,
id: device[0].id,
kind: device[0].kind,
};
this.props
.saveState(data)
2020-05-12 13:18:33 +00:00
.catch((err) => console.error('error in creating state', err));
2020-05-01 17:55:08 +00:00
}
this.resetState();
}
2020-04-16 13:15:33 +00:00
render() {
return (
<Modal
closeIcon
open={this.state.openModal}
onClose={this.resetState}
2020-05-12 13:18:33 +00:00
trigger={(
2020-05-02 11:58:02 +00:00
<StyledDiv
onClick={this.handleOpen}
style={{
2020-05-12 13:18:33 +00:00
position: 'relative',
top: 'calc(50% - 5rem)',
left: 'calc(50% - 5rem)',
2020-05-02 11:58:02 +00:00
}}
>
2020-05-12 13:18:33 +00:00
<Image src="/img/add.svg" style={{ filter: 'invert()' }} />
2020-04-16 13:15:33 +00:00
</StyledDiv>
2020-05-12 13:18:33 +00:00
)}
centered
2020-04-16 13:15:33 +00:00
>
<Modal.Header>Add a New Scene State</Modal.Header>
2020-04-16 15:07:56 +00:00
<Modal.Content>
<Form>
2020-05-12 13:18:33 +00:00
<Form.Field style={{ marginTop: '1rem' }}>
2020-04-16 15:07:56 +00:00
<label>Select devices you want to attach: </label>
<Dropdown
name="scene devices"
placeholder="Select Devices"
fluid
multiple
onChange={this.setSceneState}
onClick={() => this.availableDevices()}
options={this.state.availableDevices}
2020-04-16 15:07:56 +00:00
/>
</Form.Field>
</Form>
</Modal.Content>
2020-04-16 13:15:33 +00:00
<Modal.Actions>
<Button
onClick={this.createState}
2020-04-16 13:15:33 +00:00
color="blue"
icon
labelPosition="right"
>
<Icon name="up arrow" />
Finish
</Button>
</Modal.Actions>
</Modal>
);
}
}
const mapStateToProps = (state, _) => ({
devices: Object.values(state.devices),
get sceneStates() {
if (state.active.activeScene !== -1) {
const stateArray = [
...state.scenes[state.active.activeScene].sceneStates,
].sort();
console.log(state.scenes[state.active.activeScene]);
return stateArray.map((id) => state.sceneStates[id]);
}
2020-05-12 13:18:33 +00:00
return [];
},
2020-04-16 15:07:56 +00:00
activeScene: state.active.activeScene,
2020-04-16 13:15:33 +00:00
});
const NewSceneDeviceContainer = connect(
mapStateToProps,
2020-05-12 13:18:33 +00:00
RemoteService,
2020-04-16 13:15:33 +00:00
)(NewSceneDevice);
export default NewSceneDeviceContainer;