feat: enhance product detail loading and initialization logic
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 6m50s
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 6m50s
- Introduced an `_initialized` flag to manage product loading state more effectively. - Updated `OnParametersSetAsync` to conditionally load product details based on the initialization state and valid product ID. - Refactored product retrieval logic in `ProductService` to handle invalid IDs and improve fallback mechanisms for fetching product details. These changes improve the reliability and performance of the product detail component, ensuring that product information is loaded correctly and efficiently.
This commit is contained in:
@@ -21,6 +21,7 @@ public partial class ProductDetail : ComponentBase, IDisposable
|
||||
|
||||
private Product? _product;
|
||||
private bool _loading;
|
||||
private bool _initialized;
|
||||
private int _qty = 1;
|
||||
private const int MinQty = 1;
|
||||
|
||||
@@ -48,29 +49,35 @@ public partial class ProductDetail : ComponentBase, IDisposable
|
||||
private bool IsInCart => CurrentCartItem is not null;
|
||||
private int CurrentCartQuantity => CurrentCartItem?.Quantity ?? 0;
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
_isAuthenticated = await AuthService.IsAuthenticatedAsync();
|
||||
await Cart.EnsureInitializedAsync();
|
||||
Cart.OnChange += HandleCartChanged;
|
||||
|
||||
|
||||
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
if (!_initialized || id <= 0)
|
||||
return;
|
||||
|
||||
await LoadProductAsync();
|
||||
}
|
||||
|
||||
private async Task LoadProductAsync()
|
||||
{
|
||||
_loading = true;
|
||||
_product = await ProductService.GetByIdAsync(id);
|
||||
_loading = false;
|
||||
|
||||
if (_product is not null)
|
||||
{
|
||||
_galleryItems = BuildGalleryItems(_product);
|
||||
_selectedGalleryImage = _galleryItems.FirstOrDefault();
|
||||
_categoryPaths = _product.Categories;
|
||||
UpdateBreadcrumb();
|
||||
_qty = Math.Clamp(CurrentCartItem?.Quantity ?? _qty, MinQty, MaxQty);
|
||||
_qty = Math.Clamp(CurrentCartItem?.Quantity ?? MinQty, MinQty, Math.Max(MinQty, MaxQty));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -78,19 +85,16 @@ public partial class ProductDetail : ComponentBase, IDisposable
|
||||
_categoryPaths = Array.Empty<ProductCategoryPathInfo>();
|
||||
_breadcrumbItems.Clear();
|
||||
}
|
||||
|
||||
StateHasChanged();
|
||||
await base.OnInitializedAsync();
|
||||
}
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
await base.OnAfterRenderAsync(firstRender);
|
||||
if (firstRender)
|
||||
{
|
||||
// بارگذاری نرخ VAT
|
||||
await VAT.LoadAsync();
|
||||
}
|
||||
|
||||
await base.OnAfterRenderAsync(firstRender);
|
||||
}
|
||||
|
||||
private async Task AddToCart()
|
||||
|
||||
Reference in New Issue
Block a user