feat(product-selection): add loading state during product fetch

This commit is contained in:
2025-05-06 08:33:46 +02:00
parent 1c46e15836
commit 03bb5e70ee
5 changed files with 25 additions and 13 deletions

View File

@ -1,6 +1,6 @@
import { Injectable, signal, WritableSignal } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, shareReplay } from 'rxjs';
import { delay, Observable, shareReplay } from 'rxjs';
import { Product } from '../product-selection/product-selection.component';
@Injectable({
@ -12,7 +12,6 @@ export class ProductService {
constructor(private http: HttpClient) {}
// Local state management
setProduct(product: Product | null): void {
this.productSignal.set(product);
if (product) {
@ -32,12 +31,15 @@ export class ProductService {
return this.quantitySignal();
}
// API calls
getProducts(): Observable<any[]> {
return this.http.get<any[]>('https://fakestoreapi.com/products');
return this.http.get<any[]>('https://fakestoreapi.com/products').pipe(
delay(1000)
);
}
getProductById(id: number): Observable<any> {
return this.http.get<any>(`https://fakestoreapi.com/products/${id}`);
return this.http.get<any>(`https://fakestoreapi.com/products/${id}`).pipe(
delay(1000)
);
}
}