Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chart: color cannot be null, fix bug on edit, move & remove of device_feature #2201

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions front/src/components/boxs/chart/EditChart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,20 @@ class EditChart extends Component {
const colors = this.props.box.colors || [];
if (value) {
colors[i] = value;
} else {
colors[i] = null;
}
const atLeastOneColor = colors.some(Boolean);
this.props.updateBoxConfig(this.props.x, this.props.y, { colors: atLeastOneColor ? colors : undefined });
// Make sure all colors are filled with a defaut color
for (let y = 0; y < this.state.selectedDeviceFeaturesOptions.length; y += 1) {
// if no color is filled, pick a default color in array
if (colors[y] === null || colors[y] === undefined) {
colors[y] = DEFAULT_COLORS[y];
// if we are outside of the default color array,
// Take the first default color
if (!colors[y]) {
colors[y] = DEFAULT_COLORS[0];
}
}
}
this.props.updateBoxConfig(this.props.x, this.props.y, { colors });
};

updateDisplayAxes = e => {
Expand Down Expand Up @@ -321,6 +330,7 @@ class EditChart extends Component {
};

moveDevice = async (currentIndex, newIndex) => {
// Move element
const element = this.state.selectedDeviceFeaturesOptions[currentIndex];

const newStateWithoutElement = update(this.state, {
Expand All @@ -335,6 +345,19 @@ class EditChart extends Component {
});
await this.setState(newState);
this.refreshDeviceFeaturesNames();
// Move color if needed
if (this.props.box.colors && this.props.box.colors[currentIndex]) {
const currentColor = this.props.box.colors[currentIndex];
const newArrayWithoutOldColor = update(this.props.box.colors, {
$splice: [[currentIndex, 1]]
});
const newColorsArray = update(newArrayWithoutOldColor, {
$splice: [[newIndex, 0, currentColor]]
});
this.props.updateBoxConfig(this.props.x, this.props.y, {
colors: newColorsArray
});
}
};

removeDevice = async index => {
Expand All @@ -343,6 +366,13 @@ class EditChart extends Component {
$splice: [[index, 1]]
}
});
// Update color array
if (this.props.box.colors) {
const newColors = update(this.props.box.colors, {
$splice: [[index, 1]]
});
this.props.updateBoxConfig(this.props.x, this.props.y, { colors: newColors });
}
await this.setState(newStateWithoutElement);
await this.refreshDeviceFeaturesNames();
this.refreshDeviceUnitAndChartType(this.state.selectedDeviceFeaturesOptions);
Expand Down
Loading