Move edit prospect dialog logic

This commit is contained in:
Andre Beging
2025-03-28 18:49:38 +01:00
parent 97deed7d90
commit d754da76cd
15 changed files with 173 additions and 170 deletions

View File

@@ -0,0 +1,106 @@
using Blazorise;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.BaseClasses;
using FoodsharingSiegen.Server.Data.Service;
using Microsoft.AspNetCore.Components;
namespace FoodsharingSiegen.Server.Dialogs
{
/// <summary>
/// The add prospect modal class (a. beging, 31.05.2022)
/// </summary>
public partial class EditProspectDialog : FsBase
{
#region Dependencies
/// <summary>
/// Gets or sets the value of the prospect service (ab)
/// </summary>
[Inject]
public ProspectService ProspectService { get; set; } = null!;
#endregion
#region Parameters
/// <summary>
/// Gets or sets the value of the is update mode (ab)
/// </summary>
[Parameter]
public bool IsUpdateMode { get; set; }
[Parameter]
public Func<Task>? OnSuccess { get; set; }
/// <summary>
/// Gets or sets the value of the on add (ab)
/// </summary>
[Parameter]
public EventCallback<Prospect> OnAdd { get; set; }
/// <summary>
/// Gets or sets the value of the on update (ab)
/// </summary>
[Parameter]
public EventCallback<Prospect> OnUpdate { get; set; }
/// <summary>
/// Gets or sets the value of the prospect (ab)
/// </summary>
[Parameter]
public Prospect Prospect { get; set; } = new();
/// <summary>
/// Gets or sets the value of the save button text (ab)
/// </summary>
[Parameter]
public string? SaveButtonText { get; set; }
#endregion
#region Public Method ShowAsync
/// <summary>
/// Displays the AddProspectModal dialog asynchronously, allowing for the creation or editing of a prospect.
/// </summary>
/// <param name="modalService">The modal service used to display the dialog.</param>
/// <param name="onSuccess">Callback to be invoked upon successful addition or update of a prospect.</param>
/// <param name="prospect">The prospect to be edited, or null for creating a new prospect.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public static async Task ShowAsync(IModalService modalService, Func<Task> onSuccess, Prospect? prospect = null)
{
await modalService.Show<EditProspectDialog>(prospect == null ? "Neuling hinzufügen" : $"{prospect.Name} bearbeiten", p =>
{
p.Add(nameof(Prospect), prospect ?? new Prospect());
p.Add(nameof(IsUpdateMode), prospect != null);
p.Add(nameof(SaveButtonText), prospect == null ? "Hinzufügen" : "Speichern");
p.Add(nameof(OnSuccess), onSuccess);
});
}
#endregion
#region Private Method SaveClick
/// <summary>
/// Saves the click (a. beging, 31.05.2022)
/// </summary>
private async Task SaveClick()
{
if (IsUpdateMode)
{
var updateR = await ProspectService.UpdateAsync(Prospect);
if (updateR.Success && OnSuccess != null) await OnSuccess.Invoke();
}
else
{
var addR = await ProspectService.AddProspectAsync(Prospect);
if (addR.Success && OnSuccess != null) await OnSuccess.Invoke();
}
await ModalService.Hide();
}
#endregion
}
}