refactor(app): improve loading state management in user fetch

This commit is contained in:
2025-05-06 08:59:35 +02:00
parent dda69b680e
commit 7c04e848d2
2 changed files with 8 additions and 6 deletions

View File

@ -27,16 +27,18 @@ export class AppComponent {
ngOnInit() {
this.userService.getUsers().subscribe(users => {
this.users.set(users);
this.isLoadingUsers.set(false)
});
}
onUserSelect(event: Event) {
this.isLoadingTodos.set(true)
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(false)
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)
});
}
}

View File

@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { delay, Observable, shareReplay } from 'rxjs';
import { delay, Observable } from 'rxjs';
@Injectable({
providedIn: 'root'