diff --git a/smart-hut/src/components/dashboard/devices/Device.js b/smart-hut/src/components/dashboard/devices/Device.js index 1c2604c..24407c1 100644 --- a/smart-hut/src/components/dashboard/devices/Device.js +++ b/smart-hut/src/components/dashboard/devices/Device.js @@ -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 diff --git a/smart-hut/src/remote.js b/smart-hut/src/remote.js index 557209f..f8e62a7 100644 --- a/smart-hut/src/remote.js +++ b/smart-hut/src/remote.js @@ -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"]); + }); + }; }, /** diff --git a/smart-hut/src/store.js b/smart-hut/src/store.js index aa8c589..15dbf16 100644 --- a/smart-hut/src/store.js +++ b/smart-hut/src/store.js @@ -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": diff --git a/smart-hut/src/storeActions.js b/smart-hut/src/storeActions.js index 3ca8bcf..556ae02 100644 --- a/smart-hut/src/storeActions.js +++ b/smart-hut/src/storeActions.js @@ -40,6 +40,10 @@ const actions = { devices, partial, }), + stateDelete: (stateId)=>({ + type: "STATE_DELETE", + stateId, + }), deviceOperationUpdate: (devices) => ({ type: "DEVICES_UPDATE", devices,