diff --git a/src/app/core/models/cart.model.ts b/src/app/core/models/cart.model.ts index b37512f..dbccc0b 100644 --- a/src/app/core/models/cart.model.ts +++ b/src/app/core/models/cart.model.ts @@ -13,3 +13,8 @@ export interface CartModel { totalPrice: number; items: CartItemModel[]; } + +export interface CartItemRequest { + productId: number; + quantity: number; +} diff --git a/src/app/core/services/cart-service.ts b/src/app/core/services/cart-service.ts index 3aba7d0..1858e5c 100644 --- a/src/app/core/services/cart-service.ts +++ b/src/app/core/services/cart-service.ts @@ -1,8 +1,9 @@ import { HttpClient, HttpErrorResponse } from "@angular/common/http"; import { effect, inject, Injectable, signal } from "@angular/core"; import { API_URL } from "../tokens/api-url-tokens"; -import { CartModel } from "../models/cart.model"; +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"; @Injectable({ providedIn: "root", @@ -36,4 +37,12 @@ export class CartService { }, }); } + + updateCart(data: CartItemRequest) { + return this.http.patch(this.apiUrl + "/cart", data); + } + + removeFromCart(productId: number) { + return this.http.delete(this.apiUrl + "/cart", { body: { productId: productId } }); + } } diff --git a/src/app/shared/components/cart-item/cart-item.html b/src/app/shared/components/cart-item/cart-item.html index f7d33ca..d29f4e0 100644 --- a/src/app/shared/components/cart-item/cart-item.html +++ b/src/app/shared/components/cart-item/cart-item.html @@ -7,17 +7,19 @@

{{ cartItem.title }}

Rs. {{ cartItem.price }} x {{ cartItem.quantity }}

-
+ +
+
+

Rs. {{ cartItem.subtotal }}

+
-
-
-

Rs. {{ cartItem.subtotal }}

- -

{{ cartItem.quantity }}

- + +

{{ cartItem.quantity }}

+
diff --git a/src/app/shared/components/cart-item/cart-item.ts b/src/app/shared/components/cart-item/cart-item.ts index 404b364..1a398be 100644 --- a/src/app/shared/components/cart-item/cart-item.ts +++ b/src/app/shared/components/cart-item/cart-item.ts @@ -1,5 +1,5 @@ -import { Component, Input } from "@angular/core"; -import { CartItemModel } from "@app/core/models/cart.model"; +import { Component, EventEmitter, Input, Output, signal } from "@angular/core"; +import { CartItemModel, CartItemRequest } from "@app/core/models/cart.model"; import { LucideAngularModule, Trash } from "lucide-angular"; @Component({ @@ -10,5 +10,25 @@ import { LucideAngularModule, Trash } from "lucide-angular"; }) export class CartItem { @Input() cartItem!: CartItemModel; + @Output() qtyChangeEvent = new EventEmitter(); + @Output() productDeleteEvent = new EventEmitter(); TrashIcon = Trash; + + incrementQty() { + if (this.cartItem.quantity < 10) { + this.cartItem.quantity += 1; + this.qtyChangeEvent.emit({ productId: this.cartItem.id, quantity: this.cartItem.quantity }); + } + } + + decrementQty() { + if (this.cartItem.quantity > 1) { + this.cartItem.quantity -= 1; + this.qtyChangeEvent.emit({ productId: this.cartItem.id, quantity: this.cartItem.quantity }); + } + } + + removeProduct() { + this.productDeleteEvent.emit(this.cartItem.id); + } } diff --git a/src/app/shared/components/cart/cart.html b/src/app/shared/components/cart/cart.html index b1aa159..df526c5 100644 --- a/src/app/shared/components/cart/cart.html +++ b/src/app/shared/components/cart/cart.html @@ -4,9 +4,17 @@ } @else if (authService.authState() === AuthState.Loading) {
  • Loading
  • } @else { -
      +
        @for (item of cart.items; track item.id) { - + }
      diff --git a/src/app/shared/components/cart/cart.ts b/src/app/shared/components/cart/cart.ts index 03a9dde..7286b4f 100644 --- a/src/app/shared/components/cart/cart.ts +++ b/src/app/shared/components/cart/cart.ts @@ -1,7 +1,9 @@ -import { Component, computed, inject, Input } from "@angular/core"; -import { CartModel } from "@app/core/models/cart.model"; +import { Component, computed, inject, Input, signal } from "@angular/core"; +import { CartItemModel, CartItemRequest, CartModel } from "@app/core/models/cart.model"; import { CartItem } from "../cart-item/cart-item"; import { AuthService, AuthState } from "@app/features/auth/services/auth-service"; +import { CartService } from "@app/core/services/cart-service"; +import { finalize, tap } from "rxjs"; @Component({ selector: "app-cart", @@ -12,6 +14,31 @@ import { AuthService, AuthState } from "@app/features/auth/services/auth-service export class Cart { @Input() cart!: CartModel; + isLoading = signal(false); + protected readonly authService = inject(AuthService); + protected readonly cartService = inject(CartService); protected readonly AuthState = AuthState; + + updateProductQty(cartItem: CartItemRequest) { + this.isLoading.set(true); + + this.cartService + .updateCart(cartItem) + .pipe(finalize(() => this.isLoading.set(false))) + .subscribe((cartdata) => { + this.cart = cartdata; + }); + } + + removeProduct(productId: number) { + this.isLoading.set(true); + + this.cartService + .removeFromCart(productId) + .pipe(finalize(() => this.isLoading.set(false))) + .subscribe((cartData) => { + this.cart = cartData; + }); + } }