fix(admin-ui): scope configuration modal lifecycles and validation
This commit is contained in:
+172
-116
@@ -8,18 +8,20 @@
|
||||
@using ZB.MOM.WW.OtOpcUa.Core.Abstractions
|
||||
@inject IRawTreeService Svc
|
||||
|
||||
@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 modal-lg" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">@(_isNew ? "New device" : "Edit device") · <span class="mono">@_driverType</span></h5>
|
||||
<button type="button" class="btn-close" aria-label="Close" @onclick="CloseAsync"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
@if (_loadError is not null)
|
||||
@implements IDisposable
|
||||
@{
|
||||
var presentation = _openGeneration;
|
||||
var savedAction = OnSaved;
|
||||
var closeAction = VisibleChanged;
|
||||
}
|
||||
<ModalFrame Visible="@Visible" Title="@(_isNew ? "New device" : "Edit device")" Target="@presentation" Busy="@Pending" DialogClass="modal-lg" OnClose="() => ClosePresentation(presentation, closeAction)">
|
||||
<TitleContent>@(_isNew ? "New device" : "Edit device") · <span class="mono">@_driverType</span></TitleContent>
|
||||
<ChildContent>
|
||||
<fieldset disabled="@(Pending || _loading)" @key="_openGeneration">
|
||||
|
||||
@if (_loading)
|
||||
{ <p role="status">Loading…</p> }
|
||||
else if (_loadError is not null)
|
||||
{
|
||||
<div class="panel notice notice-alert mb-3">@_loadError</div>
|
||||
}
|
||||
@@ -27,14 +29,14 @@
|
||||
{
|
||||
<div class="row g-3 mb-2">
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">Device name</label>
|
||||
<input class="form-control form-control-sm mono" @bind="_name" placeholder="e.g. plc1" />
|
||||
<div class="form-text">A RawPath segment — letters, digits, dash, underscore.</div>
|
||||
<label class="form-label" for='@FieldId("device-name")'>Device name</label>
|
||||
<input id='@FieldId("device-name")' aria-describedby='@FieldIds("name-hint", "save-error")' class="form-control form-control-sm mono" @bind="_name" placeholder="e.g. plc1" />
|
||||
<div id='@FieldId("name-hint")' class="form-text">A RawPath segment — letters, digits, dash, underscore.</div>
|
||||
</div>
|
||||
<div class="col-md-3">
|
||||
<div class="form-check form-switch mt-4">
|
||||
<input type="checkbox" class="form-check-input" id="devEnabled" @bind="_enabled" />
|
||||
<label class="form-check-label" for="devEnabled">Enabled</label>
|
||||
<input type="checkbox" class="form-check-input" id='@FieldId("enabled")' @bind="_enabled" />
|
||||
<label class="form-check-label" for='@FieldId("enabled")'>Enabled</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -44,7 +46,7 @@
|
||||
have no per-device form; the parity test consults that set. *@
|
||||
@if (DeviceFormMap.Resolve(_driverType) is { } deviceFormType)
|
||||
{
|
||||
<DynamicComponent Type="deviceFormType" Parameters="_deviceFormParameters" />
|
||||
<DynamicComponent @key="_openGeneration" Type="deviceFormType" Parameters="_deviceFormParameters" />
|
||||
}
|
||||
else if (DeviceFormMap.IsSingleConnection(_driverType))
|
||||
{
|
||||
@@ -59,31 +61,30 @@
|
||||
}
|
||||
|
||||
<div class="mt-3">
|
||||
<DriverTestConnectButton DriverType="@_driverType" GetConfigJson="@BuildMergedProbeConfig" DriverInstanceId="@_resolvedDriverInstanceId" />
|
||||
<DriverTestConnectButton DriverType="@_driverType" GetConfigJson="@ProbeConfigAccessor" DriverInstanceId="@_resolvedDriverInstanceId" />
|
||||
</div>
|
||||
|
||||
<SaveRefusalNotice GateError="@DriverConfigSaveGate.Validate(_driverType, _deviceConfigJson)" SaveError="@_saveError" />
|
||||
<div id='@FieldId("save-error")'><SaveRefusalNotice GateError="@DriverConfigSaveGate.Validate(_driverType, _deviceConfigJson)" SaveError="@_saveError" /></div>
|
||||
}
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<div class="btn-group" role="group">
|
||||
<button type="button" class="btn btn-outline-secondary" @onclick="CloseAsync" disabled="@_busy">Cancel</button>
|
||||
@if (_loadError is null)
|
||||
{
|
||||
<button type="button" class="btn btn-primary" @onclick="SaveAsync"
|
||||
disabled="@(_busy || DriverConfigSaveGate.Validate(_driverType, _deviceConfigJson) is not null)">
|
||||
@if (_busy) { <span class="spinner-border spinner-border-sm me-1"></span> }
|
||||
@(_isNew ? "Create device" : "Save changes")
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</fieldset>
|
||||
</ChildContent>
|
||||
<Footer>
|
||||
<div class="btn-group" role="group">
|
||||
<button type="button" class="btn btn-outline-secondary" data-initial-focus="true" @onclick="() => ClosePresentation(presentation, closeAction)" disabled="@Pending">Cancel</button>
|
||||
@if (_loadError is null)
|
||||
{
|
||||
<button type="button" class="btn btn-primary" @onclick="() => SavePresentation(presentation, savedAction, closeAction)" disabled="@(Pending || _loading || DriverConfigSaveGate.Validate(_driverType, _deviceConfigJson) is not null)">
|
||||
@if (_busy) { <span class="spinner-border spinner-border-sm me-1" aria-hidden="true"></span> }
|
||||
@(_isNew ? "Create device" : "Save changes")
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</Footer>
|
||||
</ModalFrame>
|
||||
|
||||
@code {
|
||||
|
||||
/// <summary>Whether the modal is shown (supports @bind-Visible).</summary>
|
||||
[Parameter] public bool Visible { get; set; }
|
||||
/// <summary>Two-way visibility callback.</summary>
|
||||
@@ -109,14 +110,20 @@
|
||||
/// <summary>Parameters handed to the DynamicComponent-rendered device form. Cached on the value it
|
||||
/// carries and rebuilt when that changes (never build-once: see DriverConfigModal), with the callback
|
||||
/// created once (R04 H2).</summary>
|
||||
private Dictionary<string, object> _deviceFormParameters => _deviceFormParameterCache.Get(
|
||||
[_deviceConfigJson],
|
||||
() => new()
|
||||
private Dictionary<string, object> _deviceFormParameters {
|
||||
get
|
||||
{
|
||||
["DeviceConfigJson"] = _deviceConfigJson,
|
||||
["DeviceConfigJsonChanged"] = _deviceConfigJsonChanged ??=
|
||||
EventCallback.Factory.Create<string>(this, v => { _deviceConfigJson = v; _saveError = null; }),
|
||||
});
|
||||
var generation = _openGeneration;
|
||||
return _deviceFormParameterCache.Get(
|
||||
[_deviceConfigJson, _openGeneration],
|
||||
() => new()
|
||||
{
|
||||
["DeviceConfigJson"] = _deviceConfigJson,
|
||||
["DeviceConfigJsonChanged"] = _deviceConfigJsonChanged ??=
|
||||
EventCallback.Factory.Create<string>(this, GuardChange<string>(generation, v => { _deviceConfigJson = v; _saveError = null; })),
|
||||
});
|
||||
}
|
||||
}
|
||||
private readonly ParameterDictionaryCache _deviceFormParameterCache = new();
|
||||
private EventCallback<string>? _deviceConfigJsonChanged;
|
||||
private byte[] _rowVersion = [];
|
||||
@@ -127,62 +134,90 @@
|
||||
private string? _saveError;
|
||||
private bool _openHandled;
|
||||
|
||||
// Bumped on every open and close; LoadAsync re-checks it after each await so a superseded open can't
|
||||
// install one target's fields under another target's displayed modal (RawBrowseModal idiom).
|
||||
private int _openGeneration;
|
||||
|
||||
private bool IsStale(int generation) => generation != _openGeneration || !Visible;
|
||||
/// <summary>Optional nonconfidential identity for otherwise identical editor contexts.</summary>
|
||||
[Parameter] public object? Target { get; set; }
|
||||
private readonly string _fieldScope = "ot-config-" + Guid.NewGuid().ToString("N");
|
||||
private string FieldId(string suffix) => _fieldScope + "-" + suffix;
|
||||
private string FieldIds(params string[] suffixes) => string.Join(" ", suffixes.Select(FieldId));
|
||||
private bool _loading, _closing, _disposed, _lastVisible;
|
||||
private object? _lastTarget;
|
||||
private int _openGeneration;
|
||||
private bool Pending => _busy || _closing;
|
||||
private bool IsStale(int generation) => _disposed || generation != _openGeneration || !Visible;
|
||||
private Action<T> GuardChange<T>(int generation, Action<T> update) => value => { if (!IsStale(generation) && !Pending && !_loading) update(value); };
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
if (Visible && !_openHandled)
|
||||
object identity = (Target, DeviceId, DriverInstanceId, DriverType);
|
||||
if (_disposed || (_lastVisible == Visible && Equals(_lastTarget, identity))) return;
|
||||
FreezeProbeConfig();
|
||||
_lastVisible = Visible; _lastTarget = identity;
|
||||
_probeConfig = new ProbeConfigState();
|
||||
var generation = ++_openGeneration;
|
||||
_openHandled = Visible;
|
||||
if (Visible)
|
||||
{
|
||||
_openHandled = true;
|
||||
await LoadAsync(++_openGeneration);
|
||||
}
|
||||
else if (!Visible)
|
||||
{
|
||||
_openHandled = false;
|
||||
_openGeneration++;
|
||||
_deviceConfigJsonChanged = null;
|
||||
_probeConfigAccessor = null;
|
||||
_deviceFormParameterCache.Reset();
|
||||
await LoadAsync(generation);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task LoadAsync(int generation)
|
||||
{
|
||||
_busy = false; _loadError = null; _saveError = null;
|
||||
_isNew = string.IsNullOrEmpty(DeviceId);
|
||||
var deviceId = DeviceId;
|
||||
var driverInstanceId = DriverInstanceId;
|
||||
var driverType = DriverType;
|
||||
_loading = true;
|
||||
_parentDriverConfig = "{}";
|
||||
try
|
||||
{
|
||||
_loadError = null; _saveError = null;
|
||||
_isNew = string.IsNullOrEmpty(deviceId);
|
||||
|
||||
if (_isNew)
|
||||
{
|
||||
_resolvedDriverInstanceId = DriverInstanceId;
|
||||
_driverType = DriverType ?? "";
|
||||
_name = ""; // placeholder only: a pre-filled value made typing append to it (#577); the name is validated on save
|
||||
_enabled = true;
|
||||
_deviceConfigJson = "{}";
|
||||
_rowVersion = [];
|
||||
}
|
||||
else
|
||||
{
|
||||
var dto = await Svc.LoadDeviceForEditAsync(DeviceId!);
|
||||
if (IsStale(generation)) { return; }
|
||||
if (dto is null) { _loadError = $"Device '{DeviceId}' was not found."; return; }
|
||||
_resolvedDriverInstanceId = dto.DriverInstanceId;
|
||||
_driverType = dto.DriverType;
|
||||
_name = dto.Name;
|
||||
_enabled = dto.Enabled;
|
||||
_deviceConfigJson = string.IsNullOrWhiteSpace(dto.DeviceConfig) ? "{}" : dto.DeviceConfig;
|
||||
_rowVersion = dto.RowVersion;
|
||||
}
|
||||
if (_isNew)
|
||||
{
|
||||
_resolvedDriverInstanceId = driverInstanceId;
|
||||
_driverType = driverType ?? "";
|
||||
_name = ""; // placeholder only: a pre-filled value made typing append to it (#577); the name is validated on save
|
||||
_enabled = true;
|
||||
_deviceConfigJson = "{}";
|
||||
_rowVersion = [];
|
||||
}
|
||||
else
|
||||
{
|
||||
var dto = await Svc.LoadDeviceForEditAsync(deviceId!);
|
||||
if (IsStale(generation)) { return; }
|
||||
if (dto is null) { _loadError = $"Device '{deviceId}' was not found."; return; }
|
||||
_resolvedDriverInstanceId = dto.DriverInstanceId;
|
||||
_driverType = dto.DriverType;
|
||||
_name = dto.Name;
|
||||
_enabled = dto.Enabled;
|
||||
_deviceConfigJson = string.IsNullOrWhiteSpace(dto.DeviceConfig) ? "{}" : dto.DeviceConfig;
|
||||
_rowVersion = dto.RowVersion;
|
||||
}
|
||||
|
||||
// The parent driver's DriverConfig — folded with the (possibly unsaved) DeviceConfig for Test-connect.
|
||||
if (!string.IsNullOrEmpty(_resolvedDriverInstanceId))
|
||||
// The parent driver's DriverConfig — folded with the (possibly unsaved) DeviceConfig for Test-connect.
|
||||
if (!string.IsNullOrEmpty(_resolvedDriverInstanceId))
|
||||
{
|
||||
var drv = await Svc.LoadDriverForEditAsync(_resolvedDriverInstanceId);
|
||||
if (IsStale(generation)) { return; }
|
||||
_parentDriverConfig = drv?.DriverConfig ?? "{}";
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception) when (IsStale(generation)) { }
|
||||
finally
|
||||
{
|
||||
var drv = await Svc.LoadDriverForEditAsync(_resolvedDriverInstanceId);
|
||||
if (IsStale(generation)) { return; }
|
||||
_parentDriverConfig = drv?.DriverConfig ?? "{}";
|
||||
if (!IsStale(generation))
|
||||
{
|
||||
_loading = false;
|
||||
if (_loadError is null) _probeConfig.Value = BuildMergedProbeConfig();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Builds the merged Driver+Device probe config from the CURRENT (possibly unsaved) form —
|
||||
/// same merge as <c>LoadMergedProbeConfigAsync</c>, so Test-connect works before the device is saved.</summary>
|
||||
private string BuildMergedProbeConfig()
|
||||
@@ -191,50 +226,71 @@
|
||||
new[] { new DriverDeviceConfigMerger.DeviceRow(_name, _deviceConfigJson) },
|
||||
Array.Empty<RawTagEntry>());
|
||||
|
||||
private async Task SaveAsync()
|
||||
|
||||
private Func<string>? _probeConfigAccessor;
|
||||
private sealed class ProbeConfigState { public string Value = "{}"; }
|
||||
private ProbeConfigState _probeConfig = new();
|
||||
private void FreezeProbeConfig()
|
||||
{
|
||||
// A resubmit REPLACES the previous refusal; a gate refusal is already rendered live by
|
||||
// SaveRefusalNotice, so it is not copied into _saveError (that stacked two boxes — lmxopcua#847).
|
||||
if (!_loading && _loadError is null) _probeConfig.Value = BuildMergedProbeConfig();
|
||||
}
|
||||
private Func<string> ProbeConfigAccessor
|
||||
{
|
||||
get
|
||||
{
|
||||
var generation = _openGeneration;
|
||||
var state = _probeConfig;
|
||||
return _probeConfigAccessor ??= () =>
|
||||
{
|
||||
if (!IsStale(generation) && !_loading && _loadError is null) state.Value = BuildMergedProbeConfig();
|
||||
return state.Value;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private Task SaveAsync() => SavePresentation(_openGeneration, OnSaved, VisibleChanged);
|
||||
private async Task SavePresentation(int generation, EventCallback savedAction, EventCallback<bool> closeAction)
|
||||
{
|
||||
if (IsStale(generation) || Pending || _loading || _loadError is not null) return;
|
||||
_saveError = null;
|
||||
if (DriverConfigSaveGate.Validate(_driverType, _deviceConfigJson) is not null) return;
|
||||
var isNew = _isNew;
|
||||
var name = _name;
|
||||
var json = _deviceConfigJson;
|
||||
var driverType = _driverType;
|
||||
var rowVersion = _rowVersion.ToArray();
|
||||
var parentId = _resolvedDriverInstanceId;
|
||||
var deviceId = DeviceId;
|
||||
var enabled = _enabled;
|
||||
_busy = true;
|
||||
try
|
||||
{
|
||||
UnsMutationResult result;
|
||||
if (_isNew)
|
||||
if (isNew)
|
||||
{
|
||||
if (string.IsNullOrEmpty(_resolvedDriverInstanceId))
|
||||
{
|
||||
_saveError = "No owning driver instance was supplied.";
|
||||
return;
|
||||
}
|
||||
result = await Svc.CreateDeviceAsync(_resolvedDriverInstanceId, _name, _deviceConfigJson);
|
||||
if (string.IsNullOrEmpty(parentId)) { _saveError = "No owning driver instance was supplied."; return; }
|
||||
result = await Svc.CreateDeviceAsync(parentId, name, json);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = await Svc.UpdateDeviceAsync(DeviceId!, _name, _deviceConfigJson, _enabled, _rowVersion);
|
||||
}
|
||||
|
||||
if (!result.Ok)
|
||||
{
|
||||
_saveError = result.Error ?? "Save failed.";
|
||||
return;
|
||||
}
|
||||
|
||||
await OnSaved.InvokeAsync();
|
||||
await SetVisibleAsync(false);
|
||||
else result = await Svc.UpdateDeviceAsync(deviceId!, name, json, enabled, rowVersion);
|
||||
if (IsStale(generation)) return;
|
||||
if (!result.Ok) { _saveError = result.Error ?? "Save failed."; return; }
|
||||
await savedAction.InvokeAsync();
|
||||
if (!IsStale(generation)) await ClosePresentation(generation, closeAction, true);
|
||||
}
|
||||
catch (Exception ex) { _saveError = ex.Message; }
|
||||
catch (Exception ex) { if (!IsStale(generation)) _saveError = ex.Message; }
|
||||
finally { _busy = false; }
|
||||
}
|
||||
|
||||
private Task CloseAsync() => SetVisibleAsync(false);
|
||||
|
||||
private async Task SetVisibleAsync(bool value)
|
||||
private Task CloseAsync() => ClosePresentation(_openGeneration, VisibleChanged);
|
||||
private async Task ClosePresentation(int generation, EventCallback<bool> action, bool afterSave = false)
|
||||
{
|
||||
Visible = value;
|
||||
_openHandled = value;
|
||||
_openGeneration++;
|
||||
await VisibleChanged.InvokeAsync(value);
|
||||
if (IsStale(generation) || _closing || (_busy && !afterSave)) return;
|
||||
_closing = true;
|
||||
FreezeProbeConfig();
|
||||
Visible = false; _lastVisible = false; _openHandled = false; ++_openGeneration;
|
||||
try { await action.InvokeAsync(false); }
|
||||
finally { _closing = false; }
|
||||
}
|
||||
/// <summary>Invalidates UI callbacks and read results without cancelling dispatched mutations.</summary>
|
||||
public void Dispose() { FreezeProbeConfig(); _disposed = true; ++_openGeneration; }
|
||||
}
|
||||
|
||||
+130
-104
@@ -6,18 +6,20 @@
|
||||
@using ZB.MOM.WW.OtOpcUa.Core.Abstractions
|
||||
@inject IRawTreeService Svc
|
||||
|
||||
@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 modal-xl" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">@(_isNew ? "New driver" : "Configure driver") · <span class="mono">@_driverType</span></h5>
|
||||
<button type="button" class="btn-close" aria-label="Close" @onclick="CloseAsync"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
@if (_loadError is not null)
|
||||
@implements IDisposable
|
||||
@{
|
||||
var presentation = _openGeneration;
|
||||
var savedAction = OnSaved;
|
||||
var closeAction = VisibleChanged;
|
||||
}
|
||||
<ModalFrame Visible="@Visible" Title="@(_isNew ? "New driver" : "Configure driver")" Target="@presentation" Busy="@Pending" DialogClass="modal-xl" OnClose="() => ClosePresentation(presentation, closeAction)">
|
||||
<TitleContent>@(_isNew ? "New driver" : "Configure driver") · <span class="mono">@_driverType</span></TitleContent>
|
||||
<ChildContent>
|
||||
<fieldset disabled="@(Pending || _loading)" @key="_openGeneration">
|
||||
|
||||
@if (_loading)
|
||||
{ <p role="status">Loading…</p> }
|
||||
else if (_loadError is not null)
|
||||
{
|
||||
<div class="panel notice notice-alert mb-3">@_loadError</div>
|
||||
}
|
||||
@@ -26,7 +28,7 @@
|
||||
<div class="row g-3 mb-2">
|
||||
<div class="col-md-6">
|
||||
<TechField Label="Driver name" For="@_nameId" Hint="A RawPath segment — letters, digits, dash, underscore." HintId="@(_nameId + "-hint")">
|
||||
<input id="@_nameId" class="form-control form-control-sm mono" @bind="_name" placeholder="line3-modbus" aria-describedby="@(_nameId + "-hint")" />
|
||||
<input id="@_nameId" class="form-control form-control-sm mono" @bind="_name" placeholder="line3-modbus" aria-describedby="@(_nameId + "-hint " + FieldId("save-error"))" />
|
||||
</TechField>
|
||||
</div>
|
||||
</div>
|
||||
@@ -37,7 +39,7 @@
|
||||
green (deferment.md G-1/G-4). The map is guarded by DriverFormMapParityTests. *@
|
||||
@if (DriverConfigFormMap.Resolve(_driverType) is { } formType)
|
||||
{
|
||||
<DynamicComponent Type="formType" Parameters="_formParameters" />
|
||||
<DynamicComponent @key="_openGeneration" Type="formType" Parameters="_formParameters" />
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -63,28 +65,27 @@
|
||||
</p>
|
||||
}
|
||||
|
||||
<SaveRefusalNotice GateError="@DriverConfigSaveGate.Validate(_driverType, _driverConfigJson)" SaveError="@_saveError" />
|
||||
<div id='@FieldId("save-error")'><SaveRefusalNotice GateError="@DriverConfigSaveGate.Validate(_driverType, _driverConfigJson)" SaveError="@_saveError" /></div>
|
||||
}
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<div class="btn-group" role="group">
|
||||
<button type="button" class="btn btn-outline-secondary" @onclick="CloseAsync" disabled="@_busy">Cancel</button>
|
||||
@if (_loadError is null)
|
||||
{
|
||||
<button type="button" class="btn btn-primary" @onclick="SaveAsync"
|
||||
disabled="@(_busy || DriverConfigSaveGate.Validate(_driverType, _driverConfigJson) is not null)">
|
||||
@if (_busy) { <span class="spinner-border spinner-border-sm me-1"></span> }
|
||||
@(_isNew ? "Create driver" : "Save changes")
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</fieldset>
|
||||
</ChildContent>
|
||||
<Footer>
|
||||
<div class="btn-group" role="group">
|
||||
<button type="button" class="btn btn-outline-secondary" data-initial-focus="true" @onclick="() => ClosePresentation(presentation, closeAction)" disabled="@Pending">Cancel</button>
|
||||
@if (_loadError is null)
|
||||
{
|
||||
<button type="button" class="btn btn-primary" @onclick="() => SavePresentation(presentation, savedAction, closeAction)" disabled="@(Pending || _loading || DriverConfigSaveGate.Validate(_driverType, _driverConfigJson) is not null)">
|
||||
@if (_busy) { <span class="spinner-border spinner-border-sm me-1" aria-hidden="true"></span> }
|
||||
@(_isNew ? "Create driver" : "Save changes")
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</Footer>
|
||||
</ModalFrame>
|
||||
|
||||
@code {
|
||||
|
||||
/// <summary>Whether the modal is shown (supports @bind-Visible).</summary>
|
||||
[Parameter] public bool Visible { get; set; }
|
||||
/// <summary>Two-way visibility callback.</summary>
|
||||
@@ -114,17 +115,23 @@
|
||||
/// CURRENT json — a build-once dictionary would pin the value captured at first render and silently
|
||||
/// strand every subsequent edit — so the cache is keyed on the values it carries and rebuilt only when
|
||||
/// one changes; the callbacks are created once and reused (R04 H2).</summary>
|
||||
private Dictionary<string, object> _formParameters => _formParameterCache.Get(
|
||||
[_driverConfigJson, _resilienceConfig],
|
||||
() => new()
|
||||
private Dictionary<string, object> _formParameters {
|
||||
get
|
||||
{
|
||||
["DriverConfigJson"] = _driverConfigJson,
|
||||
["DriverConfigJsonChanged"] = _driverConfigJsonChanged ??=
|
||||
EventCallback.Factory.Create<string>(this, v => { _driverConfigJson = v; _saveError = null; }),
|
||||
["ResilienceConfig"] = _resilienceConfig!,
|
||||
["ResilienceConfigChanged"] = _resilienceConfigChanged ??=
|
||||
EventCallback.Factory.Create<string?>(this, v => _resilienceConfig = v),
|
||||
});
|
||||
var generation = _openGeneration;
|
||||
return _formParameterCache.Get(
|
||||
[_driverConfigJson, _resilienceConfig, _openGeneration],
|
||||
() => new()
|
||||
{
|
||||
["DriverConfigJson"] = _driverConfigJson,
|
||||
["DriverConfigJsonChanged"] = _driverConfigJsonChanged ??=
|
||||
EventCallback.Factory.Create<string>(this, GuardChange<string>(generation, v => { _driverConfigJson = v; _saveError = null; })),
|
||||
["ResilienceConfig"] = _resilienceConfig!,
|
||||
["ResilienceConfigChanged"] = _resilienceConfigChanged ??=
|
||||
EventCallback.Factory.Create<string?>(this, GuardChange<string?>(generation, v => _resilienceConfig = v)),
|
||||
});
|
||||
}
|
||||
}
|
||||
private readonly ParameterDictionaryCache _formParameterCache = new();
|
||||
private EventCallback<string>? _driverConfigJsonChanged;
|
||||
private EventCallback<string?>? _resilienceConfigChanged;
|
||||
@@ -136,96 +143,115 @@
|
||||
private string? _saveError;
|
||||
private bool _openHandled;
|
||||
|
||||
// Bumped on every open and close; LoadAsync re-checks it after each await so a superseded open can't
|
||||
// install one target's fields under another target's displayed modal (RawBrowseModal idiom).
|
||||
private int _openGeneration;
|
||||
|
||||
private bool IsStale(int generation) => generation != _openGeneration || !Visible;
|
||||
/// <summary>Optional nonconfidential identity for otherwise identical editor contexts.</summary>
|
||||
[Parameter] public object? Target { get; set; }
|
||||
private readonly string _fieldScope = "ot-config-" + Guid.NewGuid().ToString("N");
|
||||
private string FieldId(string suffix) => _fieldScope + "-" + suffix;
|
||||
private string FieldIds(params string[] suffixes) => string.Join(" ", suffixes.Select(FieldId));
|
||||
private bool _loading, _closing, _disposed, _lastVisible;
|
||||
private object? _lastTarget;
|
||||
private int _openGeneration;
|
||||
private bool Pending => _busy || _closing;
|
||||
private bool IsStale(int generation) => _disposed || generation != _openGeneration || !Visible;
|
||||
private Action<T> GuardChange<T>(int generation, Action<T> update) => value => { if (!IsStale(generation) && !Pending && !_loading) update(value); };
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
if (Visible && !_openHandled)
|
||||
object identity = (Target, DriverInstanceId, ClusterId, FolderId, DriverType);
|
||||
if (_disposed || (_lastVisible == Visible && Equals(_lastTarget, identity))) return;
|
||||
_lastVisible = Visible; _lastTarget = identity;
|
||||
var generation = ++_openGeneration;
|
||||
_openHandled = Visible;
|
||||
if (Visible)
|
||||
{
|
||||
_openHandled = true;
|
||||
await LoadAsync(++_openGeneration);
|
||||
}
|
||||
else if (!Visible)
|
||||
{
|
||||
_openHandled = false;
|
||||
_openGeneration++;
|
||||
_driverConfigJsonChanged = null;
|
||||
_resilienceConfigChanged = null;
|
||||
_formParameterCache.Reset();
|
||||
await LoadAsync(generation);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task LoadAsync(int generation)
|
||||
{
|
||||
_busy = false; _loadError = null; _saveError = null;
|
||||
_isNew = string.IsNullOrEmpty(DriverInstanceId);
|
||||
var driverInstanceId = DriverInstanceId;
|
||||
var driverType = DriverType;
|
||||
_loading = true;
|
||||
|
||||
if (_isNew)
|
||||
try
|
||||
{
|
||||
_driverType = DriverType ?? "";
|
||||
_name = "";
|
||||
_driverConfigJson = "{}";
|
||||
_resilienceConfig = null;
|
||||
_rowVersion = [];
|
||||
}
|
||||
else
|
||||
{
|
||||
var dto = await Svc.LoadDriverForEditAsync(DriverInstanceId!);
|
||||
if (IsStale(generation)) { return; }
|
||||
if (dto is null) { _loadError = $"Driver '{DriverInstanceId}' was not found."; return; }
|
||||
_driverType = dto.DriverType;
|
||||
_name = dto.Name;
|
||||
_driverConfigJson = string.IsNullOrWhiteSpace(dto.DriverConfig) ? "{}" : dto.DriverConfig;
|
||||
_resilienceConfig = dto.ResilienceConfig;
|
||||
_rowVersion = dto.RowVersion;
|
||||
_loadError = null; _saveError = null;
|
||||
_isNew = string.IsNullOrEmpty(driverInstanceId);
|
||||
|
||||
if (_isNew)
|
||||
{
|
||||
_driverType = driverType ?? "";
|
||||
_name = "";
|
||||
_driverConfigJson = "{}";
|
||||
_resilienceConfig = null;
|
||||
_rowVersion = [];
|
||||
}
|
||||
else
|
||||
{
|
||||
var dto = await Svc.LoadDriverForEditAsync(driverInstanceId!);
|
||||
if (IsStale(generation)) { return; }
|
||||
if (dto is null) { _loadError = $"Driver '{driverInstanceId}' was not found."; return; }
|
||||
_driverType = dto.DriverType;
|
||||
_name = dto.Name;
|
||||
_driverConfigJson = string.IsNullOrWhiteSpace(dto.DriverConfig) ? "{}" : dto.DriverConfig;
|
||||
_resilienceConfig = dto.ResilienceConfig;
|
||||
_rowVersion = dto.RowVersion;
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception) when (IsStale(generation)) { }
|
||||
finally { if (!IsStale(generation)) _loading = false; }
|
||||
}
|
||||
|
||||
private async Task SaveAsync()
|
||||
|
||||
private Task SaveAsync() => SavePresentation(_openGeneration, OnSaved, VisibleChanged);
|
||||
private async Task SavePresentation(int generation, EventCallback savedAction, EventCallback<bool> closeAction)
|
||||
{
|
||||
// A resubmit REPLACES the previous refusal; a gate refusal is already rendered live by
|
||||
// SaveRefusalNotice, so it is not copied into _saveError (that stacked two boxes — lmxopcua#847).
|
||||
if (IsStale(generation) || Pending || _loading || _loadError is not null) return;
|
||||
_saveError = null;
|
||||
if (DriverConfigSaveGate.Validate(_driverType, _driverConfigJson) is not null) return;
|
||||
var isNew = _isNew;
|
||||
var name = _name;
|
||||
var json = _driverConfigJson;
|
||||
var driverType = _driverType;
|
||||
var rowVersion = _rowVersion.ToArray();
|
||||
var driverInstanceId = DriverInstanceId;
|
||||
var clusterId = ClusterId;
|
||||
var folderId = FolderId;
|
||||
var resilience = _resilienceConfig;
|
||||
_busy = true;
|
||||
try
|
||||
{
|
||||
UnsMutationResult result;
|
||||
if (_isNew)
|
||||
if (isNew)
|
||||
{
|
||||
if (string.IsNullOrEmpty(ClusterId))
|
||||
{
|
||||
_saveError = "No owning cluster was supplied.";
|
||||
return;
|
||||
}
|
||||
result = await Svc.CreateDriverAsync(ClusterId, FolderId, _name, _driverType, _driverConfigJson);
|
||||
if (string.IsNullOrEmpty(clusterId)) { _saveError = "No owning cluster was supplied."; return; }
|
||||
result = await Svc.CreateDriverAsync(clusterId, folderId, name, driverType, json);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = await Svc.UpdateDriverAsync(DriverInstanceId!, _name, _driverConfigJson, _resilienceConfig, _rowVersion);
|
||||
}
|
||||
|
||||
if (!result.Ok)
|
||||
{
|
||||
_saveError = result.Error ?? "Save failed.";
|
||||
return;
|
||||
}
|
||||
|
||||
await OnSaved.InvokeAsync();
|
||||
await SetVisibleAsync(false);
|
||||
else result = await Svc.UpdateDriverAsync(driverInstanceId!, name, json, resilience, rowVersion);
|
||||
if (IsStale(generation)) return;
|
||||
if (!result.Ok) { _saveError = result.Error ?? "Save failed."; return; }
|
||||
await savedAction.InvokeAsync();
|
||||
if (!IsStale(generation)) await ClosePresentation(generation, closeAction, true);
|
||||
}
|
||||
catch (Exception ex) { _saveError = ex.Message; }
|
||||
catch (Exception ex) { if (!IsStale(generation)) _saveError = ex.Message; }
|
||||
finally { _busy = false; }
|
||||
}
|
||||
|
||||
private Task CloseAsync() => SetVisibleAsync(false);
|
||||
|
||||
private async Task SetVisibleAsync(bool value)
|
||||
private Task CloseAsync() => ClosePresentation(_openGeneration, VisibleChanged);
|
||||
private async Task ClosePresentation(int generation, EventCallback<bool> action, bool afterSave = false)
|
||||
{
|
||||
Visible = value;
|
||||
_openHandled = value;
|
||||
_openGeneration++;
|
||||
await VisibleChanged.InvokeAsync(value);
|
||||
if (IsStale(generation) || _closing || (_busy && !afterSave)) return;
|
||||
_closing = true;
|
||||
Visible = false; _lastVisible = false; _openHandled = false; ++_openGeneration;
|
||||
try { await action.InvokeAsync(false); }
|
||||
finally { _closing = false; }
|
||||
}
|
||||
/// <summary>Invalidates UI callbacks and read results without cancelling dispatched mutations.</summary>
|
||||
public void Dispose() { _disposed = true; ++_openGeneration; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user