fixed update state

This commit is contained in:
britea 2020-04-27 15:20:48 +02:00
parent c878702331
commit af0c157205
5 changed files with 23 additions and 29 deletions

View file

@ -54,8 +54,8 @@ const mapStateToProps = (state, _) => ({
const stateArray = [
...state.scenes[state.active.activeScene].sceneStates,
].sort();
console.log(stateArray);
return stateArray;
console.log("STATESCENE", stateArray);
return stateArray.map((id) => state.sceneStates[id]);
} else {
return [];
}

View file

@ -19,7 +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);
this.deleteState = this.deleteState.bind(this);
}
edit() {
@ -211,11 +211,11 @@ const mapStateToProps = (state, ownProps) => ({
if (state.active.activeTab === "Devices") {
return state.devices[ownProps.id];
} else {
console.log(
/*console.log(
state.sceneStates,
ownProps.id,
state.sceneStates[ownProps.id]
);
);*/
return state.sceneStates[ownProps.id];
}
},
@ -231,12 +231,12 @@ const mapStateToProps = (state, ownProps) => ({
}
},
get type() {
console.log("ALPACA",state,ownProps);
console.log("ALPACA", state, ownProps);
if (state.active.activeTab === "Scenes") {
if (state.sceneStates[ownProps.id]) {
console.log(state.sceneStates[ownProps.id], ownProps.id);
//console.log(state.sceneStates[ownProps.id], ownProps.id);
const id = state.sceneStates[ownProps.id].deviceId;
console.log(id, state.devices[id].kind);
//console.log(id, state.devices[id].kind);
return state.devices[id].kind;
} else {
return "";

View file

@ -75,11 +75,11 @@ class Light extends Component {
this.props
.updateState(
{
id: this.props.sceneState.id,
id: this.props.stateOrDevice.id,
on: on,
sceneId: this.props.sceneState.sceneId,
sceneId: this.props.stateOrDevice.sceneId,
},
this.props.sceneState.kind
this.props.stateOrDevice.kind
)
.then((res) => {
console.log(res);

View file

@ -107,7 +107,6 @@ function reducer(previousState, action) {
break;
case "STATES_UPDATE":
//console.log(action.sceneStates);
newState = previousState;
change = null;
// if room is given, delete all devices in that room
@ -118,7 +117,7 @@ function reducer(previousState, action) {
sceneStates: { $unset: [] },
};
const scene = newState.scenes[action.sceneId];
const scene = previousState.scenes[action.sceneId];
for (const stateId of scene.sceneStates) {
change.sceneStates.$unset.push(stateId);
}
@ -147,17 +146,19 @@ function reducer(previousState, action) {
change.scenes[sceneState.sceneId].sceneStates || {};
const sceneStates = change.scenes[sceneState.sceneId].sceneStates;
sceneStates.$add = sceneStates.$add || [];
sceneStates.$add.push(sceneState);
sceneStates.$add.push(sceneState.id);
} else {
// room does not exist yet, so add to the list of pending
// joins
if (!change.pendingJoins.scenes[sceneState.sceneId]) {
change.pendingJoins.scenes[sceneState.sceneId] = {
$set: new Set([sceneState]),
$set: new Set([sceneState.id]),
};
} else {
change.pendingJoins.scenes[sceneState.sceneId].$set.add(sceneState);
change.pendingJoins.scenes[sceneState.sceneId].$set.add(
sceneState.id
);
}
}
}
@ -292,16 +293,12 @@ function reducer(previousState, action) {
sceneStates: { [action.sceneState.id]: { $set: action.sceneState } },
};
if (
!previousState.scenes[action.sceneState.sceneId].sceneStates[
action.sceneState.id
]
) {
console.log("Ciao");
if (previousState.scenes[action.sceneState.sceneId]) {
console.log("PREVSTATE", change, previousState);
change.scenes = {
[action.sceneState.sceneId]: {
sceneStates: {
$add: [action.sceneState],
$add: [action.sceneState.id],
},
},
};
@ -309,12 +306,13 @@ function reducer(previousState, action) {
change.pendingJoins = {
scenes: {
[action.sceneState.sceneId]: {
$add: [action.sceneState],
$add: [action.sceneState.id],
},
},
};
}
newState = update(previousState, change);
console.log("NEWSTATE ", newState);
break;
case "ROOM_DELETE":
if (!(action.roomId in previousState.rooms)) {
@ -467,7 +465,7 @@ function reducer(previousState, action) {
console.warn(`Action type ${action.type} unknown`, action);
return previousState;
}
console.log("THETRUEALPACA", newState, action.type, action);
return newState;
}

View file

@ -34,10 +34,6 @@ const actions = {
sceneId,
sceneStates,
}),
stateUpdate: (state) => ({
type: "STATE_UPDATE",
state,
}),
devicesUpdate: (roomId, devices, partial = false) => ({
type: "DEVICES_UPDATE",
roomId,