added updateState to store

This commit is contained in:
Jacob Salvi 2020-04-25 15:37:40 +02:00
parent a3eff85149
commit 1484026afd
5 changed files with 87 additions and 27 deletions

View file

@ -23,9 +23,13 @@ class Curtain extends Component {
onClickDevice = () => {
const on = !this.turnedOn;
if(this.props.tab==="Devices"){
this.props
.saveDevice({ ...this.props.device, on })
.catch((err) => console.error("curtains update error", err));
}else{
this.props.updateState({ ...this.props.device, on },this.props.type);
}
};
setIntensity(intensity) {
@ -49,9 +53,13 @@ class Curtain extends Component {
saveIntensity = () => {
const intensity = Math.round(this.state.intensity);
if(this.props.tab==="Devices"){
this.props
.saveDevice({ ...this.props.device, intensity })
.catch((err) => console.error("curtains update error", err));
.catch((err) => console.error("curtain update error", err));
}else{
this.props.updateState({ ...this.props.device, on },this.props.type);
}
};
helper = () => {

View file

@ -59,9 +59,14 @@ class Light extends Component {
onClickDevice = () => {
const on = !this.turnedOn;
if(this.props.tab==="Devices"){
this.props
.saveDevice({ ...this.props.device, on })
.catch((err) => console.error("regular light update error", err));
}else{
this.props.updateState({ ...this.props.device, on },this.props.type);
}
};
getIcon = () => {
@ -89,9 +94,13 @@ class Light extends Component {
saveIntensity = () => {
const intensity = Math.round(this.state.intensity);
if(this.props.tab==="Devices"){
this.props
.saveDevice({ ...this.props.device, intensity })
.catch((err) => console.error("intensity light update error", err));
.catch((err) => console.error("regular light update error", err));
}else{
this.props.updateState({ ...this.props.device, on },this.props.type);
}
};
render() {
@ -150,5 +159,6 @@ class Light extends Component {
const mapStateToProps = (state, ownProps) => ({
device: state.devices[ownProps.id],
});
const LightContainer = connect(mapStateToProps, RemoteService)(Light);
export default LightContainer;

View file

@ -33,9 +33,13 @@ class SmartPlug extends Component {
onClickDevice = () => {
const on = !this.turnedOn;
if(this.props.tab==="Devices"){
this.props
.saveDevice({ ...this.props.device, on })
.catch((err) => console.error("smart plug update error", err));
}else{
this.props.updateState({ ...this.props.device, on },this.props.type);
}
};
getIcon = () => {

View file

@ -63,23 +63,35 @@ class Thermostats extends Component {
//i came to the conclusion that is not possible to set mode.
//this.mode = "HEATING";
const turnOn = mode;
if(this.props.tab==="Devices"){
this.props
.saveDevice({ ...this.props.device, turnOn })
.catch((err) => console.error("regular light update error", err));
.catch((err) => console.error("thermostat update error", err));
}else{
this.props.updateState({ ...this.props.device, on },this.props.type);
}
}
onClickDevice = () => {
const on = !this.turnedOn;
if(this.props.tab==="Devices"){
this.props
.saveDevice({ ...this.props.device, on })
.catch((err) => console.error("regular light update error", err));
.catch((err) => console.error("thermostat update error", err));
}else{
this.props.updateState({ ...this.props.device, on },this.props.type);
}
};
//It seems to work
saveTargetTemperature(targetTemperature) {
if(this.props.tab==="Devices"){
this.props
.saveDevice({ ...this.props.device, targetTemperature })
.catch((err) => console.error(" update error", err));
.catch((err) => console.error("thermostat update error", err));
}else{
this.props.updateState({ ...this.props.device, on },this.props.type);
}
}
setTargetTemperature(newTemp) {
@ -101,9 +113,13 @@ class Thermostats extends Component {
//I have no idea why it doesn't want to update the temperature
saveInternalSensorTemperature(internalSensorTemperature) {
if(this.props.tab==="Devices"){
this.props
.saveDevice({ ...this.props.device, internalSensorTemperature })
.catch((err) => console.error(" update error", err));
.catch((err) => console.error("thermostat update error", err));
}else{
this.props.updateState({ ...this.props.device, on },this.props.type);
}
}
setInternalSensorTemperature(newTemp) {

View file

@ -374,6 +374,28 @@ export const RemoteService = {
};
},
//
updateState: (data,type)=>{
return (dispatch) => {
let url;
if(type=="dimmableState"){
url="/dimmableState"
}else{
url="/switchableState"
}
return Endpoint["put"](url, {}, data)
.then((res) => {
dispatch(actions.stateSave(res.data));
return res.data;
})
.catch((err) => {
console.warn("Update device: ", data, "error: ", err);
throw new RemoteError(["Network error"]);
});
};
},
/**
* Creates/Updates a device with the given data. If
* data.id is truthy, then a update call is performed,