Compare commits
2 Commits
main
...
input-todo
Author | SHA1 | Date | |
---|---|---|---|
7c04e848d2
|
|||
dda69b680e
|
@ -1,13 +1,21 @@
|
||||
<ng-container *ngIf="isLoadingUsers()">
|
||||
<p>Loading...</p>
|
||||
</ng-container>
|
||||
<ng-container *ngIf="!isLoadingUsers()">
|
||||
<select id="userId" (change)="onUserSelect($event)">
|
||||
<ng-container *ngFor="let user of users()">
|
||||
<option [value]="user.id">{{user.name}}</option>
|
||||
</ng-container>
|
||||
</select>
|
||||
</ng-container>
|
||||
|
||||
<ng-container *ngIf="selectedUserId() !== null && todos()">
|
||||
<ng-container *ngIf="isLoadingTodos()">
|
||||
<p>Loading...</p>
|
||||
</ng-container>
|
||||
<div *ngIf="!isLoadingTodos() && selectedUserId() !== null && todos()">
|
||||
<select id="todoId">
|
||||
<ng-container *ngFor="let todo of todos()">
|
||||
<option [value]="todo.id">{{todo.title}}</option>
|
||||
</ng-container>
|
||||
</select>
|
||||
</ng-container>
|
||||
</div>
|
||||
|
@ -20,18 +20,25 @@ export class AppComponent {
|
||||
users: WritableSignal<any[]> = signal([]);
|
||||
|
||||
todos: WritableSignal<any[]> = signal([]);
|
||||
|
||||
isLoadingUsers = signal(true);
|
||||
isLoadingTodos = signal(false);
|
||||
|
||||
ngOnInit() {
|
||||
this.userService.getUsers().subscribe(users => this.users.set(users));
|
||||
|
||||
if(this.selectedUserId() !== null) {
|
||||
this.userService.getTodos(this.selectedUserId()!).subscribe(todos => this.todos.set(todos));
|
||||
}
|
||||
this.userService.getUsers().subscribe(users => {
|
||||
this.users.set(users);
|
||||
this.isLoadingUsers.set(false)
|
||||
});
|
||||
}
|
||||
|
||||
onUserSelect(event: Event) {
|
||||
onUserSelect(event: Event): void {
|
||||
const userId = Number((event.target as HTMLSelectElement).value);
|
||||
this.selectedUserId.set(userId);
|
||||
this.userService.getTodos(userId).subscribe(todos => this.todos.set(todos));
|
||||
this.isLoadingTodos.set(true);
|
||||
this.userService.getTodos(userId).subscribe({
|
||||
next: (todos) => this.todos.set(todos),
|
||||
error: () => this.isLoadingTodos.set(false),
|
||||
complete: () => this.isLoadingTodos.set(false)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Observable, shareReplay } from 'rxjs';
|
||||
import { delay, Observable } from 'rxjs';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@ -9,18 +9,26 @@ export class UserService {
|
||||
constructor(private http: HttpClient) {}
|
||||
|
||||
getUsers(): Observable<any[]> {
|
||||
return this.http.get<any[]>('https://jsonplaceholder.typicode.com/users')
|
||||
return this.http.get<any[]>('https://jsonplaceholder.typicode.com/users').pipe(
|
||||
delay(1000)
|
||||
)
|
||||
}
|
||||
|
||||
getUser(id: number): Observable<any> {
|
||||
return this.http.get<any>(`https://jsonplaceholder.typicode.com/users/${id}`)
|
||||
return this.http.get<any>(`https://jsonplaceholder.typicode.com/users/${id}`).pipe(
|
||||
delay(1000)
|
||||
)
|
||||
}
|
||||
|
||||
getTodos(userId: number): Observable<any[]> {
|
||||
return this.http.get<any[]>(`https://jsonplaceholder.typicode.com/todos?userId=${userId}`)
|
||||
return this.http.get<any[]>(`https://jsonplaceholder.typicode.com/todos?userId=${userId}`).pipe(
|
||||
delay(1000)
|
||||
)
|
||||
}
|
||||
|
||||
getTodo(id: number): Observable<any> {
|
||||
return this.http.get<any>(`https://jsonplaceholder.typicode.com/todos/${id}`)
|
||||
return this.http.get<any>(`https://jsonplaceholder.typicode.com/todos/${id}`).pipe(
|
||||
delay(1000)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user