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

Fixes for todo card #18388

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions src/data/todo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HomeAssistant, ServiceCallResponse } from "../types";
import { computeDomain } from "../common/entity/compute_domain";
import { computeStateName } from "../common/entity/compute_state_name";
import { isUnavailableState } from "./entity";
import { stringCompare } from "../common/string/compare";

export interface TodoList {
entity_id: string;
Expand Down Expand Up @@ -33,12 +34,12 @@ export const getTodoLists = (hass: HomeAssistant): TodoList[] =>
computeDomain(entityId) === "todo" &&
!isUnavailableState(hass.states[entityId].state)
)
.sort()
.map((entityId) => ({
...hass.states[entityId],
entity_id: entityId,
name: computeStateName(hass.states[entityId]),
}));
}))
.sort((a, b) => stringCompare(a.name, b.name, hass.locale.language));

export interface TodoItems {
items: TodoItem[];
Expand Down
14 changes: 8 additions & 6 deletions src/panels/lovelace/cards/hui-todo-list-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class HuiTodoListCard

@state() private _entityId?: string;

@state() private _items: Record<string, TodoItem> = {};
@state() private _items?: Record<string, TodoItem>;

@state() private _uncheckedItems?: TodoItem[];

Expand All @@ -96,8 +96,6 @@ export class HuiTodoListCard

this._config = config;
this._entityId = config.entity;
this._uncheckedItems = [];
this._checkedItems = [];
}

protected checkConfig(config: TodoListCardConfig): void {
Expand All @@ -112,13 +110,17 @@ export class HuiTodoListCard
}

public willUpdate(
_changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>
changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>
): void {
if (!this.hasUpdated) {
if (!this._entityId) {
this._entityId = this.getEntityId();
}
this._fetchData();
} else if (changedProperties.has("_entityId") || !this._items) {
this._uncheckedItems = [];
this._checkedItems = [];
this._fetchData();
}
}

Expand Down Expand Up @@ -338,7 +340,7 @@ export class HuiTodoListCard
}

private _completeItem(ev): void {
const item = this._items[ev.target.itemId];
const item = this._items![ev.target.itemId];
updateItem(this.hass!, this._entityId!, {
...item,
status: ev.target.checked
Expand All @@ -350,7 +352,7 @@ export class HuiTodoListCard
private _saveEdit(ev): void {
// If name is not empty, update the item otherwise remove it
if (ev.target.value) {
const item = this._items[ev.target.itemId];
const item = this._items![ev.target.itemId];
updateItem(this.hass!, this._entityId!, {
...item,
summary: ev.target.value,
Expand Down
5 changes: 1 addition & 4 deletions src/panels/todo/ha-panel-todo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import { isComponentLoaded } from "../../common/config/is_component_loaded";
import { storage } from "../../common/decorators/storage";
import { computeDomain } from "../../common/entity/compute_domain";
import { computeStateName } from "../../common/entity/compute_state_name";
import "../../components/ha-button";
import "../../components/ha-icon-button";
Expand Down Expand Up @@ -87,9 +86,7 @@ class PanelTodo extends LitElement {
super.willUpdate(changedProperties);

if (!this.hasUpdated && !this._entityId) {
this._entityId = Object.keys(this.hass.states).find(
(entityId) => computeDomain(entityId) === "todo"
);
this._entityId = getTodoLists(this.hass)[0]?.entity_id;
} else if (!this.hasUpdated) {
this._createCard();
}
Expand Down