Open History to see the last commit
1import {
2 createMutationController,
3 createQueryController,
4} from '@tanstack/lit-query'
5import { LitElement, html } from 'lit'
6import { todosMutations, todosQueries } from './services/todos/queries'
7import type { Todo } from './services/todos/types'
8β
9class EditTodo extends LitElement {
10 static properties = {
11 editingIndex: { attribute: false },
12 draft: { state: true },
13 }
14β
15 editingIndex = 0
16 private draft: Partial<Omit<Todo, 'id'>> = {}
17β
18 private readonly todo = createQueryController(this, () =>
19 todosQueries.detail(this.editingIndex),
20 )
21β
22 private readonly saveMutation = createMutationController(this, {
23 ...todosMutations.update(),
24 onSuccess: (todo, _variables, _onMutateResult, { client }) => {
25 // Update `todos` and the individual todo queries when this mutation succeeds
26 client.invalidateQueries({ queryKey: todosQueries.lists() })
27 client.setQueryData(todosQueries.detail(this.editingIndex).queryKey, todo)
28 this.draft = {}
29 },
30 })
31β
32 protected override createRenderRoot(): HTMLElement | DocumentFragment {
33 return this
34 }
35β
36 private setEditingIndex(editingIndex: number | null) {
37 this.dispatchEvent(
38 new CustomEvent('set-editing-index', { detail: editingIndex }),
39 )
40 }
41β
42 render() {
43 const { status, data, isFetching, failureCount, refetch } = this.todo()
44 const {
45 status: saveStatus,
46 error: saveError,
47 mutate: save,
48 } = this.saveMutation()
49β
50 const disableEditSave = status === 'pending' || saveStatus === 'pending'
51β
52 return html`
53 <div>
54 <div>
55 ${
56 data
57 ? html`
58 <button
59 type="button"
60 @click=${() => this.setEditingIndex(null)}
61 >
62 Back
63 </button>
64 Editing Todo "${data.name}" (#${this.editingIndex})
65 `
66 : null
67 }
68 </div>
69 ${
70 status === 'pending'
71 ? html`<span>Loading... (Attempt: ${failureCount + 1})</span>`
72 : status === 'error'
73 ? html`
74 <span>
75 Error!
76 <button type="button" @click=${() => refetch()}>
77 Retry
78 </button>
79 </span>
80 `
81 : html`
82 <label>
83 Name:
84 <input
85 .value=${this.draft.name ?? data.name}
86 ?disabled=${disableEditSave}
87 @input=${(event: Event) =>
88 (this.draft = {
89 ...this.draft,
90 name: (event.currentTarget as HTMLInputElement).value,
91 })}
92 />
93 </label>
94 <label>
95 Notes:
96 <input
97 .value=${this.draft.notes ?? data.notes}
98 ?disabled=${disableEditSave}
99 @input=${(event: Event) =>
100 (this.draft = {
101 ...this.draft,
102 notes: (event.currentTarget as HTMLInputElement)
103 .value,
104 })}
105 />
106 </label>
107 <div>
108 <button
109 type="button"
110 ?disabled=${disableEditSave}
111 @click=${() => save({ ...data, ...this.draft })}
112 >
113 Save
114 </button>
115 </div>
116 <div>
117 ${
118 saveStatus === 'pending'
119 ? 'Saving...'
120 : saveStatus === 'error'
121 ? saveError.message
122 : saveStatus === 'success'
123 ? 'Saved!'
124 : null
125 }
126 </div>
127 <div>
128 ${
129 isFetching
130 ? html`<span>
131 Background Refreshing... (Attempt:
132 ${failureCount + 1})
133 </span>`
134 : html`<span> </span>`
135 }
136 </div>
137 `
138 }
139 </div>
140 `
141 }
142}
143β
144customElements.define('edit-todo', EditTodo)