BREAKING CHANGE: change obervable name from cartItem$ to cartItems$
This commit is contained in:
parent
24bdfe9cc6
commit
419e8281e2
@ -53,7 +53,7 @@
|
||||
</ul>
|
||||
|
||||
<app-cart
|
||||
[cart]="(cartItem$ | async)!"
|
||||
[cart]="(cartItems$ | async)!"
|
||||
id="popover-2"
|
||||
class="dropdown"
|
||||
popover
|
||||
|
||||
@ -21,6 +21,6 @@ export class Header {
|
||||
readonly cartService = inject(CartService);
|
||||
protected readonly AuthState = AuthState;
|
||||
|
||||
cartItem$ = this.cartService.cartItem$;
|
||||
cartItemCount = this.cartItem$.pipe(map((cart: CartModel) => cart.itemsCount ?? 0));
|
||||
cartItems$ = this.cartService.cartItems$;
|
||||
cartItemCount = this.cartItems$.pipe(map((cart: CartModel) => cart.itemsCount ?? 0));
|
||||
}
|
||||
|
||||
@ -15,23 +15,23 @@ export class CartService {
|
||||
private http = inject(HttpClient);
|
||||
private apiUrl = inject(API_URL);
|
||||
|
||||
private _cartItem = new BehaviorSubject<CartModel>({} as CartModel);
|
||||
private _cartItems = new BehaviorSubject<CartModel>({} as CartModel);
|
||||
|
||||
cartItem$ = this._cartItem.asObservable();
|
||||
cartItems$ = this._cartItems.asObservable();
|
||||
|
||||
constructor() {
|
||||
effect(() => {
|
||||
if (this.authService.isAuthenticated()) {
|
||||
this.fetchCart();
|
||||
} else {
|
||||
this._cartItem.next({} as CartModel);
|
||||
this._cartItems.next({} as CartModel);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fetchCart() {
|
||||
private fetchCart() {
|
||||
return this.http.get<CartModel>(this.apiUrl + "/cart").subscribe({
|
||||
next: (data) => this._cartItem.next(data),
|
||||
next: (data) => this._cartItems.next(data),
|
||||
error: (error: HttpErrorResponse) => {
|
||||
if (error.status === 401) {
|
||||
this.authService.purgeAuth();
|
||||
@ -44,18 +44,18 @@ export class CartService {
|
||||
addToCart(data: CartItemRequest) {
|
||||
return this.http
|
||||
.post<CartModel>(this.apiUrl + "/cart", data)
|
||||
.pipe(tap((updatedCart: CartModel) => this._cartItem.next(updatedCart)));
|
||||
.pipe(tap((updatedCart: CartModel) => this._cartItems.next(updatedCart)));
|
||||
}
|
||||
|
||||
updateCart(data: CartItemRequest) {
|
||||
return this.http
|
||||
.patch<CartModel>(this.apiUrl + "/cart", data)
|
||||
.pipe(tap((updatedCart: CartModel) => this._cartItem.next(updatedCart)));
|
||||
.pipe(tap((updatedCart: CartModel) => this._cartItems.next(updatedCart)));
|
||||
}
|
||||
|
||||
removeFromCart(productId: number) {
|
||||
return this.http
|
||||
.delete<CartModel>(this.apiUrl + "/cart", { body: { productId: productId } })
|
||||
.pipe(tap((updatedCart: CartModel) => this._cartItem.next(updatedCart)));
|
||||
.pipe(tap((updatedCart: CartModel) => this._cartItems.next(updatedCart)));
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user