frontend/smart-hut/src/components/dashboard/devices/Curtain.js

114 lines
3.2 KiB
JavaScript
Raw Normal View History

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";
import { Slider } from "@material-ui/core";
2020-04-23 14:02:02 +00:00
class Curtain extends Component {
constructor(props) {
2020-04-23 14:02:02 +00:00
super(props);
this.state = { intensity: this.props.device.intensity, timeout: null };
this.setIntensity = this.setIntensity.bind(this);
}
//getters
get turnedOn() {
return this.props.device.on;
}
get intensity() {
return this.props.device.intensity || 0;
}
onClickDevice = () => {
const on = !this.turnedOn;
2020-04-25 13:37:40 +00:00
if(this.props.tab==="Devices"){
this.props
.saveDevice({ ...this.props.device, on })
.catch((err) => console.error("curtains update error", err));
}else{
2020-04-25 14:18:01 +00:00
this.props.updateState({ id: this.props.sceneState.id, on: on },this.props.type);
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
.saveDevice({ ...this.props.device, intensity })
.catch((err) => console.error("curtain update error", err));
}else{
2020-04-25 14:18:01 +00:00
this.props.updateState({ id: this.props.sceneState.id, intensity: intensity },this.props.type);
2020-04-25 13:37:40 +00:00
}
2020-04-23 14:02:02 +00:00
};
helper = () => {
if (this.props.device.intensity >= 90) {
2020-04-23 14:02:02 +00:00
this.setIntensity(1);
this.saveIntensity();
} else {
this.setIntensity(this.props.device.intensity / 100 + 0.1);
2020-04-23 14:02:02 +00:00
this.saveIntensity();
}
};
///*this took me way too much more time than it should have*/
2020-04-23 14:02:02 +00:00
handleChange = (a) => {
this.setIntensity(a.target.value / 100);
this.saveIntensity();
};
2020-04-23 14:02:02 +00:00
render() {
return (
<div className="container">
<div
className="open-container"
style={{
height: (9 * this.props.device.intensity) / 100 + "rem",
}}
></div>{" "}
<span className="span-open">
{Math.round(this.props.device.intensity)}%
</span>
<input
onChange={this.handleChange}
value={this.props.device.intensity}
className="slider"
type="range"
min="0"
max="100"
/>
</div>
);
2020-04-23 14:02:02 +00:00
}
}
const mapStateToProps = (state, ownProps) => ({
device: state.devices[ownProps.id],
});
const CurtainContainer = connect(mapStateToProps, RemoteService)(Curtain);
export default CurtainContainer;