Files
SomniaGuestLog/src/Somnia.GuestLog/Components/Controls/AddressQueryDialog.razor
T
Daniel-Garmig 511bd023f2 GuestLog - User message improvements
- Added UserMessageService to have common methods for showing messages and errors to the user.
- Added Result record so services can return status on operation error (so it can be shown in the UI messages).
- Updated services to use the new Result record.
- Updated pages to use the new IUserMessageService.
2026-08-23 21:57:26 +02:00

162 lines
6.0 KiB
Plaintext

@using Somnia.GuestLog.Models.DTOs;
@using Somnia.GuestLog.Services
@using Somnia.GuestLog.Services.AddressQuery;
@inject ILogger<AddressQueryDialog> logger;
@inject IAddressQueryFactory addressQueryFactory;
@inject IUserMessageService userMessage
<MudDialog @ref="@DialogRef">
<TitleContent>
<MudText Typo="Typo.h6">
<MudIcon Icon="@Icons.Material.Filled.NotListedLocation" Class="mr-3 mb-n1" />
@LanguageLocale.control_AddressQueryDialog_dialogTitle
</MudText>
</TitleContent>
<DialogContent>
<MudStack row>
<MudTextField @bind-Value="SearchText" Label="@LanguageLocale.control_AddressQueryDialog_searchText"
Required />
<MudIconButton Icon="@Icons.Material.Filled.Search" OnClick="btnSearchAddress" />
</MudStack>
<MudPaper Class="mt-4 pa-2">
@if (addressResults.Count == 0)
{
<MudText Typo="Typo.body1">@LanguageLocale.general_noResults</MudText>
}
<MudList T="AddressResultDTO" @bind-SelectedValue="SelectedAddressResult"
SelectionMode="SelectionMode.SingleSelection" Color="Color.Info">
@foreach (AddressResultDTO result in addressResults)
{
<MudListItem Value="result">
<ChildContent>
<MudTextField Value="@($"{result.AddressRoad}, {result.HouseNumber}")"
Label="@LanguageLocale.general_address_address" ReadOnly />
<MudStack row>
<MudTextField Value="@result.AddressCity" Label="@LanguageLocale.general_address_city"
ReadOnly />
<MudTextField Value="@result.AddressProvince"
Label="@LanguageLocale.general_address_province" ReadOnly />
<MudTextField Value="@result.AddressState" Label="@LanguageLocale.general_address_state"
ReadOnly />
</MudStack>
<MudStack row>
<MudTextField Value="@result.AddressCountry" Label="@LanguageLocale.general_address_country"
ReadOnly />
<MudTextField Value="@result.ZipCode" Label="@LanguageLocale.general_address_zipCode"
ReadOnly />
</MudStack>
</ChildContent>
</MudListItem>
}
</MudList>
</MudPaper>
</DialogContent>
<DialogActions>
<MudButton OnClick="Cancel">
@LanguageLocale.general_btnCancel
</MudButton>
<MudButton Color="Color.Primary" OnClick="btnSelectAddress">
@LanguageLocale.control_AddressQueryDialog_btnSelectAddress
</MudButton>
</DialogActions>
</MudDialog>
@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<AddressResultDTO> 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<AddressResultDTO?>(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<IDialogReference> ShowAsync(DialogOptions? options = null)
{
_reference = await DialogRef.ShowAsync(options: options);
return _reference;
}
public async Task CloseAsync()
{
await DialogRef.CloseAsync();
_reference = null;
}
}