Display category dialog in category picker

This commit is contained in:
Paul Bottein 2024-03-28 13:15:48 +01:00
parent 2978ca13c5
commit 8ec8dc160f
No known key found for this signature in database
4 changed files with 49 additions and 68 deletions

View File

@ -12,8 +12,10 @@ import { customElement, property, state } from "lit/decorators";
import { fireEvent } from "../common/dom/fire_event"; import { fireEvent } from "../common/dom/fire_event";
import { import {
CategoryRegistryEntry, CategoryRegistryEntry,
createCategoryRegistryEntry,
deleteCategoryRegistryEntry, deleteCategoryRegistryEntry,
subscribeCategoryRegistry, subscribeCategoryRegistry,
updateCategoryRegistryEntry,
} from "../data/category_registry"; } from "../data/category_registry";
import { showConfirmationDialog } from "../dialogs/generic/show-dialog-box"; import { showConfirmationDialog } from "../dialogs/generic/show-dialog-box";
import { SubscribeMixin } from "../mixins/subscribe-mixin"; import { SubscribeMixin } from "../mixins/subscribe-mixin";
@ -174,6 +176,8 @@ export class HaFilterCategories extends SubscribeMixin(LitElement) {
showCategoryRegistryDetailDialog(this, { showCategoryRegistryDetailDialog(this, {
scope: this.scope!, scope: this.scope!,
entry: this._categories.find((cat) => cat.category_id === id), entry: this._categories.find((cat) => cat.category_id === id),
updateEntry: (updates) =>
updateCategoryRegistryEntry(this.hass, this.scope!, id, updates),
}); });
} }
@ -206,7 +210,11 @@ export class HaFilterCategories extends SubscribeMixin(LitElement) {
if (!this.scope) { if (!this.scope) {
return; return;
} }
showCategoryRegistryDetailDialog(this, { scope: this.scope }); showCategoryRegistryDetailDialog(this, {
scope: this.scope,
createEntry: (values) =>
createCategoryRegistryEntry(this.hass, this.scope!, values),
});
} }
private _expandedWillChange(ev) { private _expandedWillChange(ev) {

View File

@ -1,6 +1,6 @@
import "@material/mwc-button"; import "@material/mwc-button";
import { css, CSSResultGroup, html, LitElement, nothing } from "lit"; import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
import { property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import { fireEvent } from "../../../common/dom/fire_event"; import { fireEvent } from "../../../common/dom/fire_event";
import "../../../components/ha-alert"; import "../../../components/ha-alert";
import { createCloseHeading } from "../../../components/ha-dialog"; import { createCloseHeading } from "../../../components/ha-dialog";
@ -8,14 +8,14 @@ import "../../../components/ha-icon-picker";
import "../../../components/ha-settings-row"; import "../../../components/ha-settings-row";
import "../../../components/ha-textfield"; import "../../../components/ha-textfield";
import { import {
CategoryRegistryEntry,
CategoryRegistryEntryMutableParams, CategoryRegistryEntryMutableParams,
createCategoryRegistryEntry,
updateCategoryRegistryEntry,
} from "../../../data/category_registry"; } from "../../../data/category_registry";
import { haStyleDialog } from "../../../resources/styles"; import { haStyleDialog } from "../../../resources/styles";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
import { CategoryRegistryDetailDialogParams } from "./show-dialog-category-registry-detail"; import { CategoryRegistryDetailDialogParams } from "./show-dialog-category-registry-detail";
@customElement("dialog-category-registry-detail")
class DialogCategoryDetail extends LitElement { class DialogCategoryDetail extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public hass!: HomeAssistant;
@ -123,24 +123,16 @@ class DialogCategoryDetail extends LitElement {
private async _updateEntry() { private async _updateEntry() {
const create = !this._params!.entry; const create = !this._params!.entry;
this._submitting = true; this._submitting = true;
let newValue: CategoryRegistryEntry | undefined;
try { try {
const values: CategoryRegistryEntryMutableParams = { const values: CategoryRegistryEntryMutableParams = {
name: this._name.trim(), name: this._name.trim(),
icon: this._icon || (create ? undefined : null), icon: this._icon || (create ? undefined : null),
}; };
if (create) { if (create) {
await createCategoryRegistryEntry( newValue = await this._params!.createEntry!(values);
this.hass,
this._params!.scope,
values
);
} else { } else {
await updateCategoryRegistryEntry( newValue = await this._params!.updateEntry!(values);
this.hass,
this._params!.scope,
this._params!.entry!.category_id,
values
);
} }
this.closeDialog(); this.closeDialog();
} catch (err: any) { } catch (err: any) {
@ -150,6 +142,7 @@ class DialogCategoryDetail extends LitElement {
} finally { } finally {
this._submitting = false; this._submitting = false;
} }
return newValue;
} }
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {
@ -171,5 +164,3 @@ declare global {
"dialog-category-registry-detail": DialogCategoryDetail; "dialog-category-registry-detail": DialogCategoryDetail;
} }
} }
customElements.define("dialog-category-registry-detail", DialogCategoryDetail);

View File

@ -20,12 +20,9 @@ import {
createCategoryRegistryEntry, createCategoryRegistryEntry,
subscribeCategoryRegistry, subscribeCategoryRegistry,
} from "../../../data/category_registry"; } from "../../../data/category_registry";
import {
showAlertDialog,
showPromptDialog,
} from "../../../dialogs/generic/show-dialog-box";
import { SubscribeMixin } from "../../../mixins/subscribe-mixin"; import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
import { HomeAssistant, ValueChangedEvent } from "../../../types"; import { HomeAssistant, ValueChangedEvent } from "../../../types";
import { showCategoryRegistryDetailDialog } from "./show-dialog-category-registry-detail";
type ScorableCategoryRegistryEntry = ScorableTextItem & CategoryRegistryEntry; type ScorableCategoryRegistryEntry = ScorableTextItem & CategoryRegistryEntry;
@ -216,54 +213,29 @@ export class HaCategoryPicker extends SubscribeMixin(LitElement) {
} }
(ev.target as any).value = this._value; (ev.target as any).value = this._value;
showPromptDialog(this, {
title: this.hass.localize( showCategoryRegistryDetailDialog(this, {
"ui.components.category-picker.add_dialog.title" scope: this.scope!,
), createEntry: async (values) => {
text: this.hass.localize("ui.components.category-picker.add_dialog.text"), const category = await createCategoryRegistryEntry(
confirmText: this.hass.localize( this.hass,
"ui.components.category-picker.add_dialog.add" this.scope!,
), values
inputLabel: this.hass.localize( );
"ui.components.category-picker.add_dialog.name" this._categories = [...this._categories!, category];
), this.comboBox.filteredItems = this._getCategories(
defaultValue: this._categories,
newValue === "add_new_suggestion" ? this._suggestion : undefined, this.noAdd
confirm: async (name) => { );
if (!name) { await this.updateComplete;
return; await this.comboBox.updateComplete;
} this._setValue(category.category_id);
try { return category;
const category = await createCategoryRegistryEntry(
this.hass,
this.scope!,
{
name,
}
);
this._categories = [...this._categories!, category];
this.comboBox.filteredItems = this._getCategories(
this._categories,
this.noAdd
);
await this.updateComplete;
await this.comboBox.updateComplete;
this._setValue(category.category_id);
} catch (err: any) {
showAlertDialog(this, {
title: this.hass.localize(
"ui.components.category-picker.add_dialog.failed_create_category"
),
text: err.message,
});
}
},
cancel: () => {
this._setValue(undefined);
this._suggestion = undefined;
this.comboBox.setInputValue("");
}, },
}); });
this._suggestion = undefined;
this.comboBox.setInputValue("");
} }
private _setValue(value?: string) { private _setValue(value?: string) {

View File

@ -1,9 +1,19 @@
import { fireEvent } from "../../../common/dom/fire_event"; import { fireEvent } from "../../../common/dom/fire_event";
import { CategoryRegistryEntry } from "../../../data/category_registry"; import {
CategoryRegistryEntry,
CategoryRegistryEntryMutableParams,
} from "../../../data/category_registry";
export interface CategoryRegistryDetailDialogParams { export interface CategoryRegistryDetailDialogParams {
entry?: CategoryRegistryEntry; entry?: CategoryRegistryEntry;
scope: string; scope: string;
suggestedName?: string;
createEntry?: (
values: CategoryRegistryEntryMutableParams
) => Promise<CategoryRegistryEntry>;
updateEntry?: (
updates: Partial<CategoryRegistryEntryMutableParams>
) => Promise<CategoryRegistryEntry>;
} }
export const loadCategoryRegistryDetailDialog = () => export const loadCategoryRegistryDetailDialog = () =>