Files
FrontOffice/src/FrontOffice.Main/Utilities/ShopListScrollRestore.cs
T
masoodafar-web a708727241
Build and Deploy to Kubernetes / build-and-deploy (push) Has been cancelled
feat: enhance product listing and navigation functionality
- Added a wrapper div for product items in the DiscountStore and Store pages to improve layout consistency.
- Implemented state management for URL synchronization and scroll restoration in both product components.
- Refactored product loading logic to streamline data retrieval and improve user experience when navigating between product pages.

These changes enhance the overall functionality and user experience of the product listing pages, ensuring better state management and navigation.
2026-07-26 00:17:55 +03:30

70 lines
2.2 KiB
C#

using System.Text.Json;
using Microsoft.JSInterop;
namespace FrontOffice.Main.Utilities;
/// <summary>
/// اسکرول/فوکوس محصول پس از بازگشت از جزئیات — کلیدهای محدود به لیست فروشگاه (نه لندینگ).
/// </summary>
public static class ShopListScrollRestore
{
public const string StoreKey = "fo:store:scroll";
public const string DiscountKey = "fo:discount:scroll";
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
public static async Task SaveAsync(IJSRuntime js, string storageKey, long productId)
{
double scrollY = 0;
try
{
scrollY = await js.InvokeAsync<double>("eval", "window.scrollY || window.pageYOffset || 0");
}
catch
{
// ignore
}
var payload = JsonSerializer.Serialize(new ScrollPayload(productId, scrollY), JsonOptions);
await js.InvokeVoidAsync("sessionStorage.setItem", storageKey, payload);
}
public static async Task<ScrollPayload?> TakeAsync(IJSRuntime js, string storageKey)
{
try
{
var raw = await js.InvokeAsync<string?>("sessionStorage.getItem", storageKey);
await js.InvokeVoidAsync("sessionStorage.removeItem", storageKey);
if (string.IsNullOrWhiteSpace(raw))
return null;
return JsonSerializer.Deserialize<ScrollPayload>(raw, JsonOptions);
}
catch
{
return null;
}
}
public static async Task RestoreAsync(IJSRuntime js, ScrollPayload payload)
{
try
{
await js.InvokeVoidAsync("eval",
$@"(function(){{
var el = document.getElementById('shop-product-{payload.ProductId}');
if (el) {{ el.scrollIntoView({{ block: 'center', behavior: 'instant' }}); return; }}
window.scrollTo(0, {payload.ScrollY.ToString(System.Globalization.CultureInfo.InvariantCulture)});
}})()");
}
catch
{
// ignore
}
}
public sealed record ScrollPayload(long ProductId, double ScrollY);
}