Merge branch '82-scene-states-update' of https://lab.si.usi.ch/sa4-2020/the-sanmarinoes/frontend into 82-scene-states-update

This commit is contained in:
britea 2020-04-25 19:00:56 +02:00
commit 34f83c9cce
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.modalRef = React.createRef();
this.edit = this.edit.bind(this); this.edit = this.edit.bind(this);
this.resetSmartPlug = this.resetSmartPlug.bind(this); this.resetSmartPlug = this.resetSmartPlug.bind(this);
this.deleteState=this.deleteState.bind(this);
} }
edit() { edit() {
@ -33,11 +34,11 @@ class Device extends React.Component {
} }
deleteState() { deleteState() {
//console.log("alpaca "+this.props);
this.props.deleteState(this.props.id, this.props.type); this.props.deleteState(this.props.id, this.props.type);
} }
renderDeviceComponent() { renderDeviceComponent() {
console.log(JSON.stringify(this.props.stateOrDevice));
switch ( switch (
this.props.tab === "Devices" this.props.tab === "Devices"
? this.props.stateOrDevice.kind ? this.props.stateOrDevice.kind

View file

@ -378,7 +378,7 @@ export const RemoteService = {
updateState: (data, type) => { updateState: (data, type) => {
return (dispatch) => { return (dispatch) => {
let url; let url;
if (type == "dimmableState") { if (type === "dimmableState") {
url = "/dimmableState"; url = "/dimmableState";
} else { } else {
url = "/switchableState"; url = "/switchableState";
@ -397,7 +397,20 @@ export const RemoteService = {
}, },
deleteState: (id, type)=>{ 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 } }; 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); newState = update(previousState, change);
break; break;
case "DEVICE_DELETE": case "DEVICE_DELETE":

View file

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