45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import { Injectable, signal, WritableSignal } from '@angular/core';
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { delay, Observable, shareReplay } from 'rxjs';
|
|
import { Product } from '../product-selection/product-selection.component';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class ProductService {
|
|
private productSignal = signal<Product | null>(null);
|
|
private quantitySignal = signal<number>(1);
|
|
|
|
constructor(private http: HttpClient) {}
|
|
|
|
setProduct(product: Product | null): void {
|
|
this.productSignal.set(product);
|
|
if (product) {
|
|
this.quantitySignal.set(1);
|
|
}
|
|
}
|
|
|
|
getProduct(): Product | null {
|
|
return this.productSignal();
|
|
}
|
|
|
|
setQuantity(quantity: number): void {
|
|
this.quantitySignal.set(quantity);
|
|
}
|
|
|
|
getQuantity(): number {
|
|
return this.quantitySignal();
|
|
}
|
|
|
|
getProducts(): Observable<any[]> {
|
|
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}`).pipe(
|
|
delay(1000)
|
|
);
|
|
}
|
|
}
|