Open History to see the last commit
1import { LitElement, html } from 'lit'
2import { createQueryController } from '@tanstack/lit-query'
3import { postsQueries } from './services/posts/queries'
4β
5class PostDetail extends LitElement {
6 static properties = {
7 postId: { attribute: false },
8 }
9β
10 postId = ''
11β
12 private readonly post = createQueryController(this, () =>
13 postsQueries.detail(this.postId),
14 )
15β
16 protected override createRenderRoot(): HTMLElement | DocumentFragment {
17 return this
18 }
19β
20 private back(event: Event) {
21 event.preventDefault()
22 this.dispatchEvent(new CustomEvent('set-post-id', { detail: null }))
23 }
24β
25 render() {
26 const { isPending, isError, isFetching, data, error } = this.post()
27β
28 return html`
29 <div>
30 <div>
31 <a href="#posts" @click=${this.back}>Back</a>
32 </div>
33 ${
34 isPending
35 ? html`<div>Loading...</div>`
36 : isError
37 ? html`<span>Error: ${error.message}</span>`
38 : html`
39 <h1>${data.title}</h1>
40 <div>
41 <p>${data.body}</p>
42 </div>
43 <div>${isFetching ? 'Background Updating...' : ' '}</div>
44 `
45 }
46 </div>
47 `
48 }
49}
50β
51customElements.define('post-detail', PostDetail)