Extract saving badge config from badge editor

This commit is contained in:
Paul Bottein 2025-02-25 10:30:42 +01:00
parent bb672d0272
commit 43374ef798
No known key found for this signature in database
5 changed files with 60 additions and 65 deletions

View File

@ -23,6 +23,7 @@ import type { LovelaceCardConfig } from "../../../data/lovelace/config/card";
import { haStyle } from "../../../resources/styles"; import { haStyle } from "../../../resources/styles";
import type { HomeAssistant } from "../../../types"; import type { HomeAssistant } from "../../../types";
import { showEditBadgeDialog } from "../editor/badge-editor/show-edit-badge-dialog"; import { showEditBadgeDialog } from "../editor/badge-editor/show-edit-badge-dialog";
import { addBadge } from "../editor/config-util";
import type { LovelaceCardPath } from "../editor/lovelace-path"; import type { LovelaceCardPath } from "../editor/lovelace-path";
import { import {
findLovelaceItems, findLovelaceItems,
@ -221,11 +222,19 @@ export class HuiBadgeEditMode extends LitElement {
const { cardIndex } = parseLovelaceCardPath(this.path!); const { cardIndex } = parseLovelaceCardPath(this.path!);
const containerPath = getLovelaceContainerPath(this.path!); const containerPath = getLovelaceContainerPath(this.path!);
const badgeConfig = ensureBadgeConfig(this._badges![cardIndex]); const badgeConfig = ensureBadgeConfig(this._badges![cardIndex]);
showEditBadgeDialog(this, { showEditBadgeDialog(this, {
lovelaceConfig: this.lovelace!.config, lovelaceConfig: this.lovelace!.config,
saveConfig: this.lovelace!.saveConfig, saveBadgeConfig: async (config) => {
path: containerPath, const newConfig = addBadge(
this.lovelace!.config,
containerPath,
config
);
await this.lovelace!.saveConfig(newConfig);
},
badgeConfig, badgeConfig,
isNew: true,
}); });
} }

View File

@ -24,6 +24,7 @@ import "./hui-badge-picker";
import type { CreateBadgeDialogParams } from "./show-create-badge-dialog"; import type { CreateBadgeDialogParams } from "./show-create-badge-dialog";
import { showEditBadgeDialog } from "./show-edit-badge-dialog"; import { showEditBadgeDialog } from "./show-edit-badge-dialog";
import { showSuggestBadgeDialog } from "./show-suggest-badge-dialog"; import { showSuggestBadgeDialog } from "./show-suggest-badge-dialog";
import { addBadge } from "../config-util";
declare global { declare global {
interface HASSDomEvents { interface HASSDomEvents {
@ -223,11 +224,22 @@ export class HuiCreateDialogBadge
} }
} }
const lovelaceConfig = this._params!.lovelaceConfig;
const containerPath = this._params!.path;
const saveConfig = this._params!.saveConfig;
showEditBadgeDialog(this, { showEditBadgeDialog(this, {
lovelaceConfig: this._params!.lovelaceConfig, lovelaceConfig,
saveConfig: this._params!.saveConfig, saveBadgeConfig: async (newBadgeConfig) => {
path: this._params!.path, const newConfig = addBadge(
lovelaceConfig,
containerPath,
newBadgeConfig
);
await saveConfig(newConfig);
},
badgeConfig: config, badgeConfig: config,
isNew: true,
}); });
this.closeDialog(); this.closeDialog();

View File

@ -11,8 +11,6 @@ import "../../../../components/ha-dialog";
import "../../../../components/ha-dialog-header"; import "../../../../components/ha-dialog-header";
import "../../../../components/ha-icon-button"; import "../../../../components/ha-icon-button";
import type { LovelaceBadgeConfig } from "../../../../data/lovelace/config/badge"; import type { LovelaceBadgeConfig } from "../../../../data/lovelace/config/badge";
import { ensureBadgeConfig } from "../../../../data/lovelace/config/badge";
import type { LovelaceViewConfig } from "../../../../data/lovelace/config/view";
import { import {
getCustomBadgeEntry, getCustomBadgeEntry,
isCustomType, isCustomType,
@ -22,13 +20,12 @@ import { showConfirmationDialog } from "../../../../dialogs/generic/show-dialog-
import type { HassDialog } from "../../../../dialogs/make-dialog-manager"; import type { HassDialog } from "../../../../dialogs/make-dialog-manager";
import { haStyleDialog } from "../../../../resources/styles"; import { haStyleDialog } from "../../../../resources/styles";
import type { HomeAssistant } from "../../../../types"; import type { HomeAssistant } from "../../../../types";
import { showToast } from "../../../../util/toast";
import { showSaveSuccessToast } from "../../../../util/toast-saved-success"; import { showSaveSuccessToast } from "../../../../util/toast-saved-success";
import "../../badges/hui-badge"; import "../../badges/hui-badge";
import "../../sections/hui-section"; import "../../sections/hui-section";
import { addBadge, replaceBadge } from "../config-util";
import { getBadgeDocumentationURL } from "../get-dashboard-documentation-url"; import { getBadgeDocumentationURL } from "../get-dashboard-documentation-url";
import type { ConfigChangedEvent } from "../hui-element-editor"; import type { ConfigChangedEvent } from "../hui-element-editor";
import { findLovelaceContainer } from "../lovelace-path";
import type { GUIModeChangedEvent } from "../types"; import type { GUIModeChangedEvent } from "../types";
import "./hui-badge-element-editor"; import "./hui-badge-element-editor";
import type { HuiBadgeElementEditor } from "./hui-badge-element-editor"; import type { HuiBadgeElementEditor } from "./hui-badge-element-editor";
@ -58,8 +55,6 @@ export class HuiDialogEditBadge
@state() private _badgeConfig?: LovelaceBadgeConfig; @state() private _badgeConfig?: LovelaceBadgeConfig;
@state() private _containerConfig!: LovelaceViewConfig;
@state() private _saving = false; @state() private _saving = false;
@state() private _error?: string; @state() private _error?: string;
@ -82,24 +77,7 @@ export class HuiDialogEditBadge
this._GUImode = true; this._GUImode = true;
this._guiModeAvailable = true; this._guiModeAvailable = true;
const containerConfig = findLovelaceContainer(
params.lovelaceConfig,
params.path
);
if ("strategy" in containerConfig) {
throw new Error("Can't edit strategy");
}
this._containerConfig = containerConfig;
if ("badgeConfig" in params) {
this._badgeConfig = params.badgeConfig; this._badgeConfig = params.badgeConfig;
this._dirty = true;
} else {
const badge = this._containerConfig.badges?.[params.badgeIndex];
this._badgeConfig = badge != null ? ensureBadgeConfig(badge) : badge;
}
this.large = false; this.large = false;
if (this._badgeConfig && !Object.isFrozen(this._badgeConfig)) { if (this._badgeConfig && !Object.isFrozen(this._badgeConfig)) {
@ -178,13 +156,6 @@ export class HuiDialogEditBadge
"ui.panel.lovelace.editor.edit_badge.typed_header", "ui.panel.lovelace.editor.edit_badge.typed_header",
{ type: badgeName } { type: badgeName }
); );
} else if (!this._badgeConfig) {
heading = this._containerConfig.title
? this.hass!.localize(
"ui.panel.lovelace.editor.edit_badge.pick_badge_view_title",
{ name: this._containerConfig.title }
)
: this.hass!.localize("ui.panel.lovelace.editor.edit_badge.pick_badge");
} else { } else {
heading = this.hass!.localize( heading = this.hass!.localize(
"ui.panel.lovelace.editor.edit_badge.header" "ui.panel.lovelace.editor.edit_badge.header"
@ -377,20 +348,18 @@ export class HuiDialogEditBadge
return; return;
} }
this._saving = true; this._saving = true;
const path = this._params!.path; try {
await this._params!.saveConfig( await this._params!.saveBadgeConfig(this._badgeConfig!);
"badgeConfig" in this._params!
? addBadge(this._params!.lovelaceConfig, path, this._badgeConfig!)
: replaceBadge(
this._params!.lovelaceConfig,
[...path, this._params!.badgeIndex],
this._badgeConfig!
)
);
this._saving = false; this._saving = false;
this._dirty = false; this._dirty = false;
showSaveSuccessToast(this, this.hass); showSaveSuccessToast(this, this.hass);
this.closeDialog(); this.closeDialog();
} catch (err: any) {
showToast(this, {
message: err.message,
});
this._saving = false;
}
} }
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {

View File

@ -1,20 +1,13 @@
import { fireEvent } from "../../../../common/dom/fire_event"; import { fireEvent } from "../../../../common/dom/fire_event";
import type { LovelaceBadgeConfig } from "../../../../data/lovelace/config/badge"; import type { LovelaceBadgeConfig } from "../../../../data/lovelace/config/badge";
import type { LovelaceConfig } from "../../../../data/lovelace/config/types"; import type { LovelaceConfig } from "../../../../data/lovelace/config/types";
import type { LovelaceContainerPath } from "../lovelace-path";
export type EditBadgeDialogParams = { export interface EditBadgeDialogParams {
lovelaceConfig: LovelaceConfig; lovelaceConfig: LovelaceConfig;
saveConfig: (config: LovelaceConfig) => void; saveBadgeConfig: (badge: LovelaceBadgeConfig) => void;
path: LovelaceContainerPath;
} & (
| {
badgeIndex: number;
}
| {
badgeConfig: LovelaceBadgeConfig; badgeConfig: LovelaceBadgeConfig;
} isNew?: boolean;
); }
export const importEditBadgeDialog = () => import("./hui-dialog-edit-badge"); export const importEditBadgeDialog = () => import("./hui-dialog-edit-badge");

View File

@ -302,11 +302,23 @@ export class HUIView extends ReactiveElement {
}); });
this._layoutElement.addEventListener("ll-edit-badge", (ev) => { this._layoutElement.addEventListener("ll-edit-badge", (ev) => {
const { cardIndex } = parseLovelaceCardPath(ev.detail.path); const { cardIndex } = parseLovelaceCardPath(ev.detail.path);
const viewConfig = this.lovelace!.config.views[this.index];
if (isStrategyView(viewConfig)) {
return;
}
const badgeConfig = ensureBadgeConfig(viewConfig.badges![cardIndex]);
showEditBadgeDialog(this, { showEditBadgeDialog(this, {
lovelaceConfig: this.lovelace.config, lovelaceConfig: this.lovelace.config,
saveConfig: this.lovelace.saveConfig, saveBadgeConfig: async (newBadgeConfig) => {
path: [this.index], const newConfig = replaceCard(
badgeIndex: cardIndex, this.lovelace!.config,
[this.index, cardIndex],
newBadgeConfig
);
await this.lovelace.saveConfig(newConfig);
},
badgeConfig,
}); });
}); });
this._layoutElement.addEventListener("ll-delete-badge", async (ev) => { this._layoutElement.addEventListener("ll-delete-badge", async (ev) => {