idk how but delete works

This commit is contained in:
Jacob Salvi 2020-04-25 18:44:54 +02:00
parent b6a27195fe
commit 79afe780b3
4 changed files with 41 additions and 3 deletions

View file

@ -19,6 +19,7 @@ class Device extends React.Component {
this.modalRef = React.createRef();
this.edit = this.edit.bind(this);
this.resetSmartPlug = this.resetSmartPlug.bind(this);
this.deleteState=this.deleteState.bind(this);
}
edit() {
@ -33,11 +34,11 @@ class Device extends React.Component {
}
deleteState() {
//console.log("alpaca "+this.props);
this.props.deleteState(this.props.id, this.props.type);
}
renderDeviceComponent() {
console.log(JSON.stringify(this.props.stateOrDevice));
switch (
this.props.tab === "Devices"
? this.props.stateOrDevice.kind

View file

@ -378,7 +378,7 @@ export const RemoteService = {
updateState: (data, type) => {
return (dispatch) => {
let url;
if (type == "dimmableState") {
if (type === "dimmableState") {
url = "/dimmableState";
} else {
url = "/switchableState";
@ -397,7 +397,20 @@ export const RemoteService = {
},
deleteState: (id, type)=>{
return (dispatch)=>{
let url;
if (type === "dimmableState") {
url = "/dimmableState";
} else {
url = "/switchableState";
}
return Endpoint.delete(url+`/${id}`)
.then((_) => dispatch(actions.stateDelete(id)))
.catch((err) => {
console.warn("state delete error", err);
throw new RemoteError(["Network error"]);
});
};
},
/**

View file

@ -356,6 +356,26 @@ function reducer(previousState, action) {
change.active = { activeScene: { $set: -1 } };
}
newState = update(previousState, change);
break;
case "STATE_DELETE":
if (!(action.stateId in previousState.sceneStates)) {
console.warn(`State to delete ${action.stateId} does not exist`);
break;
}
change = {
sceneStates: { $unset: [action.stateId] },
};
if (previousState.scenes[previousState.sceneStates[action.stateId].sceneId]) {
change.scenes = {
[previousState.sceneStates[action.stateId].sceneId]: {
sceneStates: { $remove: [action.stateId] },
},
};
}
newState = update(previousState, change);
break;
case "DEVICE_DELETE":

View file

@ -40,6 +40,10 @@ const actions = {
devices,
partial,
}),
stateDelete: (stateId)=>({
type: "STATE_DELETE",
stateId,
}),
deviceOperationUpdate: (devices) => ({
type: "DEVICES_UPDATE",
devices,