Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

todo-list.ts

90 lines2.5 KB

1import { createQueryController } from '@tanstack/lit-query'
2import { LitElement, html } from 'lit'
3import { todosQueries } from './services/todos/queries'
4​
5class TodoList extends LitElement {
6 static properties = {
7 initialFilter: { attribute: false },
8 filter: { state: true },
9 }
10​
11 initialFilter = ''
12 private filter: string | undefined
13​
14 private readonly todos = createQueryController(this, () =>
15 todosQueries.list(this.filter ?? this.initialFilter),
16 )
17​
18 protected override createRenderRoot(): HTMLElement | DocumentFragment {
19 return this
20 }
21​
22 private setEditingIndex(editingIndex: number | null) {
23 this.dispatchEvent(
24 new CustomEvent('set-editing-index', { detail: editingIndex }),
25 )
26 }
27​
28 render() {
29 const { status, data, isFetching, error, failureCount, refetch } =
30 this.todos()
31​
32 return html`
33 <div>
34 <div>
35 <label>
36 Filter:
37 <input
38 .value=${this.filter ?? this.initialFilter}
39 @input=${(event: Event) =>
40 (this.filter = (event.currentTarget as HTMLInputElement).value)}
41 />
42 </label>
43 </div>
44 ${
45 status === 'pending'
46 ? html`<span>Loading... (Attempt: ${failureCount + 1})</span>`
47 : status === 'error'
48 ? html`
49 <span>
50 Error: ${error.message}
51 <br />
52 <button type="button" @click=${() => refetch()}>
53 Retry
54 </button>
55 </span>
56 `
57 : html`
58 <ul>
59 ${data.map(
60 (todo) => html`
61 <li>
62 ${todo.name}
63 <button
64 type="button"
65 @click=${() => this.setEditingIndex(todo.id)}
66 >
67 Edit
68 </button>
69 </li>
70 `,
71 )}
72 </ul>
73 <div>
74 ${
75 isFetching
76 ? html`<span>
77 Background Refreshing... (Attempt:
78 ${failureCount + 1})
79 </span>`
80 : html`<span>&nbsp;</span>`
81 }
82 </div>
83 `
84 }
85 </div>
86 `
87 }
89​
90customElements.define('todo-list', TodoList)