Files
fs-onboarding/FoodsharingSiegen.Server/Dialogs/InteractionDialog.razor.cs
T

198 lines
6.5 KiB
C#

using Blazorise;
using FoodsharingSiegen.Contracts;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Contracts.Enums;
using FoodsharingSiegen.Server.BaseClasses;
using FoodsharingSiegen.Server.Data.Service;
using Microsoft.AspNetCore.Components;
namespace FoodsharingSiegen.Server.Dialogs
{
public record InteractionDialogParameter(InteractionType Type, Guid ProspectId, string HeaderText, Func<Task> OnSuccess, Interaction? ExistingInteraction = null);
public partial class InteractionDialog : 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
[Parameter]
public bool AllowFeedback { get; set; }
[Parameter]
public string? Info1Name { get; set; }
[Parameter]
public string? Info2Name { get; set; }
[Parameter]
public Interaction Interaction { get; set; } = new();
[Parameter]
public Func<Task>? OnSuccess { get; set; }
[Parameter]
public bool ShowAlert { get; set; }
[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
/// <summary>
/// Displays the InteractionDialog modal with the specified parameters.
/// </summary>
/// <param name="modalService">The modal service used to display the dialog.</param>
/// <param name="parameter">The parameters required for the dialog, including type, prospect ID, header text, and success callback.</param>
/// <returns>
/// A task representing the asynchronous operation.
/// </returns>
public static async Task ShowAsync(IModalService modalService, InteractionDialogParameter parameter)
{
var allowFeedback = parameter.Type switch
{
InteractionType.EinAb => true,
_ => false
};
var info1Name = parameter.Type switch
{
InteractionType.EinAb => "Welcher Betrieb?",
InteractionType.Complete => "Kommentar",
InteractionType.IdCheck => "Kommentar",
InteractionType.PrintPass => "Kommentar",
InteractionType.ReleasedForVerification => "Hinweis",
_ => null
};
var info2Name = parameter.Type switch
{
InteractionType.EinAb => "Mit wem?",
_ => null
};
var showAlert = parameter.Type switch
{
InteractionType.EinAb => true,
InteractionType.IdCheck => true,
_ => false
};
var showNotNeeded = parameter.Type switch
{
InteractionType.PrintPass => true,
_ => false
};
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 =>
{
p.Add(nameof(Interaction), interaction);
p.Add(nameof(AllowFeedback), allowFeedback);
p.Add(nameof(Info1Name), info1Name);
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 SaveAsync
/// <summary>
/// Saves the interaction (add or update) and hides the modal on success.
/// </summary>
private async Task SaveAsync()
{
OperationResult<Interaction> result;
if (IsEditMode)
{
result = await ProspectService.UpdateInteraction(Interaction);
}
else
{
Interaction.UserID = CurrentUser.Id;
result = await ProspectService.AddInteraction(Interaction);
}
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
#region Private Method SetFeedbackAsync
/// <summary>
/// Sets the feedback type for the interaction and updates the feedback property.
/// </summary>
/// <param name="feedback">The feedback type to be set for the interaction.</param>
/// <returns>
/// A task representing the asynchronous operation.
/// </returns>
private async Task SetFeedbackAsync(InteractionFeedback feedback)
{
Interaction.Feedback = feedback;
await Task.CompletedTask;
}
#endregion
}
}