Skip to content

Commit

Permalink
Fix: Device label in Chart & Device widget can be empty (#2127)
Browse files Browse the repository at this point in the history
  • Loading branch information
Terdious authored Oct 11, 2024
1 parent 7fce024 commit e27288e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 139 deletions.
12 changes: 7 additions & 5 deletions front/src/components/boxs/chart/EditChart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import get from 'get-value';
import BaseEditBox from '../baseEditBox';
import Chart from './Chart';
import { getDeviceFeatureName } from '../../../utils/device';
import { DeviceListWithDragAndDrop } from './DeviceListWithDragAndDrop';
import { DeviceListWithDragAndDrop } from '../../drag-and-drop/DeviceListWithDragAndDrop';
import { DEVICE_FEATURE_TYPES } from '../../../../../server/utils/constants';
import withIntlAsProp from '../../../utils/withIntlAsProp';
import { DEFAULT_COLORS, DEFAULT_COLORS_NAME } from './ApexChartComponent';
Expand Down Expand Up @@ -129,9 +129,8 @@ class EditChart extends Component {

refreshDeviceFeaturesNames = () => {
const newDeviceFeatureNames = this.state.selectedDeviceFeaturesOptions.map(o => {
return o.new_label !== undefined ? o.new_label : o.label;
return o.new_label !== undefined && o.new_label !== '' ? o.new_label : o.label;
});

const newDeviceFeature = this.state.selectedDeviceFeaturesOptions.map(o => {
return o.value;
});
Expand Down Expand Up @@ -213,7 +212,10 @@ class EditChart extends Component {
}
});
await this.setState(newState);
this.refreshDeviceFeaturesNames();

if (name !== '') {
this.refreshDeviceFeaturesNames();
}
};

getSelectedDeviceFeaturesAndOptions = (devices, chartType = this.state.chart_type) => {
Expand Down Expand Up @@ -342,7 +344,7 @@ class EditChart extends Component {
}
});
await this.setState(newStateWithoutElement);
this.refreshDeviceFeaturesNames();
await this.refreshDeviceFeaturesNames();
this.refreshDeviceUnitAndChartType(this.state.selectedDeviceFeaturesOptions);
};

Expand Down
16 changes: 0 additions & 16 deletions front/src/components/boxs/chart/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,4 @@
padding-left: 1.5rem;
padding-right: 1.5rem;
text-align: justify;
}
.deviceListDragAndDrop {
cursor: 'pointer';
user-select: 'none';
}

.deviceListDragAndDropDragging {
opacity: 0.5;
}

.deviceListDragAndDropActive {
background-color: #ecf0f1;
}

.deviceListRemoveButton {
z-index: 0;
}

This file was deleted.

9 changes: 6 additions & 3 deletions front/src/components/boxs/device-in-room/EditDevices.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Select from 'react-select';
import update from 'immutability-helper';
import BaseEditBox from '../baseEditBox';
import { getDeviceFeatureName } from '../../../utils/device';
import { DeviceListWithDragAndDrop } from './DeviceListWithDragAndDrop';
import { DeviceListWithDragAndDrop } from '../../drag-and-drop/DeviceListWithDragAndDrop';
import withIntlAsProp from '../../../utils/withIntlAsProp';
import SUPPORTED_FEATURE_TYPES from './SupportedFeatureTypes';

Expand All @@ -24,7 +24,7 @@ class EditDevices extends Component {

refreshDeviceFeaturesNames = () => {
const newDeviceFeatureNames = this.state.selectedDeviceFeaturesOptions.map(o => {
return o.new_label !== undefined ? o.new_label : o.label;
return o.new_label !== undefined && o.new_label !== '' ? o.new_label : o.label;
});
const newDeviceFeature = this.state.selectedDeviceFeaturesOptions.map(o => {
return o.value;
Expand Down Expand Up @@ -62,7 +62,10 @@ class EditDevices extends Component {
}
});
await this.setState(newState);
this.refreshDeviceFeaturesNames();

if (name !== '') {
this.refreshDeviceFeaturesNames();
}
};

getSelectedDeviceFeaturesAndOptions = devices => {
Expand Down
12 changes: 0 additions & 12 deletions front/src/components/boxs/device-in-room/style.css

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DndProvider, useDrag, useDrop } from 'react-dnd';
import { TouchBackend } from 'react-dnd-touch-backend';
import { HTML5Backend } from 'react-dnd-html5-backend';
import { useRef } from 'preact/hooks';
import { useRef, useState } from 'preact/hooks';
import cx from 'classnames';
import style from './style.css';

Expand Down Expand Up @@ -35,10 +35,13 @@ const DeviceRow = ({ selectedDeviceFeature, moveDevice, index, removeDevice, upd
removeDevice(index);
};

const [isEmpty, setIsEmpty] = useState(selectedDeviceFeature.new_label === '');
const updateThisDeviceFeatureName = e => {
updateDeviceFeatureName(index, e.target.value);
setIsEmpty(e.target.value === '');
};

const emptyFieldAutogenerated = isEmpty && selectedDeviceFeature.new_label !== '';
return (
<div class="mb-1">
<label class="form-label">{selectedDeviceFeature.label}</label>
Expand All @@ -55,13 +58,12 @@ const DeviceRow = ({ selectedDeviceFeature, moveDevice, index, removeDevice, upd
<input
type="text"
class={cx('form-control', {
[style.deviceListDragAndDropEmpty]: isEmpty && !emptyFieldAutogenerated,
[style.deviceListDragAndDropEmptyAutoGenerated]: emptyFieldAutogenerated,
[style.deviceListDragAndDropActive]: isActive
})}
value={
selectedDeviceFeature.new_label !== undefined
? selectedDeviceFeature.new_label
: selectedDeviceFeature.label
}
value={selectedDeviceFeature.new_label || ''}
placeholder={selectedDeviceFeature.label}
onChange={updateThisDeviceFeatureName}
/>
<div class={cx('input-group-append', style.deviceListRemoveButton)}>
Expand Down
30 changes: 30 additions & 0 deletions front/src/components/drag-and-drop/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,33 @@
height: 20%;
width: 100%;
}

.deviceListDragAndDrop {
cursor: 'pointer';
user-select: 'none';
}

.deviceListDragAndDropDragging {
opacity: 0.5;
}

.deviceListDragAndDropActive {
background-color: #ecf0f1;
}

.deviceListDragAndDropEmpty {
border: 1px solid #e90a0a;
}

.deviceListDragAndDropEmptyAutoGenerated {
border: 1px dashed #e90a0a;
}

.deviceListDragAndDropEmpty:focus {
border: 1px solid #e90a0a;
box-shadow: 0 0 5px #e90a0a;
}

.deviceListRemoveButton {
z-index: 0;
}

0 comments on commit e27288e

Please sign in to comment.