frontend/smart-hut/src/deviceProps.js

62 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-05-03 16:09:53 +00:00
function getStateOrDevice(state, ownProps) {
switch (state.active.activeTab) {
case "Devices":
return state.devices[ownProps.id];
case "Scenes":
return state.sceneStates[ownProps.id];
case "Hosts":
return state.hostDevices[ownProps.hostId][ownProps.id];
default:
throw new Error(
`stateOrDevice has no value in tab "${state.active.activeTab}"`
);
}
}
function getDevice(state, ownProps) {
switch (state.active.activeTab) {
case "Scenes":
return state.devices[getStateOrDevice(state, ownProps).deviceId];
case "Devices":
case "Hosts":
return getStateOrDevice(state, ownProps);
default:
throw new Error(`device has no value in tab "${state.active.activeTab}"`);
}
}
function getRoomName(state, ownProps) {
switch (state.active.activeTab) {
case "Scenes":
case "Devices":
return (state.rooms[getDevice(state, ownProps).roomId] || {}).name;
case "Hosts":
const hostRooms = state.hostRooms[ownProps.hostId];
if (!hostRooms) return "";
const room = hostRooms[getDevice(state, ownProps).roomId];
if (!room) return "";
return room.name;
2020-05-03 16:13:50 +00:00
default:
throw new Error(
`room name has no value in tab "${state.active.activeTab}"`
);
2020-05-03 16:09:53 +00:00
}
}
export default function mapStateToProps(state, ownProps) {
return {
get stateOrDevice() {
return getStateOrDevice(state, ownProps);
},
get device() {
return getDevice(state, ownProps);
},
2020-05-03 16:11:04 +00:00
get roomName() {
return getRoomName(state, ownProps);
},
2020-05-03 16:09:53 +00:00
get type() {
return getDevice(state, ownProps).kind;
},
};
}