feat(cart): add loading state management for cart item operations and update payment intent handling
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { inject, Injectable, signal } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { map, Observable, tap } from 'rxjs';
|
||||
import { catchError, map, Observable, tap } from 'rxjs';
|
||||
|
||||
import { ApiResponse } from '../api-response.interface';
|
||||
import { TenantService } from '../tenant.service';
|
||||
@@ -16,6 +16,9 @@ export class CartService {
|
||||
private readonly cartState = signal<Cart | null>(null);
|
||||
readonly cart = this.cartState.asReadonly();
|
||||
|
||||
private readonly isUpdatingState = signal<boolean>(false);
|
||||
readonly isUpdating = this.isUpdatingState.asReadonly();
|
||||
|
||||
private get tenantApiUrl(): string {
|
||||
return this.tenantService.getTenantApiUrl();
|
||||
}
|
||||
@@ -32,6 +35,7 @@ export class CartService {
|
||||
}
|
||||
|
||||
addItem(productVariantId: number, cantidad: number): Observable<ApiResponse<Cart>> {
|
||||
this.isUpdatingState.set(true);
|
||||
return this.http
|
||||
.post<ApiResponse<Cart>>(
|
||||
`${this.tenantApiUrl}/cart/items`,
|
||||
@@ -39,11 +43,19 @@ export class CartService {
|
||||
{ withCredentials: true }
|
||||
)
|
||||
.pipe(
|
||||
tap((response) => this.cartState.set(response.data))
|
||||
tap((response) => {
|
||||
this.cartState.set(response.data);
|
||||
this.isUpdatingState.set(false);
|
||||
}),
|
||||
catchError((error) => {
|
||||
this.isUpdatingState.set(false);
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
updateItemQuantity(productVariantId: number, cantidad: number): Observable<ApiResponse<Cart>> {
|
||||
this.isUpdatingState.set(true);
|
||||
return this.http
|
||||
.patch<ApiResponse<Cart>>(
|
||||
`${this.tenantApiUrl}/cart/items/${productVariantId}`,
|
||||
@@ -51,18 +63,33 @@ export class CartService {
|
||||
{ withCredentials: true }
|
||||
)
|
||||
.pipe(
|
||||
tap((response) => this.cartState.set(response.data))
|
||||
tap((response) => {
|
||||
this.cartState.set(response.data);
|
||||
this.isUpdatingState.set(false);
|
||||
}),
|
||||
catchError((error) => {
|
||||
this.isUpdatingState.set(false);
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
removeItem(productVariantId: number): Observable<ApiResponse<Cart>> {
|
||||
this.isUpdatingState.set(true);
|
||||
return this.http
|
||||
.delete<ApiResponse<Cart>>(
|
||||
`${this.tenantApiUrl}/cart/items/${productVariantId}`,
|
||||
{ withCredentials: true }
|
||||
)
|
||||
.pipe(
|
||||
tap((response) => this.cartState.set(response.data))
|
||||
tap((response) => {
|
||||
this.cartState.set(response.data);
|
||||
this.isUpdatingState.set(false);
|
||||
}),
|
||||
catchError((error) => {
|
||||
this.isUpdatingState.set(false);
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user