diff --git a/src/app/core/layouts/header/header.html b/src/app/core/layouts/header/header.html index c2c6a7e..3abd3eb 100644 --- a/src/app/core/layouts/header/header.html +++ b/src/app/core/layouts/header/header.html @@ -32,7 +32,7 @@ style="anchor-name: --anchor-2" > - {{ cartItemCount() }} + {{ cartItemCount | async }} @@ -53,7 +53,7 @@ this.cartItem().itemsCount ?? 0); + cartItem$ = this.cartService.cartItem$; + cartItemCount = this.cartItem$.pipe(map((cart: CartModel) => cart.itemsCount ?? 0)); } diff --git a/src/app/core/services/cart-service.ts b/src/app/core/services/cart-service.ts index 1858e5c..f58d354 100644 --- a/src/app/core/services/cart-service.ts +++ b/src/app/core/services/cart-service.ts @@ -4,6 +4,7 @@ import { API_URL } from "../tokens/api-url-tokens"; import { CartItemModel, CartItemRequest, CartModel } from "../models/cart.model"; import { AuthService, AuthState } from "@app/features/auth/services/auth-service"; import { Cart } from "@app/shared/components/cart/cart"; +import { BehaviorSubject, tap } from "rxjs"; @Injectable({ providedIn: "root", @@ -14,21 +15,23 @@ export class CartService { private http = inject(HttpClient); private apiUrl = inject(API_URL); - cartItem = signal({} as CartModel); + private _cartItem = new BehaviorSubject({} as CartModel); + + cartItem$ = this._cartItem.asObservable(); constructor() { effect(() => { if (this.authService.isAuthenticated()) { this.fetchCart(); } else { - this.cartItem.set({} as CartModel); + this._cartItem.next({} as CartModel); } }); } fetchCart() { return this.http.get(this.apiUrl + "/cart").subscribe({ - next: (data) => this.cartItem.set(data), + next: (data) => this._cartItem.next(data), error: (error: HttpErrorResponse) => { if (error.status === 401) { this.authService.purgeAuth(); @@ -38,11 +41,21 @@ export class CartService { }); } + addToCart(data: CartItemRequest) { + return this.http + .post(this.apiUrl + "/cart", data) + .pipe(tap((updatedCart: CartModel) => this._cartItem.next(updatedCart))); + } + updateCart(data: CartItemRequest) { - return this.http.patch(this.apiUrl + "/cart", data); + return this.http + .patch(this.apiUrl + "/cart", data) + .pipe(tap((updatedCart: CartModel) => this._cartItem.next(updatedCart))); } removeFromCart(productId: number) { - return this.http.delete(this.apiUrl + "/cart", { body: { productId: productId } }); + return this.http + .delete(this.apiUrl + "/cart", { body: { productId: productId } }) + .pipe(tap((updatedCart: CartModel) => this._cartItem.next(updatedCart))); } } diff --git a/src/app/features/product/components/product-card/product-card.html b/src/app/features/product/components/product-card/product-card.html index 6a68712..c4e3d1e 100644 --- a/src/app/features/product/components/product-card/product-card.html +++ b/src/app/features/product/components/product-card/product-card.html @@ -21,7 +21,7 @@

Rs. {{ product.actualPrice }}

-
diff --git a/src/app/features/product/components/product-card/product-card.ts b/src/app/features/product/components/product-card/product-card.ts index 6abd996..c137804 100644 --- a/src/app/features/product/components/product-card/product-card.ts +++ b/src/app/features/product/components/product-card/product-card.ts @@ -3,6 +3,7 @@ import { ProductModel } from "../../../../core/models/product.model"; import { Router } from "@angular/router"; import { FavoriteButton } from "../../../../shared/components/favorite-button/favorite-button"; import { LucideAngularModule, ShoppingCart } from "lucide-angular"; +import { CartService } from "@app/core/services/cart-service"; @Component({ selector: "app-product-card", @@ -13,10 +14,16 @@ import { LucideAngularModule, ShoppingCart } from "lucide-angular"; export class ProductCard { readonly router = inject(Router); readonly ShoppingCartIcon = ShoppingCart; + readonly cartService = inject(CartService); @Input() product!: ProductModel; goToProductDetails() { this.router.navigate(["/products", this.product.slug]); } + + addToCart(event: Event) { + event.stopPropagation(); + this.cartService.addToCart({ productId: this.product.id, quantity: 1 }).subscribe(); + } } diff --git a/src/app/features/product/show-product/show-product.html b/src/app/features/product/show-product/show-product.html index c90e589..6809b38 100644 --- a/src/app/features/product/show-product/show-product.html +++ b/src/app/features/product/show-product/show-product.html @@ -90,28 +90,16 @@
-
- Rs.{{ product()?.actualPrice }} - Rs.{{ product()?.listPrice }} - +
+

+ Rs.{{ product()?.listPrice }} +

+

Rs.{{ product()?.actualPrice }}

-
- - - -
-
- +
diff --git a/src/app/features/product/show-product/show-product.ts b/src/app/features/product/show-product/show-product.ts index 881afb8..c923c86 100644 --- a/src/app/features/product/show-product/show-product.ts +++ b/src/app/features/product/show-product/show-product.ts @@ -3,6 +3,7 @@ import { ProductModel } from "../../../core/models/product.model"; import { ProductService } from "../services/product-service"; import { LucideAngularModule, Heart, ArrowRight, ArrowLeft } from "lucide-angular"; import { FavoriteButton } from "../../../shared/components/favorite-button/favorite-button"; +import { CartService } from "@app/core/services/cart-service"; @Component({ selector: "app-show-product", @@ -17,6 +18,7 @@ export class ShowProduct { ArrowRightIcon = ArrowRight; ArrowLeftIcon = ArrowLeft; productService = inject(ProductService); + cartService = inject(CartService); product = signal(null); activeImageIndex: WritableSignal = signal(0); totalImageCount: number = 0; @@ -29,9 +31,14 @@ export class ShowProduct { }); } + addToCart() { + this.cartService.addToCart({ productId: this.product()!.id, quantity: 1 }).subscribe(); + } + nextImage() { this.activeImageIndex.update((index) => (index + 1) % this.totalImageCount); } + prevImage() { this.activeImageIndex.update( (index) => diff --git a/src/app/shared/components/cart/cart.ts b/src/app/shared/components/cart/cart.ts index 7286b4f..121b0ef 100644 --- a/src/app/shared/components/cart/cart.ts +++ b/src/app/shared/components/cart/cart.ts @@ -26,9 +26,7 @@ export class Cart { this.cartService .updateCart(cartItem) .pipe(finalize(() => this.isLoading.set(false))) - .subscribe((cartdata) => { - this.cart = cartdata; - }); + .subscribe(); } removeProduct(productId: number) { @@ -37,8 +35,6 @@ export class Cart { this.cartService .removeFromCart(productId) .pipe(finalize(() => this.isLoading.set(false))) - .subscribe((cartData) => { - this.cart = cartData; - }); + .subscribe(); } }