@using Somnia.GuestLog.Models.DTOs; @using Somnia.GuestLog.Services @using Somnia.GuestLog.Services.AddressQuery; @inject ILogger logger; @inject IAddressQueryFactory addressQueryFactory; @inject IUserMessageService userMessage @LanguageLocale.control_AddressQueryDialog_dialogTitle @if (addressResults.Count == 0) { @LanguageLocale.general_noResults } @foreach (AddressResultDTO result in addressResults) { } @LanguageLocale.general_btnCancel @LanguageLocale.control_AddressQueryDialog_btnSelectAddress @code { // Dialog Instance injected when open by the DialogService. // This field is null when dialog is not open by the DialogService. [CascadingParameter] private IMudDialogInstance MudDialog { get; set; } = null!; // Reference to the Dialog when open by the ShowAsync Method. (When dialog is used inline). // This field is null when dialog is not open using the internal methods. private IDialogReference? _reference; //Reference to the MudDialog component. Used to manage the internal dialog when the component is inline. private MudDialog DialogRef { get; set; } = null!; private string SearchText { get; set; } = ""; private List addressResults = []; private AddressResultDTO? SelectedAddressResult { get; set; } private void Cancel() { if (MudDialog is not null) { MudDialog.Cancel(); return; } else if (_reference is not null) { DialogRef.CloseAsync(DialogResult.Cancel()); return; } throw new InvalidOperationException("Unable to cancel (close) the dialog!"); } private async Task btnSearchAddress() { if (SearchText.Length == 0) { return; } try { var queryService = addressQueryFactory.GetService(); addressResults = await queryService.SearchAddressAsync(SearchText); } catch (Exception ex) { logger.LogError(ex, "SearchAddress returned an error!"); userMessage.ShowMessage($"Error searching address: {ex.Message}", MessageKind.Error); } } private async Task btnSelectAddress() { if (SelectedAddressResult is null) { userMessage.ShowMessage("Select an address", MessageKind.Warning); return; } DialogResult dialogResult = DialogResult.Ok(SelectedAddressResult); //Close the dialog when opened by DialogService. if (MudDialog != null) { logger.LogDebug("AddressQueryDialog - Closed MudDialogInstance!"); MudDialog.Close(dialogResult); return; } //Close the dialog when is a Inline dialog. else if (_reference is not null) { logger.LogDebug("AddressQueryDialog - Closed inline dialog instance!"); await DialogRef.CloseAsync(dialogResult); return; } //Throw if the dialog can't be closed (as none of the above is true). throw new InvalidOperationException("Dialog cannot be closed! No reference to close!"); } public async Task ShowAsync(DialogOptions? options = null) { _reference = await DialogRef.ShowAsync(options: options); return _reference; } public async Task CloseAsync() { await DialogRef.CloseAsync(); _reference = null; } }