GuestDocument - Add edit mode.

Added Edit mode for GuestDocument in GuestDocument details page.
This commit is contained in:
2026-09-10 19:34:46 +02:00
parent a1a55aa396
commit 491ecc4cd1
4 changed files with 175 additions and 16 deletions
@@ -9,6 +9,7 @@
@inject NavigationManager Navigation
@inject IGuestDocumentEntityService guestDocService
@inject IStayEntryEntityService stayService
@inject IDialogService DialogService
<PageTitle>Somnia - Guest Docs - Details</PageTitle>
@@ -16,14 +17,28 @@
<MudStack Row="true" Class="my-4">
<MudText Typo="Typo.h4" GutterBottom="true">Guest Document: @docId - @documentModel?.FullDocument</MudText>
<MudSpacer />
@if (_isEditMode)
{
<MudButton Variant="Variant.Filled" Color="Color.Error" Size="Size.Small" StartIcon="@Icons.Material.Filled.Cancel"
OnClick="() => BtnCancelEdit()" Disabled="!_isEditMode">
@LanguageLocale.general_btnCancel
</MudButton>
<MudButton Variant="Variant.Filled" Color="Color.Success" Size="Size.Small" StartIcon="@Icons.Material.Filled.Save"
OnClick="() => BtnSaveEdit()" Disabled="!_isEditMode">
@LanguageLocale.general_btnSave
</MudButton>
}
@if (!_isEditMode)
{
<MudButton Variant="Variant.Filled" Color="Color.Success" Size="Size.Small" StartIcon="@Icons.Material.Filled.Edit"
OnClick="() => BtnEdit()" Disabled>
OnClick="() => BtnEnableEdit()" Disabled="_isEditMode">
@LanguageLocale.general_btnEdit
</MudButton>
<MudButton Variant="Variant.Filled" Color="Color.Default" Size="Size.Small"
StartIcon="@Icons.Material.Filled.ArrowBack" OnClick="() => BtnReturn()">
@LanguageLocale.general_btnReturn
</MudButton>
}
</MudStack>
@if (documentModel is not null)
@@ -32,10 +47,11 @@
Centered>
<MudTabPanel Text="Details" Icon="@Icons.Material.Filled.Info">
<GuestDocForm @ref="_form" Model="documentModel" ReadOnly="true" />
<GuestDocForm @ref="_form" Model="documentModel" ReadOnly="!_isEditMode" />
</MudTabPanel>
<MudTabPanel Text="Stays" ToolTip="Guest stays" OnClick="LoadTabGuestStays" Icon="@Icons.Material.Filled.Bed">
<MudTabPanel Text="Stays" ToolTip="Guest stays" Icon="@Icons.Material.Filled.Bed" OnClick="LoadTabGuestStays"
Disabled="_isEditMode">
<MudText>Total Stays: @(guestStaysLoading ? "Loading..." : guestStayList.Count())</MudText>
<MudTable Items="@guestStayList" Hover="true" Loading="@guestStaysLoading" Class="mt-2" Elevation="2">
<HeaderContent>
@@ -87,8 +103,16 @@
IEnumerable<StayEntryDTO> guestStayList = [];
bool guestStaysLoading = true;
private bool _isEditMode = false;
protected override async Task OnInitializedAsync()
{
//Get model from db.
await LoadGuestDocumentAsync();
}
private async Task LoadGuestDocumentAsync()
{
//Get model from db.
documentModel = await guestDocService.GetLastVersionDtoByIdAsync(docId);
@@ -121,9 +145,68 @@
#region Event Handlers
private void BtnEdit()
private void BtnEnableEdit()
{
userMessage.ShowMessage("Not implemented yet!", MessageKind.Info);
Debug.Assert(_isEditMode == false);
userMessage.ShowMessage(LanguageLocale.page_GuestDocDetail_msgEditEnabled, MessageKind.Normal);
_isEditMode = true;
}
private async Task BtnCancelEdit()
{
Debug.Assert(_isEditMode == true);
var result = await DialogService.ShowMessageBoxAsync(
LanguageLocale.general_confirmTitle, LanguageLocale.page_GuestDocDetail_msgConfirmCancelEdit,
yesText: "OK", cancelText: LanguageLocale.general_btnCancel);
if (result == null || result.Value == false)
{
// Cancel cancel -> Continue with edit.
return;
}
//Cancel edit -> Reload data from DB.
await LoadGuestDocumentAsync();
userMessage.ShowMessage(LanguageLocale.general_msgEditCanceled, MessageKind.Normal);
_isEditMode = false;
}
private async Task BtnSaveEdit()
{
Debug.Assert(_isEditMode == true && documentModel is not null);
var confirmResult = await DialogService.ShowMessageBoxAsync(
LanguageLocale.general_confirmTitle, LanguageLocale.page_GuestDocDetail_msgConfirmSaveEdit,
yesText: "OK", cancelText: LanguageLocale.general_btnCancel);
if (confirmResult == null || confirmResult.Value == false)
{
// Cancel save -> Continue with edit.
return;
}
//Show Loading dialog...
var loadingDialog = await DialogService.ShowAsync<LoadingDialog>();
//Save edit.
Result updateResult = await guestDocService.UpdateDocumentAsync(documentModel);
if (!updateResult.IsSuccess)
{
string errorMsg = string.Format(LanguageLocale.page_GuestDocDetail_msgEditError, documentModel.FullDocument);
userMessage.ShowResultWithMessage(updateResult, errorMsg);
loadingDialog.Close();
return;
}
//Reload data from DB.
await LoadGuestDocumentAsync();
loadingDialog.Close();
userMessage.ShowMessage(LanguageLocale.general_msgEditSaved, MessageKind.Success);
_isEditMode = false;
}
private void BtnReturn()
@@ -144,13 +144,13 @@
Result createResult = await guestDocService.CreateDocumentAsync(model);
if (!createResult.IsSuccess)
{
string errorMsg = $"Error creating Document '{model.DocumentNum}'";
string errorMsg = $"Error creating Document '{model.FullDocument}'";
userMessage.ShowResultWithMessage(createResult, errorMsg);
loadingDialog.Close();
return;
}
userMessage.ShowMessage($"Document '{model.DocumentNum}' Created!", MessageKind.Success);
userMessage.ShowMessage($"Document '{model.FullDocument}' Created!", MessageKind.Success);
Navigation.NavigateTo(GuestConsts.Page_GuestDocs_List);
}
@@ -182,6 +182,9 @@
<data name="general.wildcardHelperText" xml:space="preserve"><value>Use '%' as wildcard</value></data>
<data name="general.dateStart" xml:space="preserve"><value>Date Start</value></data>
<data name="general.dateEnd" xml:space="preserve"><value>Date End</value></data>
<data name="general.loading" xml:space="preserve"><value>Loading...</value></data>
<data name="general.msgEditSaved" xml:space="preserve"><value>Changes saved...</value></data>
<data name="general.msgEditCanceled" xml:space="preserve"><value>Edit Canceled!</value></data>
<data name="general.address.address" xml:space="preserve"><value>Address</value></data>
<data name="general.address.city" xml:space="preserve"><value>City</value></data>
@@ -224,6 +227,11 @@
<data name="page.GuestDocNew.msgAlreadyExists" xml:space="preserve"><value>This Guest Document Already exists!!</value></data>
<data name="page.GuestDocNew.btnGoExistingDoc" xml:space="preserve"><value>Go to existing document</value></data>
<data name="page.GuestDocDetail.msgEditEnabled" xml:space="preserve"><value>Entered Edit mode</value></data>
<data name="page.GuestDocDetail.msgConfirmSaveEdit" xml:space="preserve"><value>Save changes to GuestDocument</value></data>
<data name="page.GuestDocDetail.msgConfirmCancelEdit" xml:space="preserve"><value>Cancel edit. Changes will be lost!</value></data>
<data name="page.GuestDocDetail.msgEditError" xml:space="preserve"><value>Error saving Document '{0}'</value></data>
<data name="page.StayEntryList.pageTitle" xml:space="preserve"><value>Stay Entries</value></data>
<data name="page.StayEntryList.entryList" xml:space="preserve"><value>Entry List</value></data>
<data name="page.StayEntryList.btnNewStay" xml:space="preserve"><value>New Stay</value></data>
@@ -76,9 +76,77 @@ public class GuestDocumentEntityService(
}
}
public Task<Result> UpdateDocumentAsync(GuestDocumentDTO dto)
public async Task<Result> UpdateDocumentAsync(GuestDocumentDTO dto)
{
throw new NotImplementedException();
var valResult = dtoValidator.Validate(dto);
if (!valResult.IsValid)
{
logger.LogWarning("GuestDocumentService - UpdateDocument: Dto validation failed. Fix the errors: {errorList}", valResult.Errors);
return Result.Fail(ResultCode.FailedValidation);
}
try
{
using var dbContext = contextFactory.CreateDbContext();
//Get current entity from DB.
GuestDocument? dbItem = await dbContext.FindAsync<GuestDocument>(dto.Id, dto.Version);
if (dbItem == null)
{
logger.LogWarning("GuestDocumentService - UpdateDocument: Error updating GuestDocument (ID: '{id}'): GuestDocument not found!", dto.FullKey);
return Result.Fail(ResultCode.NotFound);
}
//Dto must be valid, so null warings shouldn't matter.
GuestAddress addressItem = new()
{
Address = dto.Address,
AddressCity = dto.AddressCity,
AddressProvince = dto.AddressProvince,
AddressState = dto.AddressState,
AddressCountry = dto.AddressCountry!,
ZipCode = dto.ZipCode,
};
DateTime updateTime = DateTime.UtcNow;
GuestDocument updatedItem = new()
{
Id = dbItem.Id,
Version = dbItem.Version,
DocumentType = dbItem.DocumentType,
DocumentNum = dbItem.DocumentNum,
IssueDate = dto.IssueDate != null ? DateOnly.FromDateTime(dto.IssueDate!.Value) : null,
SupportNum = dto.SupportNum,
Nationality = dto.Nationality!,
Name = dto.Name!,
Surname = dto.Surname!,
Surname2 = dto.Surname2,
BirthDate = DateOnly.FromDateTime(dto.BirthDate!.Value),
Gender = dto.Gender,
PhoneNumber = dto.PhoneNumber,
EmailAddress = dto.EmailAddress,
AddressDetails = addressItem,
CreateTime = dbItem.CreateTime,
UpdateTime = updateTime,
IsLastVersion = dbItem.IsLastVersion,
};
dbContext.Entry(dbItem).CurrentValues.SetValues(updatedItem);
var result = await dbContext.SaveChangesAsync();
if (result != 1)
{
logger.LogWarning("GuestDocumentService - UpdateDocument: Unexpected updated entity count: {count}", result);
}
logger.LogInformation("GuestDocumentService - UpdateDocument: GuestDocument Updated OK {id}", dbItem.GetFullKey());
return Result.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "GuestDocumentService - UpdateDocument: Error updating GuestDocument: '{id}'", dto.FullKey);
return Result.FailUnknownError();
}
}
public Task<Result> DeleteDocumentAsync(GuestDocumentDTO dto)