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

121 lines
2.9 KiB
JavaScript
Raw Normal View History

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