Skip to content

Commit

Permalink
fix initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
bramkragten committed Oct 19, 2023
1 parent d9dbbbf commit 6af5a05
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/panels/calendar/ha-panel-calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ class PanelCalendar extends LitElement {
}

private async _fetchEvents(
start: Date,
end: Date,
start: Date | undefined,
end: Date | undefined,
calendars: Calendar[]
): Promise<{ events: CalendarEvent[]; errors: string[] }> {
if (!calendars.length || !start || !end) {
Expand All @@ -184,6 +184,8 @@ class PanelCalendar extends LitElement {
}

private async _handleSelected(ev): Promise<void> {
const oldSelected = this._selectedCalendars;

const deselectedCalendars: Set<string> = new Set(
this._calendars.map((cal) => cal.entity_id)
);
Expand All @@ -192,13 +194,15 @@ class PanelCalendar extends LitElement {
}
this._deSelectedCalendars = [...deselectedCalendars];

if (ev.detail.diff.added.length) {
if (ev.detail.diff.added.length === 1) {
const cal = this._calendars[ev.detail.diff.added[0]];
const result = await this._fetchEvents(this._start, this._end, [cal]);
this._events = [...this._events, ...result.events];
this._handleErrors(result.errors);
if (!oldSelected.includes(cal)) {
const result = await this._fetchEvents(this._start, this._end, [cal]);
this._events = [...this._events, ...result.events];
this._handleErrors(result.errors);
}
}
if (ev.detail.diff.removed.length) {
if (ev.detail.diff.removed.length === 1) {
const cal = this._calendars[ev.detail.diff.removed[0]];
this._events = this._events.filter(
(event) => event.calendar !== cal.entity_id
Expand Down

0 comments on commit 6af5a05

Please sign in to comment.