Add edit functionality for EinAb interactions: implement UpdateInteraction method, extend InteractionDialog for edit mode, and add edit button in InteractionRow

This commit is contained in:
troogs
2026-09-14 16:09:13 +02:00
parent fdd7ea5a40
commit 8b298b37c7
11 changed files with 360 additions and 24 deletions
@@ -76,5 +76,5 @@
<div class="d-flex justify-content-end">
<Button Color="Color.Secondary" Clicked="@ModalService.Hide">Abbrechen</Button>
<Button Color="Color.Primary" Clicked="@AddInteractionAsync" Class="ml-2">OK</Button>
<Button Color="Color.Primary" Clicked="@SaveAsync" Class="ml-2">@(IsEditMode ? "Speichern" : "OK")</Button>
</div>
@@ -1,4 +1,5 @@
using Blazorise;
using FoodsharingSiegen.Contracts;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Contracts.Enums;
using FoodsharingSiegen.Server.BaseClasses;
@@ -7,7 +8,7 @@ using Microsoft.AspNetCore.Components;
namespace FoodsharingSiegen.Server.Dialogs
{
public record InteractionDialogParameter(InteractionType Type, Guid ProspectId, string HeaderText, Func<Task> OnSuccess);
public record InteractionDialogParameter(InteractionType Type, Guid ProspectId, string HeaderText, Func<Task> OnSuccess, Interaction? ExistingInteraction = null);
public partial class InteractionDialog : FsBase
{
@@ -44,6 +45,19 @@ namespace FoodsharingSiegen.Server.Dialogs
[Parameter]
public bool ShowNotNeeded { get; set; }
[Parameter]
public bool IsEditMode { get; set; }
#endregion
#region Public Method IsEditable
public static bool IsEditable(InteractionType type) => type switch
{
InteractionType.EinAb => true,
_ => false
};
#endregion
#region Public Method ShowAsync
@@ -93,12 +107,29 @@ namespace FoodsharingSiegen.Server.Dialogs
_ => false
};
var interaction = new Interaction
{
Type = parameter.Type,
Date = DateTime.UtcNow,
ProspectID = parameter.ProspectId
};
var isEditMode = parameter.ExistingInteraction != null;
var interaction = isEditMode
? new Interaction
{
Id = parameter.ExistingInteraction!.Id,
Type = parameter.ExistingInteraction.Type,
Date = parameter.ExistingInteraction.Date,
ProspectID = parameter.ExistingInteraction.ProspectID,
UserID = parameter.ExistingInteraction.UserID,
Info1 = parameter.ExistingInteraction.Info1,
Info2 = parameter.ExistingInteraction.Info2,
Feedback = parameter.ExistingInteraction.Feedback,
FeedbackInfo = parameter.ExistingInteraction.FeedbackInfo,
Alert = parameter.ExistingInteraction.Alert,
NotNeeded = parameter.ExistingInteraction.NotNeeded,
Created = parameter.ExistingInteraction.Created
}
: new Interaction
{
Type = parameter.Type,
Date = DateTime.UtcNow,
ProspectID = parameter.ProspectId
};
await modalService.Show<InteractionDialog>(parameter.HeaderText, p =>
{
@@ -108,29 +139,41 @@ namespace FoodsharingSiegen.Server.Dialogs
p.Add(nameof(Info2Name), info2Name);
p.Add(nameof(ShowAlert), showAlert);
p.Add(nameof(ShowNotNeeded), showNotNeeded);
p.Add(nameof(IsEditMode), isEditMode);
p.Add(nameof(OnSuccess), parameter.OnSuccess);
});
}
#endregion
#region Private Method AddInteractionAsync
#region Private Method SaveAsync
/// <summary>
/// Adds a new interaction for the current user using the ProspectService and hides the modal.
/// Saves the interaction (add or update) and hides the modal on success.
/// </summary>
/// <returns>
/// A task representing the asynchronous operation.
/// </returns>
private async Task AddInteractionAsync()
private async Task SaveAsync()
{
Interaction.UserID = CurrentUser.Id;
OperationResult<Interaction> result;
var addR = await ProspectService.AddInteraction(Interaction);
if (IsEditMode)
{
result = await ProspectService.UpdateInteraction(Interaction);
}
else
{
Interaction.UserID = CurrentUser.Id;
result = await ProspectService.AddInteraction(Interaction);
}
await ModalService.Hide();
if (addR.Success && OnSuccess != null) await OnSuccess.Invoke();
if (result.Success)
{
await ModalService.Hide();
if (OnSuccess != null) await OnSuccess.Invoke();
}
else
{
await Notification.Error(result.Exception?.Message ?? "Unbekannter Fehler beim Speichern.", "Fehler");
}
}
#endregion