-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
157 lines (143 loc) · 3.96 KB
/
main.ts
File metadata and controls
157 lines (143 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// deno-lint-ignore-file no-import-prefix
import "@angular/compiler";
import "zone.js";
import { AsyncPipe } from "@angular/common";
import { HttpClient, provideHttpClient } from "@angular/common/http";
import {
ChangeDetectionStrategy,
Component,
inject,
Injectable,
} from "@angular/core";
import { FormControl, ReactiveFormsModule } from "@angular/forms";
import { bootstrapApplication } from "@angular/platform-browser";
import type {
Post,
User,
} from "https://esm.sh/*@untypeable/jsonplaceholder@1.0.2";
import {
BehaviorSubject,
combineLatest,
distinctUntilChanged,
filter,
map,
type Observable,
share,
startWith,
switchMap,
tap,
} from "rxjs";
const urlBase = "https://jsonplaceholder.typicode.com";
@Injectable({ providedIn: "root" })
class UserService {
readonly #httpClient = inject(HttpClient);
getUsers() {
return this.#httpClient.get<User[]>(`${urlBase}/users`);
}
}
@Injectable({ providedIn: "root" })
class PostService {
readonly #httpClient = inject(HttpClient);
getPosts(userId: number) {
return this.#httpClient.get<Post[]>(`${urlBase}/posts`, {
params: { userId },
});
}
}
@Injectable({ providedIn: "root" })
class ReadmeService {
readonly #httpClient = inject(HttpClient);
getReadmeHTML() {
return combineLatest([
import("https://esm.sh/*marked@17.0.0"),
this.#httpClient.get("./README.md", { responseType: "text" }),
]).pipe(
switchMap(async ([{ marked }, readmeText]) => await marked(readmeText)),
);
}
}
@Component({
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
selector: "app-root",
imports: [AsyncPipe, ReactiveFormsModule],
template: `
<section [innerHTML]="readmeHTML$ | async"></section>
@if (users$ | async; as users) {
<label>
Select User:
<select [formControl]="formControl">
<option hidden selected></option>
@for (user of users; track user.id) {
<option value="{{ user.id }}">
@{{ user.username }}: {{ user.name }}
</option>
}
</select>
</label>
} @else if (loadingUsers$ | async) {
<p>Loading Users...</p>
}
@if (posts$ | async; as posts) {
<ul>
@for (post of posts; track post.id) {
<li>{{ post.title }}</li>
}
</ul>
} @else if (loadingPosts$ | async) {
<p>Loading Posts...</p>
} @else if (!(user$ | async)) {
<p>Select User to view posts</p>
}
<p>
Data Source:
<a href="https://jsonplaceholder.typicode.com/" target="_blank">
JSONPlaceholder
</a>
</p>
`,
})
class AppComponent {
protected readonly formControl = new FormControl("");
protected readonly users$ = this.#getUsers();
protected readonly posts$ = this.#getPosts(this.#getSelectedUserId());
protected readonly loadingUsers$ = new BehaviorSubject(false);
protected readonly loadingPosts$ = new BehaviorSubject(false);
protected readonly readmeHTML$ = inject(ReadmeService).getReadmeHTML();
#getSelectedUserId() {
return this.formControl.valueChanges.pipe(
startWith(this.formControl.value),
filter((id) => id !== null),
filter((id) => id !== ""),
map((id) => +id),
share(),
);
}
#getUsers() {
const userService = inject(UserService);
return userService.getUsers()
.pipe(
tap({
subscribe: () => this.loadingUsers$.next(true),
finalize: () => this.loadingUsers$.next(false),
}),
share(),
);
}
#getPosts(selectedUserId$: Observable<number>) {
const postService = inject(PostService);
return selectedUserId$.pipe(
distinctUntilChanged(),
switchMap((id) =>
postService.getPosts(id).pipe(tap({
subscribe: () => this.loadingPosts$.next(true),
finalize: () => this.loadingPosts$.next(false),
}))
),
share(),
);
}
}
await bootstrapApplication(AppComponent, {
providers: [provideHttpClient()],
});