Skip to content

Commit

Permalink
Refactor dashboard devices-in-room box (#1138)
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato authored Apr 23, 2021
1 parent 4cf4752 commit 22dc3e9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 115 deletions.
79 changes: 18 additions & 61 deletions front/src/components/boxs/device-in-room/DeviceRow.jsx
Original file line number Diff line number Diff line change
@@ -1,75 +1,32 @@
import { createElement } from 'preact';
import { DEVICE_FEATURE_TYPES } from '../../../../../server/utils/constants';

import BinaryDeviceFeature from './device-features/BinaryDeviceFeature';
import ColorDeviceFeature from './device-features/ColorDeviceFeature';
import SensorDeviceFeature from './device-features/SensorDeviceFeature';
import MultilevelDeviceFeature from './device-features/MultiLevelDeviceFeature';
import BrightnessDeviceFeature from './device-features/BrightnessDeviceFeature';
import MultiLevelDeviceFeature from './device-features/MultiLevelDeviceFeature';

const ROW_TYPE_BY_FEATURE_TYPE = {
[DEVICE_FEATURE_TYPES.LIGHT.BINARY]: BinaryDeviceFeature,
[DEVICE_FEATURE_TYPES.LIGHT.COLOR]: ColorDeviceFeature,
[DEVICE_FEATURE_TYPES.SWITCH.DIMMER]: MultiLevelDeviceFeature,
[DEVICE_FEATURE_TYPES.LIGHT.BRIGHTNESS]: MultiLevelDeviceFeature
};

const DeviceRow = ({ children, ...props }) => {
// if device is a sensor, we display the sensor deviceFeature
if (props.deviceFeature.read_only) {
return <SensorDeviceFeature user={props.user} deviceFeature={props.deviceFeature} />;
}

// else, it's not a sensor
// if it's a binary
if (props.deviceFeature.type === 'binary') {
return (
<BinaryDeviceFeature
x={props.x}
y={props.y}
device={props.device}
deviceFeature={props.deviceFeature}
roomIndex={props.roomIndex}
deviceIndex={props.deviceIndex}
deviceFeatureIndex={props.deviceFeatureIndex}
updateValue={props.updateValue}
/>
);
}
if (props.deviceFeature.type === 'color') {
return (
<ColorDeviceFeature
x={props.x}
y={props.y}
device={props.device}
deviceFeature={props.deviceFeature}
roomIndex={props.roomIndex}
deviceIndex={props.deviceIndex}
deviceFeatureIndex={props.deviceFeatureIndex}
updateValue={props.updateValue}
/>
);
}
if (props.deviceFeature.type === 'dimmer') {
return (
<MultilevelDeviceFeature
x={props.x}
y={props.y}
device={props.device}
deviceFeature={props.deviceFeature}
roomIndex={props.roomIndex}
deviceIndex={props.deviceIndex}
deviceFeatureIndex={props.deviceFeatureIndex}
updateValue={props.updateValue}
/>
);
}
if (props.deviceFeature.type === 'brightness') {
return (
<BrightnessDeviceFeature
x={props.x}
y={props.y}
device={props.device}
deviceFeature={props.deviceFeature}
roomIndex={props.roomIndex}
deviceIndex={props.deviceIndex}
deviceFeatureIndex={props.deviceFeatureIndex}
updateValue={props.updateValue}
/>
);
const elementType = ROW_TYPE_BY_FEATURE_TYPE[props.deviceFeature.type];

if (!elementType) {
// if no related components, we return nothing
return null;
}
// if not, we return nothing
return null;

return createElement(elementType, props);
};

export default DeviceRow;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Text } from 'preact-i18n';
import get from 'get-value';

import { getDeviceName } from './utils';
import { DeviceFeatureCategoriesIcon } from '../../../../utils/consts';

const MultiLevelDeviceType = ({ children, ...props }) => {
function updateValue(e) {
Expand All @@ -17,17 +20,15 @@ const MultiLevelDeviceType = ({ children, ...props }) => {
return (
<tr>
<td>
<i class="fe fe-toggle-right" />
<i
class={`fe fe-${get(
DeviceFeatureCategoriesIcon,
`${props.deviceFeature.category}.${props.deviceFeature.type}`,
{ default: 'arrow-right' }
)}`}
/>
</td>
{props.deviceFeature.deviceFeatureName && <td>{props.deviceFeature.deviceFeatureName}</td>}
{!props.deviceFeature.deviceFeatureName && (
<td>
<Text
id="dashboard.boxes.devicesInRoom.deviceTitle"
fields={{ name: props.deviceFeature.name, type: props.deviceFeature.type }}
/>
</td>
)}
<td>{getDeviceName(props.device, props.deviceFeature)}</td>

<td class="text-right" style="padding-top: 0px; padding-bottom: 0px">
<div class="col">
Expand Down
12 changes: 12 additions & 0 deletions front/src/config/demo.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
"tv-lamp-color",
"tv-lamp-brightness",
"mqtt-living-room-switch",
"mqtt-living-room-dimmer",
"mqtt-living-room-temp"
]
}
Expand Down Expand Up @@ -337,6 +338,17 @@
"last_value": 1,
"last_value_changed": "2019-02-12 07:49:07.556 +00:00"
},
{
"name": "Main Dimmer",
"selector": "mqtt-living-room-dimmer",
"category": "switch",
"type": "dimmer",
"min": 0,
"max": 100,
"read_only": false,
"last_value": 17,
"last_value_changed": "2019-02-12 07:49:07.556 +00:00"
},
{
"name": "Window Temp",
"selector": "mqtt-living-room-temp",
Expand Down

0 comments on commit 22dc3e9

Please sign in to comment.