From cd0a8240643ff357b1d22531209a85fb7e481519 Mon Sep 17 00:00:00 2001 From: OlgunR Date: Mon, 20 Apr 2026 10:52:05 +0200 Subject: [PATCH] Refactor grid logic into BandGridBase base class Move shared state and methods from CatalogsGrid and MassDataGrid into BandGridBase. This centralizes edit context handling, validation, popup header logic, row editing/deleting, and layout feedback, reducing duplication and improving maintainability. Individual grid components now only override OnEditFieldChanged for custom validation. --- .../Components/BandGridBase.cs | 59 +++++++++++++++++++ .../Components/CatalogsGrid.razor | 53 +---------------- .../Components/MassDataGrid.razor | 53 +---------------- 3 files changed, 61 insertions(+), 104 deletions(-) diff --git a/DbFirst.BlazorWebApp/Components/BandGridBase.cs b/DbFirst.BlazorWebApp/Components/BandGridBase.cs index 47766cd..c04cc7d 100644 --- a/DbFirst.BlazorWebApp/Components/BandGridBase.cs +++ b/DbFirst.BlazorWebApp/Components/BandGridBase.cs @@ -2,6 +2,7 @@ using DbFirst.BlazorWebApp.Services; using DevExpress.Blazor; using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Forms; using Microsoft.AspNetCore.Components.Rendering; namespace DbFirst.BlazorWebApp.Components; @@ -27,6 +28,15 @@ public abstract class BandGridBase : ComponentBase protected SizeMode _sizeMode = SizeMode.Medium; protected static readonly List _sizeModes = Enum.GetValues().ToList(); + protected string? errorMessage; + protected string? infoMessage; + protected bool isLoading; + protected bool hasLoaded; + protected EditContext? editContext; + protected ValidationMessageStore? validationMessageStore; + protected string popupHeaderText = "Edit"; + protected int _focusedVisibleIndex; + private const string LayoutType = "GRID_BANDS"; // --- Lifecycle --- @@ -216,4 +226,53 @@ public abstract class BandGridBase : ComponentBase builder.AddAttribute(seq++, "ReadOnly", true); builder.CloseComponent(); } + + protected void SetEditContext(EditContext context) + { + if (editContext == context) return; + if (editContext != null) + editContext.OnFieldChanged -= OnEditFieldChanged; + editContext = context; + validationMessageStore = new ValidationMessageStore(editContext); + editContext.OnFieldChanged += OnEditFieldChanged; + } + + protected virtual void OnEditFieldChanged(object? sender, FieldChangedEventArgs e) + { + validationMessageStore?.Clear(); + editContext?.NotifyValidationStateChanged(); + } + + protected void SetPopupHeaderText(bool isNew) => popupHeaderText = isNew ? "Neu" : "Edit"; + + protected async Task EditFocusedRow() + => await gridRef!.StartEditRowAsync(_focusedVisibleIndex); + + protected Task DeleteFocusedRow() + { + gridRef!.ShowRowDeleteConfirmation(_focusedVisibleIndex); + return Task.CompletedTask; + } + + protected async Task SaveLayoutWithFeedbackAsync() + { + try + { + await SaveLayoutAsync(); + infoMessage = "Layout gespeichert."; + errorMessage = null; + } + catch (Exception ex) + { + errorMessage = $"Layout konnte nicht gespeichert werden: {ex.Message}"; + } + } + + protected async Task ResetLayoutWithFeedbackAsync() + { + await ResetLayoutAsync(); + infoMessage = "Layout zurückgesetzt."; + errorMessage = null; + } + } \ No newline at end of file diff --git a/DbFirst.BlazorWebApp/Components/CatalogsGrid.razor b/DbFirst.BlazorWebApp/Components/CatalogsGrid.razor index ca33b29..e3d5372 100644 --- a/DbFirst.BlazorWebApp/Components/CatalogsGrid.razor +++ b/DbFirst.BlazorWebApp/Components/CatalogsGrid.razor @@ -149,14 +149,7 @@ else @code { private List items = new(); - private bool isLoading; - private bool hasLoaded; - private string? errorMessage; - private string? infoMessage; - private EditContext? editContext; - private ValidationMessageStore? validationMessageStore; private int? focusedRowKey; - private string popupHeaderText = "Edit"; protected override string LayoutKey => "CatalogsGrid"; protected override bool ShowCommandColumn => false; @@ -209,17 +202,7 @@ else } } - private void SetEditContext(EditContext context) - { - if (editContext == context) return; - if (editContext != null) - editContext.OnFieldChanged -= OnEditFieldChanged; - editContext = context; - validationMessageStore = new ValidationMessageStore(editContext); - editContext.OnFieldChanged += OnEditFieldChanged; - } - - private void OnEditFieldChanged(object? sender, FieldChangedEventArgs e) + protected override void OnEditFieldChanged(object? sender, FieldChangedEventArgs e) { if (validationMessageStore == null || editContext == null) return; @@ -237,8 +220,6 @@ else } } - private void SetPopupHeaderText(bool isNew) => popupHeaderText = isNew ? "Neu" : "Edit"; - private void OnCustomizeEditModel(GridCustomizeEditModelEventArgs e) { popupHeaderText = e.IsNew ? "Neu" : "Edit"; @@ -378,36 +359,4 @@ else public int Value { get; set; } public string Text { get; set; } = string.Empty; } - - private int _focusedVisibleIndex; - - private async Task EditFocusedRow() - => await gridRef!.StartEditRowAsync(_focusedVisibleIndex); - - private Task DeleteFocusedRow() - { - gridRef!.ShowRowDeleteConfirmation(_focusedVisibleIndex); - return Task.CompletedTask; - } - - private async Task SaveLayoutWithFeedbackAsync() - { - try - { - await SaveLayoutAsync(); - infoMessage = "Layout gespeichert."; - errorMessage = null; - } - catch (Exception ex) - { - errorMessage = $"Layout konnte nicht gespeichert werden: {ex.Message}"; - } - } - - private async Task ResetLayoutWithFeedbackAsync() - { - await ResetLayoutAsync(); - infoMessage = "Layout zurückgesetzt."; - errorMessage = null; - } } \ No newline at end of file diff --git a/DbFirst.BlazorWebApp/Components/MassDataGrid.razor b/DbFirst.BlazorWebApp/Components/MassDataGrid.razor index 87969e3..0e93895 100644 --- a/DbFirst.BlazorWebApp/Components/MassDataGrid.razor +++ b/DbFirst.BlazorWebApp/Components/MassDataGrid.razor @@ -175,16 +175,9 @@ else @code { private List items = new(); - private bool isLoading; - private bool hasLoaded; - private string? errorMessage; - private string? infoMessage; private int pageIndex; private int pageCount = 1; private int? pageSize = 100; - private string popupHeaderText = "Edit"; - private EditContext? editContext; - private ValidationMessageStore? validationMessageStore; private int? focusedRowKey; protected override string LayoutKey => "MassDataGrid"; @@ -259,17 +252,7 @@ else await LoadPage(0); } - private void SetEditContext(EditContext context) - { - if (editContext == context) return; - if (editContext != null) - editContext.OnFieldChanged -= OnEditFieldChanged; - editContext = context; - validationMessageStore = new ValidationMessageStore(editContext); - editContext.OnFieldChanged += OnEditFieldChanged; - } - - private void OnEditFieldChanged(object? sender, FieldChangedEventArgs e) + protected override void OnEditFieldChanged(object? sender, FieldChangedEventArgs e) { if (validationMessageStore == null || editContext == null) return; if (e.FieldIdentifier.FieldName == nameof(MassDataEditModel.UpdateProcedure)) @@ -285,8 +268,6 @@ else } } - private void SetPopupHeaderText(bool isNew) => popupHeaderText = isNew ? "Neu" : "Edit"; - private void OnCustomizeEditModel(GridCustomizeEditModelEventArgs e) { if (e.IsNew) @@ -391,36 +372,4 @@ else public int? Value { get; set; } public string Text { get; set; } = string.Empty; } - - private int _focusedVisibleIndex; - - private async Task EditFocusedRow() - => await gridRef!.StartEditRowAsync(_focusedVisibleIndex); - - private Task DeleteFocusedRow() - { - gridRef!.ShowRowDeleteConfirmation(_focusedVisibleIndex); - return Task.CompletedTask; - } - - private async Task SaveLayoutWithFeedbackAsync() - { - try - { - await SaveLayoutAsync(); - infoMessage = "Layout gespeichert."; - errorMessage = null; - } - catch (Exception ex) - { - errorMessage = $"Layout konnte nicht gespeichert werden: {ex.Message}"; - } - } - - private async Task ResetLayoutWithFeedbackAsync() - { - await ResetLayoutAsync(); - infoMessage = "Layout zurückgesetzt."; - errorMessage = null; - } } \ No newline at end of file