frontend/smart-hut/src/deviceProps.js

70 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-05-03 16:09:53 +00:00
function getStateOrDevice(state, ownProps) {
switch (state.active.activeTab) {
2020-05-12 13:18:33 +00:00
case 'Devices':
2020-05-03 16:09:53 +00:00
return state.devices[ownProps.id];
2020-05-12 13:18:33 +00:00
case 'Scenes':
2020-05-03 16:09:53 +00:00
return state.sceneStates[ownProps.id];
2020-05-12 13:18:33 +00:00
case 'Hosts':
2020-05-03 16:09:53 +00:00
return state.hostDevices[ownProps.hostId][ownProps.id];
default:
throw new Error(
2020-05-12 13:18:33 +00:00
`stateOrDevice has no value in tab "${state.active.activeTab}"`,
2020-05-03 16:09:53 +00:00
);
}
}
function getDevice(state, ownProps) {
switch (state.active.activeTab) {
2020-05-12 13:18:33 +00:00
case 'Scenes':
2020-05-03 16:09:53 +00:00
return state.devices[getStateOrDevice(state, ownProps).deviceId];
2020-05-12 13:18:33 +00:00
case 'Devices':
case 'Hosts':
2020-05-03 16:09:53 +00:00
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) {
2020-05-12 13:18:33 +00:00
case 'Scenes':
case 'Devices':
2020-05-03 16:09:53 +00:00
return (state.rooms[getDevice(state, ownProps).roomId] || {}).name;
2020-05-12 13:18:33 +00:00
case 'Hosts':
const hostRooms = state.hostRooms[ownProps.hostId];
2020-05-12 13:18:33 +00:00
if (!hostRooms) return '';
const room = hostRooms[getDevice(state, ownProps).roomId];
2020-05-12 13:18:33 +00:00
if (!room) return '';
return room.name;
2020-05-03 16:13:50 +00:00
default:
throw new Error(
2020-05-12 13:18:33 +00:00
`room name has no value in tab "${state.active.activeTab}"`,
2020-05-03 16:13:50 +00:00
);
2020-05-03 16:09:53 +00:00
}
}
export default function mapStateToProps(state, ownProps) {
return {
2020-05-04 09:13:19 +00:00
activeHost: state.active.activeHost,
2020-05-03 16:09:53 +00:00
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;
},
2020-05-04 09:13:19 +00:00
get disabled() {
return (
2020-05-12 13:18:33 +00:00
ownProps.tab === 'Hosts'
&& ['dimmableLight', 'light'].indexOf(getDevice(state, ownProps).kind)
=== -1
2020-05-04 09:13:19 +00:00
);
},
2020-05-03 16:09:53 +00:00
};
}