Refactor interaction and user deletion logic

Consolidated repeated methods for interaction/user deletion into reusable components to improve maintainability. Introduced a `ConfirmDialog` for consistent confirmation UI and streamlined associated logic across pages. Removed redundant methods and enhanced admin-specific page security checks.
This commit is contained in:
Andre Beging
2025-03-28 23:55:12 +01:00
parent 83257d1d2a
commit 027a36ce17
12 changed files with 174 additions and 234 deletions

View File

@@ -8,59 +8,83 @@ namespace FoodsharingSiegen.Server.Controls
{
public partial class ProspectContainer
{
[Parameter] public Prospect? Prospect { get; set; }
[Parameter] public ProspectStateFilter StateFilter { get; set; }
[Parameter] public string? CssClass { get; set; }
[Parameter] public Func<Task>? OnDataChanged { get; set; }
private async Task AddInteraction(InteractionType type)
{
if (Prospect != null && OnDataChanged != null)
{
var headerText = $"{type.Translate(AppSettings)} eintragen";
await InteractionDialog.ShowAsync(ModalService, new(type, Prospect.Id, headerText, OnDataChanged));
}
}
private List<Interaction> GetTyped(InteractionType type)
{
return Prospect?.Interactions?.Where(x => x.Type == type).ToList() ?? [];
}
private async Task EditProspectAsync()
{
await EditProspectDialog.ShowAsync(ModalService, () => InvokeAsync(StateHasChanged), Prospect);
}
#region Dependencies
/// <summary>
/// Gets or sets the value of the prospect service (ab)
/// </summary>
[Inject]
public ProspectService ProspectService { get; set; } = null!;
#endregion
#region Parameters
[Parameter]
public string? CssClass { get; set; }
[Parameter]
public Func<Task>? OnDataChanged { get; set; }
[Parameter]
public Prospect? Prospect { get; set; }
[Parameter]
public ProspectStateFilter StateFilter { get; set; }
#endregion
#region Private Method AddInteraction
private async Task AddInteraction(InteractionType type)
{
if (Prospect != null && OnDataChanged != null)
{
var headerText = $"{type.Translate(AppSettings)} eintragen";
await InteractionDialog.ShowAsync(ModalService, new(type, Prospect.Id, headerText, OnDataChanged));
}
}
#endregion
#region Private Method EditProspectAsync
private async Task EditProspectAsync()
{
await EditProspectDialog.ShowAsync(ModalService, () => InvokeAsync(StateHasChanged), Prospect);
}
#endregion
#region Private Method GetTyped
private List<Interaction> GetTyped(InteractionType type)
{
return Prospect?.Interactions?.Where(x => x.Type == type).ToList() ?? [];
}
#endregion
#region Private Method RemoveInteraction
/// <summary>
/// Removes a specified interaction by its identifier. Displays a confirmation dialog to the user before performing the removal.
/// </summary>
/// <param name="arg">The unique identifier of the interaction to be removed.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
private async Task RemoveInteraction(Guid arg)
{
var type = Prospect?.Interactions.FirstOrDefault(x => x.Id == arg)?.Type;
var typeName = type != null ? type.Value.Translate(AppSettings) : "Interaktion";
var confirm = await Message.Confirm($"{typeName} wirklich entfernen?", "Bestätigen", o =>
{
o.ConfirmButtonText = "Ja, wirklich!";
o.CancelButtonText = "Abbrechen";
o.ShowMessageIcon = false;
});
if (confirm)
await ConfirmDialog.ShowAsync(ModalService, "Bestätigen", $"{typeName} wirklich entfernen?", async () =>
{
var removeR = await ProspectService.RemoveInteraction(arg);
if(removeR.Success && OnDataChanged != null) await OnDataChanged();
}
if (removeR.Success && OnDataChanged != null) await OnDataChanged();
});
}
#endregion
}
}