fix(admin-ui): harden simple modal field and pending lifecycles

This commit is contained in:
Joseph Doherty
2026-11-13 22:07:54 -05:00
parent b990ebe3ea
commit c086f97757
44 changed files with 3424 additions and 276 deletions
@@ -1,53 +1,35 @@
@* Driver-type picker for the /raw "New driver" action: pick a driver type + name, then the caller
creates a minimal driver (CreateDriverAsync with "{}") and the operator configures it afterwards via
the Configure-driver modal. Name is inline-validated as a RawPath segment. Markup mirrors the other
/raw modal shells. *@
@using ZB.MOM.WW.OtOpcUa.Commons.Types
@using ZB.MOM.WW.OtOpcUa.Core.Abstractions
@if (Visible)
{
<div class="modal-backdrop fade show" style="display:block"></div>
<div class="modal fade show" tabindex="-1" role="dialog" style="display:block">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">New driver</h5>
<button type="button" class="btn-close" aria-label="Close" @onclick="Cancel"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label class="form-label" for="raw-drv-type">Driver type</label>
<select id="raw-drv-type" class="form-select form-select-sm" @bind="_type">
@foreach (var t in Types)
{
<option value="@t.Value">@t.Label</option>
}
</select>
</div>
<div class="mb-1">
<label class="form-label" for="raw-drv-name">Name</label>
<input id="raw-drv-name" class="form-control form-control-sm mono"
@bind="_name" @bind:event="oninput" placeholder="line3-modbus" />
<div class="form-text">A RawPath segment — no '/', no leading or trailing spaces.</div>
</div>
@if (_error is not null)
{
<div class="text-danger small mt-1">@_error</div>
}
</div>
<div class="modal-footer">
<div class="btn-group" role="group">
<button type="button" class="btn btn-outline-secondary" @onclick="Cancel">Cancel</button>
<button type="button" class="btn btn-primary" @onclick="Submit">Create</button>
</div>
</div>
</div>
</div>
</div>
@implements IDisposable
@{
var presentation = _generation;
var submitAction = OnSubmit;
var closeAction = VisibleChanged;
}
<ModalFrame Visible="@Visible" Title="New driver" Target="@presentation" Busy="@_busy" OnClose="() => Cancel(presentation, closeAction)">
<ChildContent>
<fieldset disabled="@_busy">
<div class="mb-3"><label class="form-label" for='@FieldId("raw-drv-type")'>Driver type</label>
<select id='@FieldId("raw-drv-type")' class="form-select form-select-sm" @bind="_type">
@foreach (var t in Types) { <option value="@t.Value">@t.Label</option> }
</select></div>
<label class="form-label" for='@FieldId("raw-drv-name")'>Name</label>
<input id='@FieldId("raw-drv-name")' class="form-control form-control-sm mono" @bind="_name" @bind:event="oninput"
aria-describedby='@FieldIds("name-hint", "name-error")' aria-invalid="@(_error is null ? null : "true")" placeholder="line3-modbus" />
<div id='@FieldId("name-hint")' class="form-text">A RawPath segment — no '/', no leading or trailing spaces.</div>
<div id='@FieldId("name-error")' class="text-danger small mt-1" role="alert">@_error</div>
</fieldset>
</ChildContent>
<Footer>
<div class="btn-group" role="group">
<button type="button" class="btn btn-outline-secondary" data-initial-focus="true" disabled="@_busy" @onclick="() => Cancel(presentation, closeAction)">Cancel</button>
<button type="button" class="btn btn-primary" disabled="@_busy" @onclick="() => Submit(presentation, submitAction, closeAction)">Create</button>
</div>
</Footer>
</ModalFrame>
@code {
/// <summary>Whether the dialog is shown (supports <c>@bind-Visible</c>).</summary>
[Parameter] public bool Visible { get; set; }
@@ -57,6 +39,9 @@
/// <summary>Raised with the chosen driver type + name when the operator confirms; the dialog self-closes after.</summary>
[Parameter] public EventCallback<(string DriverType, string Name)> OnSubmit { get; set; }
/// <summary>Optional nonconfidential identity when successive targets share the same displayed values.</summary>
[Parameter] public object? Target { get; set; }
// Label → DriverType value. Galaxy's factory registers as "GalaxyMxGateway". Guarded against
// DriverTypeNames by RawDriverTypeDialogParityTests, which reads this field by reflection.
private static readonly (string Label, string Value)[] Types =
@@ -76,35 +61,53 @@
("Heidenhain", DriverTypeNames.Heidenhain),
];
private readonly string _fieldScope = "ot-simple-" + Guid.NewGuid().ToString("N");
private string FieldId(string suffix) => _fieldScope + "-" + suffix;
private string FieldIds(params string[] suffixes) => string.Join(" ", suffixes.Select(FieldId));
private string _type = DriverTypeNames.Modbus;
private string _name = "";
private string? _error;
private bool _open; // seed once per open; preserve edits across unrelated re-renders
private bool _busy, _disposed, _lastVisible;
private object? _lastTarget;
private long _generation;
protected override void OnParametersSet()
{
if (!Visible) { _open = false; return; }
if (_open) { return; }
_open = true;
object? identity = Target;
if (_disposed || (_lastVisible == Visible && Equals(_lastTarget, identity))) return;
_lastVisible = Visible;
_lastTarget = identity;
++_generation;
if (!Visible) return;
_type = DriverTypeNames.Modbus;
_name = "";
_error = null;
}
private async Task Submit()
private async Task Submit(long generation, EventCallback<(string DriverType, string Name)> action, EventCallback<bool> closeAction)
{
if (!Current(generation) || _busy) return;
var err = RawPaths.ValidateSegment(_name);
if (err is not null) { _error = err; return; }
await OnSubmit.InvokeAsync((_type, _name));
await Close();
var submitted = (_type, _name);
_busy = true;
try { await action.InvokeAsync(submitted); }
finally { _busy = false; }
if (Current(generation)) await Close(generation, closeAction);
}
private Task Cancel() => Close();
private async Task Close()
private bool Current(long generation) => !_disposed && Visible && generation == _generation;
private Task Cancel(long generation, EventCallback<bool> closeAction) => Current(generation) && !_busy ? Close(generation, closeAction) : Task.CompletedTask;
private async Task Close(long generation, EventCallback<bool> closeAction)
{
if (!Current(generation)) return;
Visible = false;
_open = false;
await VisibleChanged.InvokeAsync(false);
_lastVisible = false;
++_generation;
await closeAction.InvokeAsync(false);
}
/// <summary>Invalidates UI callbacks without cancelling a dispatched caller operation.</summary>
public void Dispose() { _disposed = true; ++_generation; }
}
@@ -1,43 +1,31 @@
@* Reusable name-input dialog for the /raw create + rename operations (New folder / New tag-group /
New group / Rename folder-driver-device-tag-group). Inline-validates the input as a RawPath segment
(RawPaths.ValidateSegment) before raising OnSubmit; a valid submit self-closes the dialog and the
caller runs the create/rename service call + surfaces any server-side error/warnings. Markup mirrors
the other /raw modal shells (RawStubModal). *@
@using ZB.MOM.WW.OtOpcUa.Commons.Types
@if (Visible)
{
<div class="modal-backdrop fade show" style="display:block"></div>
<div class="modal fade show" tabindex="-1" role="dialog" style="display:block">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">@Title</h5>
<button type="button" class="btn-close" aria-label="Close" @onclick="Cancel"></button>
</div>
<div class="modal-body">
<label class="form-label" for="raw-name-input">Name</label>
<input id="raw-name-input" class="form-control form-control-sm mono"
@bind="_value" @bind:event="oninput" @onkeydown="OnKeyDown"
placeholder="segment-name" />
<div class="form-text">A RawPath segment — no '/', no leading or trailing spaces.</div>
@if (_error is not null)
{
<div class="text-danger small mt-1">@_error</div>
}
</div>
<div class="modal-footer">
<div class="btn-group" role="group">
<button type="button" class="btn btn-outline-secondary" @onclick="Cancel">Cancel</button>
<button type="button" class="btn btn-primary" @onclick="Submit">OK</button>
</div>
</div>
</div>
</div>
</div>
@implements IDisposable
@{
var presentation = _generation;
var submitAction = OnSubmit;
var closeAction = VisibleChanged;
}
<ModalFrame Visible="@Visible" Title="@Title" Target="@presentation" Busy="@_busy" OnClose="() => Cancel(presentation, closeAction)">
<ChildContent>
<fieldset disabled="@_busy">
<label class="form-label" for='@FieldId("raw-name-input")'>Name</label>
<input id='@FieldId("raw-name-input")' class="form-control form-control-sm mono" @bind="_value" @bind:event="oninput" @onkeydown="e => OnKeyDown(e, presentation, submitAction, closeAction)"
aria-describedby='@FieldIds("name-hint", "name-error")' aria-invalid="@(_error is null ? null : "true")" placeholder="segment-name" />
<div id='@FieldId("name-hint")' class="form-text">A RawPath segment — no '/', no leading or trailing spaces.</div>
<div id='@FieldId("name-error")' class="text-danger small mt-1" role="alert">@_error</div>
</fieldset>
</ChildContent>
<Footer>
<div class="btn-group" role="group">
<button type="button" class="btn btn-outline-secondary" data-initial-focus="true" disabled="@_busy" @onclick="() => Cancel(presentation, closeAction)">Cancel</button>
<button type="button" class="btn btn-primary" disabled="@_busy" @onclick="() => Submit(presentation, submitAction, closeAction)">OK</button>
</div>
</Footer>
</ModalFrame>
@code {
/// <summary>Whether the dialog is shown (supports <c>@bind-Visible</c>).</summary>
[Parameter] public bool Visible { get; set; }
@@ -53,38 +41,55 @@
/// <summary>Raised with the validated name when the operator confirms. The dialog self-closes after.</summary>
[Parameter] public EventCallback<string> OnSubmit { get; set; }
/// <summary>Optional nonconfidential identity when successive targets share the same displayed values.</summary>
[Parameter] public object? Target { get; set; }
private readonly string _fieldScope = "ot-simple-" + Guid.NewGuid().ToString("N");
private string FieldId(string suffix) => _fieldScope + "-" + suffix;
private string FieldIds(params string[] suffixes) => string.Join(" ", suffixes.Select(FieldId));
private string _value = "";
private string? _error;
private bool _open; // seed the input once per open; preserve typing across unrelated re-renders
private bool _busy, _disposed, _lastVisible;
private object? _lastTarget;
private long _generation;
protected override void OnParametersSet()
{
if (!Visible) { _open = false; return; }
if (_open) { return; }
_open = true;
object? identity = (Target, Title, Value);
if (_disposed || (_lastVisible == Visible && Equals(_lastTarget, identity))) return;
_lastVisible = Visible;
_lastTarget = identity;
++_generation;
if (!Visible) return;
_value = Value ?? "";
_error = null;
}
private async Task Submit()
private async Task Submit(long generation, EventCallback<string> action, EventCallback<bool> closeAction)
{
if (!Current(generation) || _busy) return;
var err = RawPaths.ValidateSegment(_value);
if (err is not null) { _error = err; return; }
await OnSubmit.InvokeAsync(_value);
await Close();
var submitted = _value;
_busy = true;
try { await action.InvokeAsync(submitted); }
finally { _busy = false; }
if (Current(generation)) await Close(generation, closeAction);
}
private async Task OnKeyDown(KeyboardEventArgs e)
{
if (e.Key == "Enter") { await Submit(); }
}
private Task Cancel() => Close();
private async Task Close()
private bool Current(long generation) => !_disposed && Visible && generation == _generation;
private Task Cancel(long generation, EventCallback<bool> closeAction) => Current(generation) && !_busy ? Close(generation, closeAction) : Task.CompletedTask;
private async Task Close(long generation, EventCallback<bool> closeAction)
{
if (!Current(generation)) return;
Visible = false;
_open = false;
await VisibleChanged.InvokeAsync(false);
_lastVisible = false;
++_generation;
await closeAction.InvokeAsync(false);
}
private Task OnKeyDown(KeyboardEventArgs e, long generation, EventCallback<string> action, EventCallback<bool> closeAction) => e.Key == "Enter" ? Submit(generation, action, closeAction) : Task.CompletedTask;
/// <summary>Invalidates UI callbacks without cancelling a dispatched caller operation.</summary>
public void Dispose() { _disposed = true; ++_generation; }
}
@@ -1,37 +1,14 @@
@* Placeholder modal for the /raw context-menu actions whose real dialogs
(Configure / Device / TagModal / CSV import-export / Browse device / Rename)
land in later Batch-2 waves (B/C). This wave wires every menu action to a
named handler that opens this "coming soon" placeholder, so the tree + lazy
expansion + menus can be live-verified without the real modals existing yet.
Wave B/C replaces each handler body with the real modal invocation and can
then delete this component. Markup mirrors the GlobalUns delete-confirm modal. *@
@if (Visible)
{
<div class="modal-backdrop fade show" style="display:block"></div>
<div class="modal fade show" tabindex="-1" role="dialog" style="display:block">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">@Title</h5>
<button type="button" class="btn-close" aria-label="Close" @onclick="OnClose"></button>
</div>
<div class="modal-body">
<p>@Message</p>
@if (!string.IsNullOrWhiteSpace(NodeName))
{
<p class="text-muted small mb-0">Target: <span class="mono">@NodeName</span></p>
}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-secondary" @onclick="OnClose">Close</button>
</div>
</div>
</div>
</div>
}
@implements IDisposable
@{ var presentation = _generation; var closeAction = OnClose; }
<ModalFrame Visible="@Visible" Title="@Title" Description="@Message" Target="@presentation" Busy="@_closing" OnClose="() => Close(presentation, closeAction)">
<ChildContent>
@if (!string.IsNullOrWhiteSpace(NodeName))
{ <p class="text-muted small mb-0">Target: <span class="mono">@NodeName</span></p> }
</ChildContent>
<Footer><button type="button" class="btn btn-outline-secondary" data-initial-focus="true" disabled="@_closing" @onclick="() => Close(presentation, closeAction)">Close</button></Footer>
</ModalFrame>
@code {
/// <summary>Whether the placeholder is shown.</summary>
[Parameter] public bool Visible { get; set; }
@@ -46,4 +23,25 @@
/// <summary>Raised when the placeholder is dismissed.</summary>
[Parameter] public EventCallback OnClose { get; set; }
/// <summary>Optional nonconfidential identity for successive messages with identical captions.</summary>
[Parameter] public object? Target { get; set; }
private bool _lastVisible, _disposed, _closing;
private object? _lastTarget;
private long _generation;
protected override void OnParametersSet()
{
object identity = (Target, Title, Message, NodeName);
if (_lastVisible == Visible && Equals(_lastTarget, identity)) return;
_lastVisible = Visible; _lastTarget = identity; ++_generation;
}
private async Task Close(long generation, EventCallback action)
{
if (_disposed || !Visible || generation != _generation || _closing) return;
_closing = true;
try { await action.InvokeAsync(); }
finally { _closing = false; }
}
/// <summary>Invalidates old presentation callbacks.</summary>
public void Dispose() { _disposed = true; ++_generation; }
}
@@ -1,72 +1,64 @@
@* Create/edit modal for a UNS area, wired straight into IUnsTreeService. The host page owns
visibility and supplies the parent cluster (create) or the loaded AreaEditDto (edit) plus the
served-by cluster list. On a successful save it raises OnSaved so the host can reload the tree. *@
@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.Components.Forms
@using ZB.MOM.WW.OtOpcUa.Configuration.Validation
@using ZB.MOM.WW.OtOpcUa.AdminUI.Uns
@inject IUnsTreeService Svc
@implements IDisposable
@{
var presentation = _generation;
var savedAction = OnSaved;
var cancelAction = OnCancel;
}
<ModalFrame Visible="@Visible" Title="@(IsNew ? "New UNS area" : "Edit UNS area")" Target="@presentation" Busy="@Pending" OnClose="() => CancelAsync(presentation, cancelAction)">
<ChildContent>
<EditForm id='@FieldId("form")' Model="_form" OnValidSubmit="() => SaveAsync(presentation, savedAction)" FormName="areaModal" aria-describedby='@FieldId("service-error")'>
<DataAnnotationsValidator />
<fieldset disabled="@Pending">
@if (Visible)
{
<div class="modal-backdrop fade show" style="display:block"></div>
<div class="modal fade show" tabindex="-1" role="dialog" style="display:block">
<div class="modal-dialog" role="document">
<div class="modal-content">
<EditForm Model="_form" OnValidSubmit="SaveAsync" FormName="areaModal">
<DataAnnotationsValidator />
<div class="modal-header">
<h5 class="modal-title">@(IsNew ? "New UNS area" : "Edit UNS area")</h5>
<button type="button" class="btn-close" aria-label="Close" @onclick="CancelAsync"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label class="form-label" for="area-id">UnsAreaId</label>
<InputText id="area-id" @bind-Value="_form.UnsAreaId" disabled="@(!IsNew)"
<label class="form-label" for='@FieldId("area-id")'>UnsAreaId</label>
<InputText id='@FieldId("area-id")' aria-describedby='@FieldIds("area-id-error")' @bind-Value="_form.UnsAreaId" disabled="@(!IsNew)"
class="form-control form-control-sm mono" />
<ValidationMessage For="@(() => _form.UnsAreaId)" />
<div id='@FieldId("area-id-error")'><ValidationMessage For="@(() => _form.UnsAreaId)" /></div>
</div>
<div class="mb-3">
<label class="form-label" for="area-name">Name</label>
<InputText id="area-name" @bind-Value="_form.Name" class="form-control form-control-sm" />
<ValidationMessage For="@(() => _form.Name)" />
<div class="form-text">UNS path segment: lowercase letters, digits and dashes, 1-32 characters. It becomes a level of the OPC UA NodeId.</div>
<label class="form-label" for='@FieldId("area-name")'>Name</label>
<InputText id='@FieldId("area-name")' aria-describedby='@FieldIds("area-name-error", "area-name-hint")' @bind-Value="_form.Name" class="form-control form-control-sm" />
<div id='@FieldId("area-name-error")'><ValidationMessage For="@(() => _form.Name)" /></div>
<div id='@FieldId("area-name-hint")' class="form-text">UNS path segment: lowercase letters, digits and dashes, 1-32 characters. It becomes a level of the OPC UA NodeId.</div>
</div>
<div class="mb-3">
<label class="form-label" for="area-cluster">Served by cluster</label>
<InputSelect id="area-cluster" @bind-Value="_form.ClusterId" class="form-select form-select-sm">
<label class="form-label" for='@FieldId("area-cluster")'>Served by cluster</label>
<InputSelect id='@FieldId("area-cluster")' aria-describedby='@FieldIds("area-cluster-error")' @bind-Value="_form.ClusterId" class="form-select form-select-sm">
@foreach (var (id, display) in Clusters)
{
<option value="@id">@display</option>
}
</InputSelect>
<ValidationMessage For="@(() => _form.ClusterId)" />
<div id='@FieldId("area-cluster-error")'><ValidationMessage For="@(() => _form.ClusterId)" /></div>
</div>
<div class="mb-3">
<label class="form-label" for="area-notes">Notes</label>
<InputTextArea id="area-notes" @bind-Value="_form.Notes" class="form-control form-control-sm" rows="3" />
<label class="form-label" for='@FieldId("area-notes")'>Notes</label>
<InputTextArea id='@FieldId("area-notes")' @bind-Value="_form.Notes" class="form-control form-control-sm" rows="3" />
</div>
@if (!string.IsNullOrWhiteSpace(_error))
{
<div class="text-danger small mt-2">@_error</div>
}
</div>
<div class="modal-footer">
<div class="btn-group" role="group">
<button type="button" class="btn btn-outline-secondary" @onclick="CancelAsync" disabled="@_busy">Cancel</button>
<button type="submit" class="btn btn-primary" disabled="@_busy">
@if (_busy) { <span class="spinner-border spinner-border-sm me-1"></span> }
@(IsNew ? "Create" : "Save changes")
</button>
</div>
</div>
</EditForm>
</div>
<div id='@FieldId("service-error")' class="text-danger small mt-2" role="alert">@_error</div>
</fieldset>
</EditForm>
</ChildContent>
<Footer>
<div class="btn-group" role="group">
<button type="button" class="btn btn-outline-secondary" data-initial-focus="true" @onclick="() => CancelAsync(presentation, cancelAction)" disabled="@Pending">Cancel</button>
<button type="submit" form='@FieldId("form")' class="btn btn-primary" disabled="@Pending">
@if (_busy) { <span class="spinner-border spinner-border-sm me-1" aria-hidden="true"></span> }
@(IsNew ? "Create" : "Save changes")
</button>
</div>
</div>
}
</Footer>
</ModalFrame>
@code {
/// <summary>Whether the modal is shown. The host owns this flag.</summary>
[Parameter] public bool Visible { get; set; }
@@ -88,13 +80,25 @@
/// <summary>Raised when the user cancels so the host can close.</summary>
[Parameter] public EventCallback OnCancel { get; set; }
/// <summary>Optional nonconfidential target identity for successive presentations.</summary>
[Parameter] public object? Target { get; set; }
private readonly string _fieldScope = "ot-simple-" + Guid.NewGuid().ToString("N");
private string FieldId(string suffix) => _fieldScope + "-" + suffix;
private string FieldIds(params string[] suffixes) => string.Join(" ", suffixes.Select(FieldId));
private FormModel _form = new();
private bool _busy;
private bool _busy, _closing, _disposed, _lastVisible;
private bool Pending => _busy || _closing;
private object? _lastTarget;
private long _generation;
private string? _error;
protected override void OnParametersSet()
{
// Rebuild the working form whenever the host (re)opens the modal for a fresh target.
object identity = (Target, IsNew, ClusterId, Existing);
if (_disposed || (_lastVisible == Visible && Equals(_lastTarget, identity))) return;
_lastVisible = Visible; _lastTarget = identity; ++_generation;
if (!Visible) return;
if (IsNew)
{
_form = new FormModel { ClusterId = ClusterId ?? "" };
@@ -109,35 +113,38 @@
ClusterId = Existing.ClusterId,
};
}
_error = null;
}
private async Task SaveAsync()
private async Task SaveAsync(long generation, EventCallback savedAction)
{
if (!Current(generation) || Pending || (!IsNew && Existing is null)) return;
var submitted = new FormModel { UnsAreaId = _form.UnsAreaId, Name = _form.Name, Notes = _form.Notes, ClusterId = _form.ClusterId };
var isNew = IsNew;
var rowVersion = Existing?.RowVersion.ToArray() ?? Array.Empty<byte>();
_busy = true;
_error = null;
try
{
var result = IsNew
? await Svc.CreateAreaAsync(_form.ClusterId, _form.UnsAreaId, _form.Name, _form.Notes)
: await Svc.UpdateAreaAsync(_form.UnsAreaId, _form.Name, _form.Notes, _form.ClusterId, Existing!.RowVersion);
if (result.Ok)
{
await OnSaved.InvokeAsync();
}
else
{
_error = result.Error;
}
}
finally
{
_busy = false;
var result = isNew ? await Svc.CreateAreaAsync(submitted.ClusterId, submitted.UnsAreaId, submitted.Name, submitted.Notes) : await Svc.UpdateAreaAsync(submitted.UnsAreaId, submitted.Name, submitted.Notes, submitted.ClusterId, rowVersion);
if (!Current(generation)) return;
if (result.Ok) await savedAction.InvokeAsync();
else _error = result.Error;
}
finally { _busy = false; }
}
private Task CancelAsync() => OnCancel.InvokeAsync();
private bool Current(long generation) => !_disposed && Visible && generation == _generation;
private async Task CancelAsync(long generation, EventCallback cancelAction)
{
if (!Current(generation) || Pending) return;
_closing = true;
try { await cancelAction.InvokeAsync(); }
finally { _closing = false; }
}
/// <summary>Invalidates UI callbacks without cancelling a dispatched service operation.</summary>
public void Dispose() { _disposed = true; ++_generation; }
private sealed class FormModel
{
@@ -147,4 +154,5 @@
[Required] public string ClusterId { get; set; } = "";
public string? Notes { get; set; }
}
}
@@ -1,73 +1,64 @@
@* Create/edit modal for a UNS line, wired straight into IUnsTreeService. The host page owns
visibility and supplies the parent area (create) or the loaded LineEditDto (edit). The parent-area
list is SCOPED TO THE LINE'S CLUSTER by the host so an edit cannot move a line across clusters.
On a successful save it raises OnSaved so the host can reload the tree. *@
@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.Components.Forms
@using ZB.MOM.WW.OtOpcUa.Configuration.Validation
@using ZB.MOM.WW.OtOpcUa.AdminUI.Uns
@inject IUnsTreeService Svc
@implements IDisposable
@{
var presentation = _generation;
var savedAction = OnSaved;
var cancelAction = OnCancel;
}
<ModalFrame Visible="@Visible" Title="@(IsNew ? "New UNS line" : "Edit UNS line")" Target="@presentation" Busy="@Pending" OnClose="() => CancelAsync(presentation, cancelAction)">
<ChildContent>
<EditForm id='@FieldId("form")' Model="_form" OnValidSubmit="() => SaveAsync(presentation, savedAction)" FormName="lineModal" aria-describedby='@FieldId("service-error")'>
<DataAnnotationsValidator />
<fieldset disabled="@Pending">
@if (Visible)
{
<div class="modal-backdrop fade show" style="display:block"></div>
<div class="modal fade show" tabindex="-1" role="dialog" style="display:block">
<div class="modal-dialog" role="document">
<div class="modal-content">
<EditForm Model="_form" OnValidSubmit="SaveAsync" FormName="lineModal">
<DataAnnotationsValidator />
<div class="modal-header">
<h5 class="modal-title">@(IsNew ? "New UNS line" : "Edit UNS line")</h5>
<button type="button" class="btn-close" aria-label="Close" @onclick="CancelAsync"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label class="form-label" for="line-id">UnsLineId</label>
<InputText id="line-id" @bind-Value="_form.UnsLineId" disabled="@(!IsNew)"
<label class="form-label" for='@FieldId("line-id")'>UnsLineId</label>
<InputText id='@FieldId("line-id")' aria-describedby='@FieldIds("line-id-error")' @bind-Value="_form.UnsLineId" disabled="@(!IsNew)"
class="form-control form-control-sm mono" />
<ValidationMessage For="@(() => _form.UnsLineId)" />
<div id='@FieldId("line-id-error")'><ValidationMessage For="@(() => _form.UnsLineId)" /></div>
</div>
<div class="mb-3">
<label class="form-label" for="line-area">Parent area</label>
<InputSelect id="line-area" @bind-Value="_form.UnsAreaId" class="form-select form-select-sm">
<label class="form-label" for='@FieldId("line-area")'>Parent area</label>
<InputSelect id='@FieldId("line-area")' aria-describedby='@FieldIds("line-area-error")' @bind-Value="_form.UnsAreaId" class="form-select form-select-sm">
@foreach (var (id, display) in Areas)
{
<option value="@id">@display</option>
}
</InputSelect>
<ValidationMessage For="@(() => _form.UnsAreaId)" />
<div id='@FieldId("line-area-error")'><ValidationMessage For="@(() => _form.UnsAreaId)" /></div>
</div>
<div class="mb-3">
<label class="form-label" for="line-name">Name</label>
<InputText id="line-name" @bind-Value="_form.Name" class="form-control form-control-sm" />
<ValidationMessage For="@(() => _form.Name)" />
<div class="form-text">UNS path segment: lowercase letters, digits and dashes, 1-32 characters. It becomes a level of the OPC UA NodeId.</div>
<label class="form-label" for='@FieldId("line-name")'>Name</label>
<InputText id='@FieldId("line-name")' aria-describedby='@FieldIds("line-name-error", "line-name-hint")' @bind-Value="_form.Name" class="form-control form-control-sm" />
<div id='@FieldId("line-name-error")'><ValidationMessage For="@(() => _form.Name)" /></div>
<div id='@FieldId("line-name-hint")' class="form-text">UNS path segment: lowercase letters, digits and dashes, 1-32 characters. It becomes a level of the OPC UA NodeId.</div>
</div>
<div class="mb-3">
<label class="form-label" for="line-notes">Notes</label>
<InputTextArea id="line-notes" @bind-Value="_form.Notes" class="form-control form-control-sm" rows="3" />
<label class="form-label" for='@FieldId("line-notes")'>Notes</label>
<InputTextArea id='@FieldId("line-notes")' @bind-Value="_form.Notes" class="form-control form-control-sm" rows="3" />
</div>
@if (!string.IsNullOrWhiteSpace(_error))
{
<div class="text-danger small mt-2">@_error</div>
}
</div>
<div class="modal-footer">
<div class="btn-group" role="group">
<button type="button" class="btn btn-outline-secondary" @onclick="CancelAsync" disabled="@_busy">Cancel</button>
<button type="submit" class="btn btn-primary" disabled="@_busy">
@if (_busy) { <span class="spinner-border spinner-border-sm me-1"></span> }
@(IsNew ? "Create" : "Save changes")
</button>
</div>
</div>
</EditForm>
</div>
<div id='@FieldId("service-error")' class="text-danger small mt-2" role="alert">@_error</div>
</fieldset>
</EditForm>
</ChildContent>
<Footer>
<div class="btn-group" role="group">
<button type="button" class="btn btn-outline-secondary" data-initial-focus="true" @onclick="() => CancelAsync(presentation, cancelAction)" disabled="@Pending">Cancel</button>
<button type="submit" form='@FieldId("form")' class="btn btn-primary" disabled="@Pending">
@if (_busy) { <span class="spinner-border spinner-border-sm me-1" aria-hidden="true"></span> }
@(IsNew ? "Create" : "Save changes")
</button>
</div>
</div>
}
</Footer>
</ModalFrame>
@code {
/// <summary>Whether the modal is shown. The host owns this flag.</summary>
[Parameter] public bool Visible { get; set; }
@@ -89,13 +80,25 @@
/// <summary>Raised when the user cancels so the host can close.</summary>
[Parameter] public EventCallback OnCancel { get; set; }
/// <summary>Optional nonconfidential target identity for successive presentations.</summary>
[Parameter] public object? Target { get; set; }
private readonly string _fieldScope = "ot-simple-" + Guid.NewGuid().ToString("N");
private string FieldId(string suffix) => _fieldScope + "-" + suffix;
private string FieldIds(params string[] suffixes) => string.Join(" ", suffixes.Select(FieldId));
private FormModel _form = new();
private bool _busy;
private bool _busy, _closing, _disposed, _lastVisible;
private bool Pending => _busy || _closing;
private object? _lastTarget;
private long _generation;
private string? _error;
protected override void OnParametersSet()
{
// Rebuild the working form whenever the host (re)opens the modal for a fresh target.
object identity = (Target, IsNew, UnsAreaId, Existing);
if (_disposed || (_lastVisible == Visible && Equals(_lastTarget, identity))) return;
_lastVisible = Visible; _lastTarget = identity; ++_generation;
if (!Visible) return;
if (IsNew)
{
_form = new FormModel { UnsAreaId = UnsAreaId ?? "" };
@@ -110,35 +113,38 @@
Notes = Existing.Notes,
};
}
_error = null;
}
private async Task SaveAsync()
private async Task SaveAsync(long generation, EventCallback savedAction)
{
if (!Current(generation) || Pending || (!IsNew && Existing is null)) return;
var submitted = new FormModel { UnsLineId = _form.UnsLineId, UnsAreaId = _form.UnsAreaId, Name = _form.Name, Notes = _form.Notes };
var isNew = IsNew;
var rowVersion = Existing?.RowVersion.ToArray() ?? Array.Empty<byte>();
_busy = true;
_error = null;
try
{
var result = IsNew
? await Svc.CreateLineAsync(_form.UnsAreaId, _form.UnsLineId, _form.Name, _form.Notes)
: await Svc.UpdateLineAsync(_form.UnsLineId, _form.Name, _form.Notes, _form.UnsAreaId, Existing!.RowVersion);
if (result.Ok)
{
await OnSaved.InvokeAsync();
}
else
{
_error = result.Error;
}
}
finally
{
_busy = false;
var result = isNew ? await Svc.CreateLineAsync(submitted.UnsAreaId, submitted.UnsLineId, submitted.Name, submitted.Notes) : await Svc.UpdateLineAsync(submitted.UnsLineId, submitted.Name, submitted.Notes, submitted.UnsAreaId, rowVersion);
if (!Current(generation)) return;
if (result.Ok) await savedAction.InvokeAsync();
else _error = result.Error;
}
finally { _busy = false; }
}
private Task CancelAsync() => OnCancel.InvokeAsync();
private bool Current(long generation) => !_disposed && Visible && generation == _generation;
private async Task CancelAsync(long generation, EventCallback cancelAction)
{
if (!Current(generation) || Pending) return;
_closing = true;
try { await cancelAction.InvokeAsync(); }
finally { _closing = false; }
}
/// <summary>Invalidates UI callbacks without cancelling a dispatched service operation.</summary>
public void Dispose() { _disposed = true; ++_generation; }
private sealed class FormModel
{
@@ -148,4 +154,5 @@
public string Name { get; set; } = "";
public string? Notes { get; set; }
}
}