Open History to see the last commit
1import { LitElement, html } from 'lit'
2import {
3 QueryClient,
4 QueryClientProvider,
5 createQueryController,
6} from '@tanstack/lit-query'
7import { resetTodoApi } from './services/todos/api'
8import { todosQueries } from './services/todos/queries'
9β
10resetTodoApi()
11β
12const queryClient = new QueryClient({
13 defaultOptions: {
14 queries: { retry: false },
15 },
16})
17β
18class BasicQueryProvider extends QueryClientProvider {
19 constructor() {
20 super()
21 this.client = queryClient
22 }
23β
24 protected override createRenderRoot(): HTMLElement | DocumentFragment {
25 return this
26 }
27}
28β
29customElements.define('basic-query-provider', BasicQueryProvider)
30β
31class BasicQueryExample extends LitElement {
32 private readonly todos = createQueryController(this, todosQueries.list())
33β
34 protected override createRenderRoot(): HTMLElement | DocumentFragment {
35 return this
36 }
37β
38 render() {
39 const query = this.todos()
40 return html`
41 <main>
42 <h1>Basic Query Example</h1>
43 <p data-testid="basic-query-status">
44 Status: <strong>${query.status}</strong>
45 </p>
46 <button
47 data-testid="basic-refetch"
48 @click=${() => this.todos.refetch()}
49 >
50 Refetch
51 </button>
52β
53 ${
54 query.isPending
55 ? html`<p data-testid="basic-loading">Loading...</p>`
56 : null
57 }
58 ${
59 query.isError
60 ? html`<p data-testid="basic-error">
61 Error: ${query.error.message}
62 </p>`
63 : null
64 }
65β
66 <ul data-testid="basic-todo-list">
67 ${(query.data?.items ?? []).map(
68 (todo) =>
69 html`<li data-testid="basic-todo-item">${todo.title}</li>`,
70 )}
71 </ul>
72 </main>
73 `
74 }
75}
76β
77customElements.define('basic-query-example', BasicQueryExample)
78β
79class BasicQueryRoot extends LitElement {
80 protected override createRenderRoot(): HTMLElement | DocumentFragment {
81 return this
82 }
83β
84 render() {
85 return html`
86 <basic-query-provider>
87 <basic-query-example></basic-query-example>
88 </basic-query-provider>
89 `
90 }
91}
92β
93customElements.define('basic-query-root', BasicQueryRoot)