Pridani info o tom ze jsi neco pridal do kosiku
This commit is contained in:
@@ -1,17 +1,30 @@
|
|||||||
@page "/"
|
@page "/"
|
||||||
|
|
||||||
@using Microsoft.EntityFrameworkCore
|
@using Microsoft.EntityFrameworkCore
|
||||||
@using Web_Jarmark.Data
|
@using Web_Jarmark.Data
|
||||||
@using Web_Jarmark.Data.Models
|
@using Web_Jarmark.Data.Models
|
||||||
@using Web_Jarmark.Components.Pages.Shared
|
@using Web_Jarmark.Components.Pages.Shared
|
||||||
|
|
||||||
@inject ApplicationDbContext DbContext
|
@inject ApplicationDbContext DbContext
|
||||||
@inject IHttpContextAccessor HttpContextAccessor
|
@inject IHttpContextAccessor HttpContextAccessor
|
||||||
@inject PersistentComponentState ApplicationState
|
@inject PersistentComponentState ApplicationState
|
||||||
|
|
||||||
@implements IDisposable
|
@implements IDisposable
|
||||||
|
|
||||||
<PageTitle>Školní jarmark | Katalog</PageTitle>
|
<PageTitle>Školní jarmark | Katalog</PageTitle>
|
||||||
|
|
||||||
<a href="/kosik" class="catalog-cart-button">
|
<a href="/kosik" class="catalog-cart-button">
|
||||||
🛒 Zobrazit košík
|
🛒 Zobrazit košík
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
@if (showNotification)
|
||||||
|
{
|
||||||
|
<div class="cart-notification" role="status" aria-live="polite">
|
||||||
|
<span class="notification-icon">✓</span>
|
||||||
|
<span>@notificationMessage</span>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
<main class="market-shell">
|
<main class="market-shell">
|
||||||
|
|
||||||
<section class="market-heading">
|
<section class="market-heading">
|
||||||
@@ -46,17 +59,92 @@
|
|||||||
@foreach (var product in products)
|
@foreach (var product in products)
|
||||||
{
|
{
|
||||||
<ProductCard @key="product.Id"
|
<ProductCard @key="product.Id"
|
||||||
Product="product" OnAddToCart="HandleAddToCart" />
|
Product="product"
|
||||||
|
OnAddToCart="HandleAddToCart" />
|
||||||
}
|
}
|
||||||
</section>
|
</section>
|
||||||
}
|
}
|
||||||
|
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.cart-notification {
|
||||||
|
position: fixed;
|
||||||
|
top: 25px;
|
||||||
|
right: 25px;
|
||||||
|
z-index: 9999;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 12px;
|
||||||
|
padding: 16px 22px;
|
||||||
|
background: #ffffff;
|
||||||
|
color: #222222;
|
||||||
|
border: 1px solid #d9eadf;
|
||||||
|
border-radius: 12px;
|
||||||
|
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.14);
|
||||||
|
font-size: 14px;
|
||||||
|
font-weight: 650;
|
||||||
|
animation: notification-in 0.25s ease-out, notification-out 1s ease-in 2.5s forwards;
|
||||||
|
}
|
||||||
|
|
||||||
|
.notification-icon {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
width: 27px;
|
||||||
|
height: 27px;
|
||||||
|
flex-shrink: 0;
|
||||||
|
border-radius: 50%;
|
||||||
|
background: #e2f7e9;
|
||||||
|
color: #168344;
|
||||||
|
font-size: 17px;
|
||||||
|
font-weight: 900;
|
||||||
|
}
|
||||||
|
|
||||||
|
@@keyframes notification-in {
|
||||||
|
from {
|
||||||
|
opacity: 0;
|
||||||
|
transform: translateY(-10px);
|
||||||
|
}
|
||||||
|
|
||||||
|
to {
|
||||||
|
opacity: 1;
|
||||||
|
transform: translateY(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@@keyframes notification-out {
|
||||||
|
from {
|
||||||
|
opacity: 1;
|
||||||
|
transform: translateY(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
to {
|
||||||
|
opacity: 0;
|
||||||
|
transform: translateY(-12px);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@@media (max-width: 500px) {
|
||||||
|
.cart-notification {
|
||||||
|
top: 15px;
|
||||||
|
right: 12px;
|
||||||
|
left: 12px;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
private List<Product> products = new();
|
private List<Product> products = new();
|
||||||
private bool isLoading = true;
|
private bool isLoading = true;
|
||||||
private string? cartToken;
|
private string? cartToken;
|
||||||
|
|
||||||
|
private bool showNotification;
|
||||||
|
private string notificationMessage = "";
|
||||||
|
|
||||||
|
private CancellationTokenSource? notificationCts;
|
||||||
|
|
||||||
private PersistingComponentStateSubscription? subscription;
|
private PersistingComponentStateSubscription? subscription;
|
||||||
|
|
||||||
protected override async Task OnInitializedAsync()
|
protected override async Task OnInitializedAsync()
|
||||||
@@ -74,6 +162,7 @@
|
|||||||
{
|
{
|
||||||
isLoading = false;
|
isLoading = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ApplicationState.TryTakeFromJson<string>(
|
if (!ApplicationState.TryTakeFromJson<string>(
|
||||||
"CartToken", out cartToken)
|
"CartToken", out cartToken)
|
||||||
|| string.IsNullOrWhiteSpace(cartToken))
|
|| string.IsNullOrWhiteSpace(cartToken))
|
||||||
@@ -83,32 +172,16 @@
|
|||||||
cartToken = context?.Items["CartToken"] as string
|
cartToken = context?.Items["CartToken"] as string
|
||||||
?? context?.Request.Cookies["CartToken"];
|
?? context?.Request.Cookies["CartToken"];
|
||||||
}
|
}
|
||||||
// if (!ApplicationState.TryTakeFromJson<string>(
|
|
||||||
// "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)
|
private async Task HandleAddToCart(Product product)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(cartToken))
|
if (string.IsNullOrWhiteSpace(cartToken))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// 1. Najdeme košík podle tokenu
|
|
||||||
var cart = await DbContext.Carts
|
var cart = await DbContext.Carts
|
||||||
.FirstOrDefaultAsync(c => c.Token == cartToken);
|
.FirstOrDefaultAsync(c => c.Token == cartToken);
|
||||||
|
|
||||||
// 2. Pokud košík neexistuje, vytvoříme ho
|
|
||||||
if (cart == null)
|
if (cart == null)
|
||||||
{
|
{
|
||||||
cart = new Cart
|
cart = new Cart
|
||||||
@@ -119,12 +192,9 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
DbContext.Carts.Add(cart);
|
DbContext.Carts.Add(cart);
|
||||||
|
|
||||||
// Uložíme košík, abychom získali jeho ID
|
|
||||||
await DbContext.SaveChangesAsync();
|
await DbContext.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Zjistíme, jestli produkt už v košíku je
|
|
||||||
var cartItem = await DbContext.CartItems
|
var cartItem = await DbContext.CartItems
|
||||||
.FirstOrDefaultAsync(ci =>
|
.FirstOrDefaultAsync(ci =>
|
||||||
ci.CartId == cart.Id &&
|
ci.CartId == cart.Id &&
|
||||||
@@ -132,12 +202,10 @@
|
|||||||
|
|
||||||
if (cartItem != null)
|
if (cartItem != null)
|
||||||
{
|
{
|
||||||
// Produkt už existuje – zvýšíme množství
|
|
||||||
cartItem.Quantity++;
|
cartItem.Quantity++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Produkt ještě v košíku není
|
|
||||||
cartItem = new CartItem
|
cartItem = new CartItem
|
||||||
{
|
{
|
||||||
CartId = cart.Id,
|
CartId = cart.Id,
|
||||||
@@ -148,17 +216,55 @@
|
|||||||
DbContext.CartItems.Add(cartItem);
|
DbContext.CartItems.Add(cartItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Aktualizujeme datum změny košíku
|
|
||||||
cart.UpdatedAt = DateTime.UtcNow;
|
cart.UpdatedAt = DateTime.UtcNow;
|
||||||
|
|
||||||
// 5. Uložíme položku do databáze
|
|
||||||
await DbContext.SaveChangesAsync();
|
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()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
notificationCts?.Cancel();
|
||||||
|
notificationCts?.Dispose();
|
||||||
subscription?.Dispose();
|
subscription?.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user