2020-04-23 14:55:44 +00:00
|
|
|
import React, { Component } from "react";
|
|
|
|
import "./Curtains.css";
|
2020-04-23 14:02:02 +00:00
|
|
|
import { RemoteService } from "../../../remote";
|
|
|
|
import { connect } from "react-redux";
|
|
|
|
|
|
|
|
class Curtain extends Component {
|
2020-04-23 14:55:44 +00:00
|
|
|
constructor(props) {
|
2020-04-23 14:02:02 +00:00
|
|
|
super(props);
|
2020-04-25 15:27:56 +00:00
|
|
|
this.state = { intensity: this.props.stateOrDevice.intensity, timeout: null };
|
2020-04-23 14:02:02 +00:00
|
|
|
|
|
|
|
this.setIntensity = this.setIntensity.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//getters
|
|
|
|
get turnedOn() {
|
2020-04-25 15:27:56 +00:00
|
|
|
return this.props.stateOrDevice.on;
|
2020-04-23 14:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get intensity() {
|
2020-04-25 15:27:56 +00:00
|
|
|
return this.props.stateOrDevice.intensity || 0;
|
2020-04-23 14:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onClickDevice = () => {
|
|
|
|
const on = !this.turnedOn;
|
2020-04-25 13:37:40 +00:00
|
|
|
if(this.props.tab==="Devices"){
|
|
|
|
this.props
|
2020-04-25 15:27:56 +00:00
|
|
|
.saveDevice({ ...this.props.stateOrDevice, on })
|
2020-04-25 13:37:40 +00:00
|
|
|
.catch((err) => console.error("curtains update error", err));
|
|
|
|
}else{
|
2020-04-25 14:47:53 +00:00
|
|
|
this.props.updateState({ id: this.props.sceneState.id, on: on },this.props.sceneState.kind);
|
2020-04-25 13:37:40 +00:00
|
|
|
}
|
2020-04-23 14:02:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
setIntensity(intensity) {
|
|
|
|
intensity *= 100;
|
|
|
|
|
|
|
|
if (this.state.timeout) {
|
|
|
|
clearTimeout(this.state.timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
intensity,
|
|
|
|
timeout: setTimeout(() => {
|
|
|
|
this.saveIntensity();
|
|
|
|
this.setState({
|
|
|
|
intensity: this.state.intensity,
|
|
|
|
timeout: null,
|
|
|
|
});
|
|
|
|
}, 100),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
saveIntensity = () => {
|
|
|
|
const intensity = Math.round(this.state.intensity);
|
2020-04-25 13:37:40 +00:00
|
|
|
if(this.props.tab==="Devices"){
|
|
|
|
this.props
|
2020-04-25 15:27:56 +00:00
|
|
|
.saveDevice({ ...this.props.stateOrDevice, intensity })
|
2020-04-25 13:37:40 +00:00
|
|
|
.catch((err) => console.error("curtain update error", err));
|
|
|
|
}else{
|
2020-04-25 14:47:53 +00:00
|
|
|
this.props.updateState({ id: this.props.sceneState.id, intensity: intensity },this.props.sceneState.kind);
|
2020-04-25 13:37:40 +00:00
|
|
|
}
|
2020-04-23 14:02:02 +00:00
|
|
|
};
|
|
|
|
|
2020-04-23 14:55:44 +00:00
|
|
|
helper = () => {
|
|
|
|
if (this.props.device.intensity >= 90) {
|
2020-04-23 14:02:02 +00:00
|
|
|
this.setIntensity(1);
|
|
|
|
this.saveIntensity();
|
2020-04-23 14:55:44 +00:00
|
|
|
} else {
|
2020-04-25 15:27:56 +00:00
|
|
|
this.setIntensity(this.props.stateOrDevice.intensity / 100 + 0.1);
|
2020-04-23 14:02:02 +00:00
|
|
|
this.saveIntensity();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-04-23 14:55:44 +00:00
|
|
|
///*this took me way too much more time than it should have*/
|
2020-04-23 14:02:02 +00:00
|
|
|
|
2020-04-23 14:55:44 +00:00
|
|
|
handleChange = (a) => {
|
|
|
|
this.setIntensity(a.target.value / 100);
|
|
|
|
this.saveIntensity();
|
|
|
|
};
|
2020-04-23 14:02:02 +00:00
|
|
|
|
2020-04-23 14:55:44 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className="container">
|
|
|
|
<div
|
|
|
|
className="open-container"
|
|
|
|
style={{
|
2020-04-25 15:27:56 +00:00
|
|
|
height: (9 * this.props.stateOrDevice.intensity) / 100 + "rem",
|
2020-04-23 14:55:44 +00:00
|
|
|
}}
|
|
|
|
></div>{" "}
|
|
|
|
<span className="span-open">
|
2020-04-25 15:27:56 +00:00
|
|
|
{Math.round(this.props.stateOrDevice.intensity)}%
|
2020-04-23 14:55:44 +00:00
|
|
|
</span>
|
|
|
|
<input
|
|
|
|
onChange={this.handleChange}
|
2020-04-25 15:27:56 +00:00
|
|
|
value={this.props.stateOrDevice.intensity}
|
2020-04-23 14:55:44 +00:00
|
|
|
className="slider"
|
|
|
|
type="range"
|
|
|
|
min="0"
|
|
|
|
max="100"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
);
|
2020-04-23 14:02:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => ({
|
2020-04-25 15:27:56 +00:00
|
|
|
get stateOrDevice(){
|
|
|
|
if(state.active.activeTab==="Devices"){
|
|
|
|
return state.devices[ownProps.id];
|
|
|
|
}else{
|
|
|
|
const sceneState = state.sceneStates[ownProps.id];
|
|
|
|
return state.devices[sceneState];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
//device: state.devices[ownProps.id],
|
2020-04-23 14:02:02 +00:00
|
|
|
});
|
|
|
|
const CurtainContainer = connect(mapStateToProps, RemoteService)(Curtain);
|
2020-04-23 14:55:44 +00:00
|
|
|
export default CurtainContainer;
|