Fix badges not saved in view editor (#19971)

This commit is contained in:
Paul Bottein 2024-03-04 15:28:58 +01:00 committed by GitHub
parent b5bb6c6fe5
commit 88ccbcd883
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -23,7 +23,6 @@ import "../../../../components/ha-dialog";
import "../../../../components/ha-dialog-header";
import "../../../../components/ha-yaml-editor";
import type { HaYamlEditor } from "../../../../components/ha-yaml-editor";
import { LovelaceBadgeConfig } from "../../../../data/lovelace/config/badge";
import {
LovelaceViewConfig,
isStrategyView,
@ -61,8 +60,6 @@ export class HuiDialogEditView extends LitElement {
@state() private _config?: LovelaceViewConfig;
@state() private _badges?: LovelaceBadgeConfig[];
@state() private _saving = false;
@state() private _curTab?: string;
@ -88,7 +85,6 @@ export class HuiDialogEditView extends LitElement {
if (this._yamlMode && changedProperties.has("_yamlMode")) {
const viewConfig = {
...this._config,
badges: this._badges,
};
this._editor?.setValue(viewConfig);
}
@ -99,7 +95,6 @@ export class HuiDialogEditView extends LitElement {
if (this._params.viewIndex === undefined) {
this._config = {};
this._badges = [];
this._dirty = false;
return;
}
@ -108,19 +103,15 @@ export class HuiDialogEditView extends LitElement {
if (isStrategyView(view)) {
const { strategy, ...viewConfig } = view;
this._config = viewConfig;
this._badges = [];
return;
}
const { badges, ...viewConfig } = view;
this._config = viewConfig;
this._badges = badges ? processEditorEntities(badges) : [];
this._config = view;
}
public closeDialog(): void {
this._curTabIndex = 0;
this._params = undefined;
this._config = {};
this._badges = [];
this._yamlMode = false;
this._dirty = false;
fireEvent(this, "dialog-closed", { dialog: this.localName });
@ -166,7 +157,7 @@ export class HuiDialogEditView extends LitElement {
break;
case "tab-badges":
content = html`
${this._badges?.length
${this._config?.badges?.length
? html`
${VIEWS_NO_BADGE_SUPPORT.includes(this._type)
? html`
@ -178,7 +169,7 @@ export class HuiDialogEditView extends LitElement {
`
: nothing}
<div class="preview-badges">
${this._badges.map(
${this._config.badges.map(
(badgeConfig) => html`
<hui-badge-preview
.hass=${this.hass}
@ -191,7 +182,7 @@ export class HuiDialogEditView extends LitElement {
: nothing}
<hui-entity-editor
.hass=${this.hass}
.entities=${this._badges}
.entities=${this._config?.badges || []}
@entities-changed=${this._badgesChanged}
></hui-entity-editor>
`;
@ -420,9 +411,9 @@ export class HuiDialogEditView extends LitElement {
}
this._saving = true;
const viewConf: LovelaceViewConfig = {
const viewConf = {
...this._config,
badges: this._badges,
};
if (viewConf.type === SECTION_VIEW_LAYOUT && !viewConf.sections?.length) {
@ -487,10 +478,13 @@ export class HuiDialogEditView extends LitElement {
}
private _badgesChanged(ev: EntitiesEditorEvent): void {
if (!this._badges || !this.hass || !ev.detail || !ev.detail.entities) {
if (!this.hass || !ev.detail || !ev.detail.entities) {
return;
}
this._badges = processEditorEntities(ev.detail.entities);
this._config = {
...this._config,
badges: processEditorEntities(ev.detail.entities),
};
this._dirty = true;
}
@ -501,7 +495,6 @@ export class HuiDialogEditView extends LitElement {
}
const { badges, ...config } = ev.detail.value;
this._config = config;
this._badges = badges;
this._dirty = true;
}