using Blazorise;
using Microsoft.AspNetCore.Components;
using Server.Data;
using Address = Server.Model.Address;
namespace Server.Components.Dialogs
{
public partial class AddressSelector : ComponentBase
{
#region Dependencies
[Inject]
private IModalService ModalService { get; set; } = null!;
#endregion
#region Parameters
///
/// Gets or sets the delegate that will be invoked when an address is selected.
///
///
/// This property expects a delegate where the input parameter is an
/// and the return type is a . It allows customization of what should happen when a user selects an address
/// from the AddressSelector component.
///
[Parameter]
public Func
? OnSelected { get; set; }
#endregion
#region Private Properties
private Address? SelectedCustomer { get; set; }
#endregion
#region Override SetParametersAsync
////
public override async Task SetParametersAsync(ParameterView parameters)
{
parameters.SetParameterProperties(this);
await CustomerData.LoadAsync();
await base.SetParametersAsync(ParameterView.Empty);
}
#endregion
#region Public Method ShowAsync
///
/// Displays the AddressSelector component as a modal dialog asynchronously.
///
/// The service used to show the modal dialog.
/// The callback to invoke when an address is selected.
///
/// A Task that represents the asynchronous operation of showing the modal dialog.
///
public static Task ShowAsync(IModalService modalService, Func onSelected)
{
return modalService.Show(builder => { builder.Add(x => x.OnSelected, onSelected); });
}
#endregion
#region Private Method SelectedAsync
///
/// Handles the selection of an address asynchronously.
/// Invokes the OnSelected callback with the selected address
/// and hides the modal service upon completion.
///
///
/// A Task that represents the asynchronous operation.
///
private async Task SelectedAsync()
{
if (SelectedCustomer != null && OnSelected != null) await OnSelected.Invoke(SelectedCustomer);
await ModalService.Hide();
}
#endregion
}
}