Open History to see the last commit
1import { LitElement, html } from 'lit'
2import {
3 QueryClient,
4 QueryClientProvider,
5 createMutationController,
6 createQueryController,
7} from '@tanstack/lit-query'
8import { addTodoOnServer, fetchTodosFromServer, resetTodoApi } from './todoApi'
9import type { TodosResponse } from './todoApi'
10β
11resetTodoApi()
12β
13const queryClient = new QueryClient({
14 defaultOptions: {
15 queries: { retry: false },
16 mutations: { retry: false },
17 },
18})
19β
20class MutationExampleProvider extends QueryClientProvider {
21 constructor() {
22 super()
23 this.client = queryClient
24 }
25β
26 protected override createRenderRoot(): HTMLElement | DocumentFragment {
27 return this
28 }
29}
30β
31customElements.define('mutation-example-provider', MutationExampleProvider)
32β
33class MutationExample extends LitElement {
34 static properties = {
35 nextTitle: { state: true },
36 }
37β
38 private nextTitle = 'Created from mutation example'
39β
40 private readonly todos = createQueryController(this, {
41 queryKey: ['todos'],
42 queryFn: fetchTodosFromServer,
43 })
44β
45 private readonly addTodo = createMutationController(this, {
46 mutationKey: ['create-todo'],
47 mutationFn: addTodoOnServer,
48 onSuccess: (created) => {
49 queryClient.setQueryData<TodosResponse>(['todos'], (existing) => {
50 if (!existing) {
51 return {
52 items: [created],
53 requestCount: 0,
54 source: 'cache',
55 }
56 }
57β
58 return {
59 items: [...existing.items, created],
60 requestCount: existing.requestCount,
61 source: 'cache',
62 }
63 })
64 },
65 })
66β
67 protected override createRenderRoot(): HTMLElement | DocumentFragment {
68 return this
69 }
70β
71 private onInput(event: Event): void {
72 const target = event.target as HTMLInputElement
73 this.nextTitle = target.value
74 }
75β
76 private submit(): void {
77 const title = this.nextTitle.trim()
78 if (!title) return
79 this.addTodo.mutate(title)
80 this.nextTitle = ''
81 }
82β
83 render() {
84 const query = this.todos()
85 const mutation = this.addTodo()
86 const items = query.data?.items ?? []
87β
88 return html`
89 <main>
90 <h1>Mutation Example</h1>
91 <p data-testid="mutation-query-status">
92 Query: <strong>${query.status}</strong>
93 </p>
94 <p data-testid="mutation-mutation-status">
95 Mutation: <strong>${mutation.status}</strong>
96 </p>
97β
98 <label for="title">Todo title</label>
99 <input
100 id="title"
101 data-testid="mutation-input"
102 .value=${this.nextTitle}
103 @input=${this.onInput}
104 />
105 <button data-testid="mutation-add" @click=${this.submit}>
106 Add Todo
107 </button>
108β
109 <ul data-testid="mutation-todo-list">
110 ${items.map(
111 (todo) =>
112 html`<li data-testid="mutation-todo-item">${todo.title}</li>`,
113 )}
114 </ul>
115 </main>
116 `
117 }
118}
119β
120customElements.define('mutation-example', MutationExample)
121β
122class MutationExampleRoot extends LitElement {
123 protected override createRenderRoot(): HTMLElement | DocumentFragment {
124 return this
125 }
126β
127 render() {
128 return html`
129 <mutation-example-provider>
130 <mutation-example></mutation-example>
131 </mutation-example-provider>
132 `
133 }
134}
135β
136customElements.define('mutation-example-root', MutationExampleRoot)