using Blazorise; using FoodsharingSiegen.Contracts.Entity; using FoodsharingSiegen.Contracts.Enums; using FoodsharingSiegen.Server.Data.Service; using FoodsharingSiegen.Server.Dialogs; using FoodsharingSiegen.Server.Service; using Microsoft.AspNetCore.Components; namespace FoodsharingSiegen.Server.Controls; public partial class ProspectSortControl { [Inject] private IModalService ModalService { get; set; } = null!; [Inject] private LocalStorageService LocalStorageService { get; set; } = null!; [Parameter] public ProspectSortOption CurrentSort { get; set; } = ProspectSortOption.NameAscending; [Parameter] public EventCallback CurrentSortChanged { get; set; } [Parameter] public EventCallback OnSortChanged { get; set; } [Parameter] public string? StorageKey { get; set; } protected override async Task OnInitializedAsync() { if (!string.IsNullOrEmpty(StorageKey)) { var savedSort = await LocalStorageService.GetItem(StorageKey); if (savedSort.HasValue) { CurrentSort = savedSort.Value; await CurrentSortChanged.InvokeAsync(CurrentSort); } } await base.OnInitializedAsync(); } private async Task OpenSortDialogAsync() { await ProspectSortDialog.ShowAsync(ModalService, CurrentSort, async option => { await UpdateSortAsync(option); }); } private bool HasCustomSort => CurrentSort != ProspectSortOption.NameAscending; private string CurrentSortText => CurrentSort switch { ProspectSortOption.NameDescending => "Sortierung: Name (absteigend)", ProspectSortOption.ModifiedAscending => "Sortierung: Zuletzt geƤndert (aufsteigend)", ProspectSortOption.ModifiedDescending => "Sortierung: Zuletzt geaendert (absteigend)", _ => string.Empty }; private async Task ResetSortAsync() { await UpdateSortAsync(ProspectSortOption.NameAscending); } private async Task UpdateSortAsync(ProspectSortOption option) { CurrentSort = option; if (!string.IsNullOrEmpty(StorageKey)) { await LocalStorageService.SetItem(StorageKey, CurrentSort); } await CurrentSortChanged.InvokeAsync(CurrentSort); await OnSortChanged.InvokeAsync(); } }