143 lines
4.9 KiB
C#
143 lines
4.9 KiB
C#
using FoodsharingSiegen.Contracts;
|
|
using FoodsharingSiegen.Contracts.Entity;
|
|
using FoodsharingSiegen.Contracts.Enums;
|
|
using FoodsharingSiegen.Contracts.Model;
|
|
using FoodsharingSiegen.Server.Data.Service;
|
|
using FoodsharingSiegen.Server.Dialogs;
|
|
using FoodsharingSiegen.Server.Service;
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace FoodsharingSiegen.Server.Pages
|
|
{
|
|
/// <summary>
|
|
/// The prospects class (a. beging, 11.04.2022)
|
|
/// </summary>
|
|
public partial class Prospects
|
|
{
|
|
#region Dependencies
|
|
|
|
[Inject]
|
|
private ProspectService ProspectService { get; set; } = null!;
|
|
|
|
/// <summary>
|
|
/// Gets or sets the value of the user service (ab)
|
|
/// </summary>
|
|
[Inject]
|
|
private UserService UserService { get; set; } = null!;
|
|
|
|
#endregion
|
|
|
|
#region Private Properties
|
|
|
|
private ProspectFilter Filter { get; set; } = new();
|
|
|
|
/// <summary>
|
|
/// Gets or sets the value of the prospect list (ab)
|
|
/// </summary>
|
|
private List<Prospect>? ProspectList { get; set; }
|
|
|
|
private ProspectSortOption CurrentSort { get; set; } = ProspectSortOption.NameAscending;
|
|
|
|
#endregion
|
|
|
|
#region Override InitializeDataAsync
|
|
|
|
/// <inheritdoc />
|
|
protected override async Task InitializeDataAsync()
|
|
{
|
|
var filter = await LocalStorageService.GetItem<ProspectFilter>(StorageKeys.ProspectFilter);
|
|
if (filter != null) Filter = filter;
|
|
|
|
// Load prospects
|
|
await LoadProspects();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Private Method CreateProspectAsync
|
|
|
|
/// <summary>
|
|
/// Asynchronously creates a new prospect by displaying the AddProspectModal dialog and refreshing the prospect list.
|
|
/// </summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
private async Task CreateProspectAsync()
|
|
{
|
|
await EditProspectDialog.ShowAsync(ModalService, LoadProspects);
|
|
}
|
|
|
|
private async Task OpenSortDialogAsync()
|
|
{
|
|
await ProspectSortDialog.ShowAsync(ModalService, CurrentSort, async option =>
|
|
{
|
|
CurrentSort = option;
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
});
|
|
}
|
|
|
|
private bool HasCustomSort => CurrentSort != ProspectSortOption.NameAscending;
|
|
|
|
private string CurrentSortText => CurrentSort switch
|
|
{
|
|
ProspectSortOption.NameDescending => "Sortierung: Name (absteigend)",
|
|
ProspectSortOption.ModifiedAscending => "Sortierung: Zuletzt geaendert (aufsteigend) ",
|
|
ProspectSortOption.ModifiedDescending => "Sortierung: Zuletzt geaendert (absteigend) ",
|
|
_ => string.Empty
|
|
};
|
|
|
|
private async Task ResetSortAsync()
|
|
{
|
|
CurrentSort = ProspectSortOption.NameAscending;
|
|
await InvokeAsync(StateHasChanged);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Private Method FilterChangedAsync
|
|
|
|
/// <summary>
|
|
/// Updates the current filter with the specified ProspectFilter object.
|
|
/// </summary>
|
|
/// <param name="arg">The ProspectFilter object containing the updated filter criteria.</param>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
private async Task FilterChangedAsync(ProspectFilter arg)
|
|
{
|
|
Filter = arg;
|
|
await LocalStorageService.SetItem(StorageKeys.ProspectFilter, Filter);
|
|
}
|
|
|
|
private List<Prospect> SortProspects(List<Prospect> prospects)
|
|
{
|
|
return CurrentSort switch
|
|
{
|
|
ProspectSortOption.NameAscending => prospects.OrderBy(x => x.Name, StringComparer.OrdinalIgnoreCase).ToList(),
|
|
ProspectSortOption.NameDescending => prospects.OrderByDescending(x => x.Name, StringComparer.OrdinalIgnoreCase).ToList(),
|
|
ProspectSortOption.ModifiedAscending => prospects.OrderBy(x => x.Modified ?? x.Created).ToList(),
|
|
ProspectSortOption.ModifiedDescending => prospects.OrderByDescending(x => x.Modified ?? x.Created).ToList(),
|
|
_ => prospects
|
|
};
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Private Method LoadProspects
|
|
|
|
/// <summary>
|
|
/// Loads the prospects (a. beging, 11.04.2022)
|
|
/// </summary>
|
|
private async Task LoadProspects()
|
|
{
|
|
var parameter = new GetProspectsParameter
|
|
{
|
|
CannotHaveInteractions = [InteractionType.Complete, InteractionType.Verify, InteractionType.ReleasedForVerification]
|
|
};
|
|
|
|
var prospectsR = await ProspectService.GetProspectsAsync(parameter);
|
|
if (prospectsR.Success) ProspectList = prospectsR.Data;
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |