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