feat(product-selection): add loading state during product fetch
This commit is contained in:
@ -1 +1 @@
|
||||
<input type="number" [ngModel]="quantity" (ngModelChange)="onQuantityChange($event)" [disabled]="!product" min="1">
|
||||
<input type="number" [ngModel]="quantity" (ngModelChange)="onQuantityChange($event)" [disabled]="!product" min="1">
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
|
||||
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output, signal } from '@angular/core';
|
||||
import { Product } from '../product-selection/product-selection.component';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { ProductService } from '../services/product.service';
|
||||
@ -11,6 +11,7 @@ import { ProductService } from '../services/product.service';
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
})
|
||||
export class ProductQuantityComponent {
|
||||
|
||||
@Input({ required: true }) set product(value: Product | null) {
|
||||
this.productService.setProduct(value);
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
<select [ngModel]="selectedProduct()" (ngModelChange)="onProductChange($event)">
|
||||
<option [ngValue]="null">Select a product</option>
|
||||
<option *ngFor="let product of products()" [ngValue]="product">{{ product.title }} - {{ product.price | currency : 'EUR' : 'symbol' : '1.2-2' }}</option>
|
||||
<ng-container *ngIf="isProductLoading()">
|
||||
<div>Loading...</div>
|
||||
</ng-container>
|
||||
<ng-container *ngIf="!isProductLoading()">
|
||||
<select [ngModel]="selectedProduct()" (ngModelChange)="onProductChange($event)">
|
||||
<option [ngValue]="null">Select a product</option>
|
||||
<option *ngFor="let product of products()" [ngValue]="product">{{ product.title }} - {{ product.price | currency : 'EUR' : 'symbol' : '1.2-2' }}</option>
|
||||
</select>
|
@ -1,4 +1,4 @@
|
||||
import { NgFor } from '@angular/common';
|
||||
import { NgFor, NgIf } from '@angular/common';
|
||||
import { CurrencyPipe } from '@angular/common';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { Component, ChangeDetectionStrategy, Output, EventEmitter, signal, inject, computed, OnInit } from '@angular/core';
|
||||
@ -20,7 +20,7 @@ export type Product = {
|
||||
@Component({
|
||||
selector: 'app-product-selection',
|
||||
standalone: true,
|
||||
imports: [NgFor, CurrencyPipe, FormsModule],
|
||||
imports: [NgFor, CurrencyPipe, FormsModule, NgIf],
|
||||
templateUrl: './product-selection.component.html',
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
})
|
||||
@ -31,6 +31,8 @@ export class ProductSelectionComponent implements OnInit {
|
||||
selectedProduct = signal<Product | null>(null);
|
||||
|
||||
quantity = signal<number>(1);
|
||||
|
||||
isProductLoading = signal(true);
|
||||
|
||||
@Output() quantityChange = new EventEmitter<number>();
|
||||
@Output() productChange = new EventEmitter<Product>();
|
||||
@ -38,7 +40,10 @@ export class ProductSelectionComponent implements OnInit {
|
||||
private productService = inject(ProductService);
|
||||
|
||||
ngOnInit() {
|
||||
this.productService.getProducts().subscribe(products => this.products.set(products));
|
||||
this.productService.getProducts().subscribe(products => {
|
||||
this.products.set(products);
|
||||
this.isProductLoading.set(false);
|
||||
});
|
||||
}
|
||||
|
||||
onProductChange(selectedProduct: Product) {
|
||||
|
@ -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)
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user