Open History to see the last commit
1import { ChangeDetectionStrategy, Component, inject } from '@angular/core'
2import { injectQuery } from '@tanstack/angular-query'
3import { lastValueFrom } from 'rxjs'
4import { PostsService } from '../services/posts.service'
5β
6@Component({
7 changeDetection: ChangeDetectionStrategy.OnPush,
8 selector: 'posts',
9 template: `
10 @if (postsQuery.isPending()) {
11 <p>Loading posts...</p>
12 } @else if (postsQuery.isError()) {
13 <p>Failed to load posts.</p>
14 } @else {
15 <section class="grid">
16 @for (post of postsQuery.data(); track post.id) {
17 <article class="card">
18 <p class="meta">Post #{{ post.id }}</p>
19 <h2>{{ post.title }}</h2>
20 <p>{{ post.body }}</p>
21 </article>
22 }
23 </section>
24 }
25 `,
26 styles: `
27 .grid {
28 display: grid;
29 gap: 16px;
30 grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
31 }
32β
33 .card {
34 padding: 20px;
35 border: 1px solid #eadfcb;
36 border-radius: 18px;
37 background: #fffdf8;
38 box-shadow: 0 12px 30px rgba(123, 88, 31, 0.08);
39 }
40β
41 .meta {
42 margin: 0 0 10px;
43 font-size: 12px;
44 font-weight: 700;
45 letter-spacing: 0.08em;
46 text-transform: uppercase;
47 color: #8b5e34;
48 }
49β
50 h2 {
51 margin: 0 0 10px;
52 font-size: 1.1rem;
53 line-height: 1.3;
54 }
55β
56 p {
57 margin: 0;
58 line-height: 1.5;
59 color: #374151;
60 }
61 `,
62})
63export class PostsComponent {
64 readonly #postsService = inject(PostsService)
65β
66 readonly postsQuery = injectQuery(() => ({
67 queryKey: ['posts'],
68 queryFn: () => lastValueFrom(this.#postsService.allPosts$()),
69 }))
70}