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

main.ts

85 lines2.1 KB

1import { LitElement, html } from 'lit'
2import { QueryClient, QueryClientProvider } from '@tanstack/lit-query'
3import './character-list'
4​
5const queryClient = new QueryClient({
6 defaultOptions: {
7 queries: {
8 retry: 0,
9 },
10 },
11})
12​
13class BatchingQueryProvider extends QueryClientProvider {
14 constructor() {
15 super()
16 this.client = queryClient
17 }
19​
20class BatchingApp extends LitElement {
21 static properties = {
22 ids: { state: true },
23 idsToLoad: { state: true },
24 error: { state: true },
25 }
26​
27 private ids: Array<number> = []
28 private idsToLoad: Array<number> = []
29 private error = ''
30​
31 protected override createRenderRoot(): HTMLElement | DocumentFragment {
32 return this
33 }
34​
35 private handleInput(event: Event) {
36 const parsedIds = (event.currentTarget as HTMLInputElement).value
37 .split(',')
38 .map((id) => id.trim())
39​
40 if (parsedIds.every((id) => /^\d+$/.test(id))) {
41 this.ids = parsedIds.map(Number)
42 this.error = ''
43 } else {
44 this.error = 'Invalid.. only use numbers and comma'
45 }
46 }
47​
48 render() {
49 return html`
50 <batching-query-provider>
51 <div class="App">
52 <h2>Character fetcher</h2>
53 <div>
54 <label for="ids" style="display: block">
55 Character ids (numeric, comma separated)
56 </label>
57 <input
58 id="ids"
59 style="display: inline-block"
60 placeholder="ids"
61 @input=${this.handleInput}
62 />
63 </div>
64​
65 ${this.error ? html`<h3 style="color: red">${this.error}</h3>` : null}
66​
67 <button
68 type="button"
69 ?disabled=${!!this.error}
70 @click=${() => (this.idsToLoad = this.ids)}
71 >
72 Load characters
73 </button>
74 <div>
75 <h4 style="text-align: left">Characters:</h4>
76 <character-list .ids=${this.idsToLoad}></character-list>
77 </div>
78 </div>
79 </batching-query-provider>
80 `
81 }
83​
84customElements.define('batching-query-provider', BatchingQueryProvider)
85customElements.define('batching-app', BatchingApp)