()
| 78 | } |
| 79 | |
| 80 | render() { |
| 81 | const { status, data: todos, error } = this.todos() |
| 82 | const addTodoMutation = this.addTodoMutation() |
| 83 | |
| 84 | if (status === 'pending') return html`<p>Loading todos…</p>` |
| 85 | if (status === 'error') return html`<p>Error: ${error.message}</p>` |
| 86 | |
| 87 | return html` |
| 88 | <div> |
| 89 | <p style="font-size: 0.875rem; color: #555; margin-bottom: 1rem"> |
| 90 | <strong>Approach 2 — via cache manipulation:</strong> |
| 91 | <code>onMutate</code> snapshots the cache and writes the optimistic |
| 92 | item in. <code>onError</code> restores the snapshot on failure. |
| 93 | </p> |
| 94 | |
| 95 | <form |
| 96 | @submit=${this.handleSubmit} |
| 97 | style="display: flex; gap: 8px; margin-bottom: 1rem" |
| 98 | > |
| 99 | <input |
| 100 | .value=${this.inputValue} |
| 101 | @input=${(event: Event) => |
| 102 | (this.inputValue = ( |
| 103 | event.currentTarget as HTMLInputElement |
| 104 | ).value)} |
| 105 | placeholder="New todo…" |
| 106 | style="flex: 1; padding: 0.4rem 0.6rem; font-size: 1rem" |
| 107 | /> |
| 108 | <button type="submit" ?disabled=${addTodoMutation.isPending}> |
| 109 | Add |
| 110 | </button> |
| 111 | </form> |
| 112 | |
| 113 | ${ |
| 114 | this.lastError |
| 115 | ? html`<p style="color: red; margin-bottom: 0.5rem"> |
| 116 | ${this.lastError} |
| 117 | </p>` |
| 118 | : null |
| 119 | } |
| 120 | |
| 121 | <ul style="list-style: none; padding: 0"> |
| 122 | ${todos.map( |
| 123 | (todo) => html` |
| 124 | <li |
| 125 | style="padding: 0.4rem 0; border-bottom: 1px solid #eee; opacity: ${ |
| 126 | todo.id.startsWith('optimistic-') ? 0.5 : 1 |
| 127 | }" |
| 128 | > |
| 129 | ${todo.text} |
| 130 | ${ |
| 131 | todo.id.startsWith('optimistic-') |
| 132 | ? html`<em> (saving…)</em>` |
| 133 | : null |
| 134 | } |
| 135 | </li> |
| 136 | `, |
| 137 | )} |
nothing calls this directly
no outgoing calls
no test coverage detected