From 7c04e848d2d49946bbd16dba719d9e663e7ff431 Mon Sep 17 00:00:00 2001 From: Jan-Marlon Leibl Date: Tue, 6 May 2025 08:59:35 +0200 Subject: [PATCH] refactor(app): improve loading state management in user fetch --- src/app/app.component.ts | 12 +++++++----- src/app/user.service.ts | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index b024a3d..26b89b8 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -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) }); } } diff --git a/src/app/user.service.ts b/src/app/user.service.ts index c50fdb0..ee630f7 100644 --- a/src/app/user.service.ts +++ b/src/app/user.service.ts @@ -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'