diff --git a/Web-Jarmark/Web-Jarmark/Components/Pages/Katalog.razor b/Web-Jarmark/Web-Jarmark/Components/Pages/Katalog.razor
index b82f270..fff2d58 100644
--- a/Web-Jarmark/Web-Jarmark/Components/Pages/Katalog.razor
+++ b/Web-Jarmark/Web-Jarmark/Components/Pages/Katalog.razor
@@ -1,17 +1,30 @@
@page "/"
+
@using Microsoft.EntityFrameworkCore
@using Web_Jarmark.Data
@using Web_Jarmark.Data.Models
@using Web_Jarmark.Components.Pages.Shared
+
@inject ApplicationDbContext DbContext
@inject IHttpContextAccessor HttpContextAccessor
@inject PersistentComponentState ApplicationState
+
@implements IDisposable
Školní jarmark | Katalog
+
🛒 Zobrazit košík
+
+@if (showNotification)
+{
+
+ ✓
+ @notificationMessage
+
+}
+
@@ -46,17 +59,92 @@
@foreach (var product in products)
{
+ Product="product"
+ OnAddToCart="HandleAddToCart" />
}
}
+
+
+
@code {
private List products = new();
private bool isLoading = true;
private string? cartToken;
+ private bool showNotification;
+ private string notificationMessage = "";
+
+ private CancellationTokenSource? notificationCts;
+
private PersistingComponentStateSubscription? subscription;
protected override async Task OnInitializedAsync()
@@ -74,41 +162,26 @@
{
isLoading = false;
}
+
if (!ApplicationState.TryTakeFromJson(
- "CartToken", out cartToken)
- || string.IsNullOrWhiteSpace(cartToken))
+ "CartToken", out cartToken)
+ || string.IsNullOrWhiteSpace(cartToken))
{
var context = HttpContextAccessor.HttpContext;
cartToken = context?.Items["CartToken"] as string
?? context?.Request.Cookies["CartToken"];
}
- // if (!ApplicationState.TryTakeFromJson(
- // "CartToken", out cartToken))
- // {
- // cartToken = HttpContextAccessor.HttpContext?
- // .Items["CartToken"] as string;
- // }
-
- // subscription = ApplicationState.RegisterOnPersisting(() =>
- // {
- // ApplicationState.PersistAsJson("CartToken", cartToken);
- // return Task.CompletedTask;
- // });
-
}
-
private async Task HandleAddToCart(Product product)
{
if (string.IsNullOrWhiteSpace(cartToken))
return;
- // 1. Najdeme košík podle tokenu
var cart = await DbContext.Carts
.FirstOrDefaultAsync(c => c.Token == cartToken);
- // 2. Pokud košík neexistuje, vytvoříme ho
if (cart == null)
{
cart = new Cart
@@ -119,12 +192,9 @@
};
DbContext.Carts.Add(cart);
-
- // Uložíme košík, abychom získali jeho ID
await DbContext.SaveChangesAsync();
}
- // 3. Zjistíme, jestli produkt už v košíku je
var cartItem = await DbContext.CartItems
.FirstOrDefaultAsync(ci =>
ci.CartId == cart.Id &&
@@ -132,12 +202,10 @@
if (cartItem != null)
{
- // Produkt už existuje – zvýšíme množství
cartItem.Quantity++;
}
else
{
- // Produkt ještě v košíku není
cartItem = new CartItem
{
CartId = cart.Id,
@@ -148,17 +216,55 @@
DbContext.CartItems.Add(cartItem);
}
- // 4. Aktualizujeme datum změny košíku
cart.UpdatedAt = DateTime.UtcNow;
- // 5. Uložíme položku do databáze
await DbContext.SaveChangesAsync();
+
+ // Zobrazíme potvrzení až po úspěšném uložení.
+ await ShowCartNotification(product.Name);
}
+ private async Task ShowCartNotification(string productName)
+ {
+ notificationCts?.Cancel();
+
+ var currentCts = new CancellationTokenSource();
+ notificationCts = currentCts;
+
+ notificationMessage = $"{productName} byl přidán do košíku.";
+ showNotification = true;
+
+ await InvokeAsync(StateHasChanged);
+
+ try
+ {
+ // 2,5 sekundy zobrazení + 1 sekunda postupného zmizení.
+ await Task.Delay(3500, currentCts.Token);
+
+ if (!currentCts.Token.IsCancellationRequested)
+ {
+ showNotification = false;
+ await InvokeAsync(StateHasChanged);
+ }
+ }
+ catch (OperationCanceledException)
+ {
+ // Nové přidání produktu restartovalo čas notifikace.
+ }
+ finally
+ {
+ if (ReferenceEquals(notificationCts, currentCts))
+ {
+ notificationCts = null;
+ currentCts.Dispose();
+ }
+ }
+ }
public void Dispose()
{
+ notificationCts?.Cancel();
+ notificationCts?.Dispose();
subscription?.Dispose();
}
-
}
diff --git a/Web-Jarmark/Web-Jarmark/jarmark.db-shm b/Web-Jarmark/Web-Jarmark/jarmark.db-shm
index fe9ac28..f6bd50d 100644
Binary files a/Web-Jarmark/Web-Jarmark/jarmark.db-shm and b/Web-Jarmark/Web-Jarmark/jarmark.db-shm differ
diff --git a/Web-Jarmark/Web-Jarmark/jarmark.db-wal b/Web-Jarmark/Web-Jarmark/jarmark.db-wal
index e69de29..f3fd530 100644
Binary files a/Web-Jarmark/Web-Jarmark/jarmark.db-wal and b/Web-Jarmark/Web-Jarmark/jarmark.db-wal differ