feat: add loading indicators for users and todos
This commit is contained in:
@ -1,13 +1,21 @@
|
|||||||
|
<ng-container *ngIf="isLoadingUsers()">
|
||||||
|
<p>Loading...</p>
|
||||||
|
</ng-container>
|
||||||
|
<ng-container *ngIf="!isLoadingUsers()">
|
||||||
<select id="userId" (change)="onUserSelect($event)">
|
<select id="userId" (change)="onUserSelect($event)">
|
||||||
<ng-container *ngFor="let user of users()">
|
<ng-container *ngFor="let user of users()">
|
||||||
<option [value]="user.id">{{user.name}}</option>
|
<option [value]="user.id">{{user.name}}</option>
|
||||||
</ng-container>
|
</ng-container>
|
||||||
</select>
|
</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">
|
<select id="todoId">
|
||||||
<ng-container *ngFor="let todo of todos()">
|
<ng-container *ngFor="let todo of todos()">
|
||||||
<option [value]="todo.id">{{todo.title}}</option>
|
<option [value]="todo.id">{{todo.title}}</option>
|
||||||
</ng-container>
|
</ng-container>
|
||||||
</select>
|
</select>
|
||||||
</ng-container>
|
</div>
|
||||||
|
@ -21,17 +21,22 @@ export class AppComponent {
|
|||||||
|
|
||||||
todos: WritableSignal<any[]> = signal([]);
|
todos: WritableSignal<any[]> = signal([]);
|
||||||
|
|
||||||
ngOnInit() {
|
isLoadingUsers = signal(true);
|
||||||
this.userService.getUsers().subscribe(users => this.users.set(users));
|
isLoadingTodos = signal(false);
|
||||||
|
|
||||||
if(this.selectedUserId() !== null) {
|
ngOnInit() {
|
||||||
this.userService.getTodos(this.selectedUserId()!).subscribe(todos => this.todos.set(todos));
|
this.userService.getUsers().subscribe(users => {
|
||||||
}
|
this.users.set(users);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onUserSelect(event: Event) {
|
onUserSelect(event: Event) {
|
||||||
|
this.isLoadingTodos.set(true)
|
||||||
const userId = Number((event.target as HTMLSelectElement).value);
|
const userId = Number((event.target as HTMLSelectElement).value);
|
||||||
this.selectedUserId.set(userId);
|
this.selectedUserId.set(userId);
|
||||||
this.userService.getTodos(userId).subscribe(todos => this.todos.set(todos));
|
this.userService.getTodos(userId).subscribe(todos => {
|
||||||
|
this.todos.set(todos)
|
||||||
|
this.isLoadingTodos.set(false)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { HttpClient } from '@angular/common/http';
|
import { HttpClient } from '@angular/common/http';
|
||||||
import { Observable, shareReplay } from 'rxjs';
|
import { delay, Observable, shareReplay } from 'rxjs';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
@ -9,18 +9,26 @@ export class UserService {
|
|||||||
constructor(private http: HttpClient) {}
|
constructor(private http: HttpClient) {}
|
||||||
|
|
||||||
getUsers(): Observable<any[]> {
|
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> {
|
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[]> {
|
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> {
|
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