Fixes for todo card (#18388)

This commit is contained in:
Bram Kragten 2023-10-24 22:53:54 +02:00 committed by GitHub
parent 3e6ab8b179
commit 1cb238ec2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 12 deletions

View File

@ -2,6 +2,7 @@ import { HomeAssistant, ServiceCallResponse } from "../types";
import { computeDomain } from "../common/entity/compute_domain"; import { computeDomain } from "../common/entity/compute_domain";
import { computeStateName } from "../common/entity/compute_state_name"; import { computeStateName } from "../common/entity/compute_state_name";
import { isUnavailableState } from "./entity"; import { isUnavailableState } from "./entity";
import { stringCompare } from "../common/string/compare";
export interface TodoList { export interface TodoList {
entity_id: string; entity_id: string;
@ -33,12 +34,12 @@ export const getTodoLists = (hass: HomeAssistant): TodoList[] =>
computeDomain(entityId) === "todo" && computeDomain(entityId) === "todo" &&
!isUnavailableState(hass.states[entityId].state) !isUnavailableState(hass.states[entityId].state)
) )
.sort()
.map((entityId) => ({ .map((entityId) => ({
...hass.states[entityId], ...hass.states[entityId],
entity_id: entityId, entity_id: entityId,
name: computeStateName(hass.states[entityId]), name: computeStateName(hass.states[entityId]),
})); }))
.sort((a, b) => stringCompare(a.name, b.name, hass.locale.language));
export interface TodoItems { export interface TodoItems {
items: TodoItem[]; items: TodoItem[];

View File

@ -73,7 +73,7 @@ export class HuiTodoListCard
@state() private _entityId?: string; @state() private _entityId?: string;
@state() private _items: Record<string, TodoItem> = {}; @state() private _items?: Record<string, TodoItem>;
@state() private _uncheckedItems?: TodoItem[]; @state() private _uncheckedItems?: TodoItem[];
@ -96,8 +96,6 @@ export class HuiTodoListCard
this._config = config; this._config = config;
this._entityId = config.entity; this._entityId = config.entity;
this._uncheckedItems = [];
this._checkedItems = [];
} }
protected checkConfig(config: TodoListCardConfig): void { protected checkConfig(config: TodoListCardConfig): void {
@ -112,13 +110,17 @@ export class HuiTodoListCard
} }
public willUpdate( public willUpdate(
_changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown> changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>
): void { ): void {
if (!this.hasUpdated) { if (!this.hasUpdated) {
if (!this._entityId) { if (!this._entityId) {
this._entityId = this.getEntityId(); this._entityId = this.getEntityId();
} }
this._fetchData(); this._fetchData();
} else if (changedProperties.has("_entityId") || !this._items) {
this._uncheckedItems = [];
this._checkedItems = [];
this._fetchData();
} }
} }
@ -338,7 +340,7 @@ export class HuiTodoListCard
} }
private _completeItem(ev): void { private _completeItem(ev): void {
const item = this._items[ev.target.itemId]; const item = this._items![ev.target.itemId];
updateItem(this.hass!, this._entityId!, { updateItem(this.hass!, this._entityId!, {
...item, ...item,
status: ev.target.checked status: ev.target.checked
@ -350,7 +352,7 @@ export class HuiTodoListCard
private _saveEdit(ev): void { private _saveEdit(ev): void {
// If name is not empty, update the item otherwise remove it // If name is not empty, update the item otherwise remove it
if (ev.target.value) { if (ev.target.value) {
const item = this._items[ev.target.itemId]; const item = this._items![ev.target.itemId];
updateItem(this.hass!, this._entityId!, { updateItem(this.hass!, this._entityId!, {
...item, ...item,
summary: ev.target.value, summary: ev.target.value,

View File

@ -14,7 +14,6 @@ import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one"; import memoizeOne from "memoize-one";
import { isComponentLoaded } from "../../common/config/is_component_loaded"; import { isComponentLoaded } from "../../common/config/is_component_loaded";
import { storage } from "../../common/decorators/storage"; import { storage } from "../../common/decorators/storage";
import { computeDomain } from "../../common/entity/compute_domain";
import { computeStateName } from "../../common/entity/compute_state_name"; import { computeStateName } from "../../common/entity/compute_state_name";
import "../../components/ha-button"; import "../../components/ha-button";
import "../../components/ha-icon-button"; import "../../components/ha-icon-button";
@ -87,9 +86,7 @@ class PanelTodo extends LitElement {
super.willUpdate(changedProperties); super.willUpdate(changedProperties);
if (!this.hasUpdated && !this._entityId) { if (!this.hasUpdated && !this._entityId) {
this._entityId = Object.keys(this.hass.states).find( this._entityId = getTodoLists(this.hass)[0]?.entity_id;
(entityId) => computeDomain(entityId) === "todo"
);
} else if (!this.hasUpdated) { } else if (!this.hasUpdated) {
this._createCard(); this._createCard();
} }