@using RackPeek.Domain.Persistence @using RackPeek.Domain.Resources @using RackPeek.Domain.Resources.Connections @using RackPeek.Domain.Resources.Servers @using RackPeek.Domain.Resources.SubResources @inject IResourceCollection Repository @inject IAddConnectionUseCase AddConnectionUseCase @if (IsOpen) {
Create Connection
Side A
@if (_resourceA?.Ports?.Any() == true) { } @if (_groupA is not null) { }
Side B
@if (_resourceB?.Ports?.Any() == true) { } @if (_groupB is not null) { }
} @code { [Parameter] public bool IsOpen { get; set; } [Parameter] public EventCallback IsOpenChanged { get; set; } [Parameter] public string? TestIdPrefix { get; set; } private string BaseTestId => string.IsNullOrWhiteSpace(TestIdPrefix) ? "connection-modal" : $"{TestIdPrefix}-connection-modal"; [Parameter] public PortReference? SeedPort { get; set; } List HardwareWithPorts = new(); IPortResource? _resourceA; IPortResource? _resourceB; Port? _groupA; Port? _groupB; readonly PortReference _portA = new(); readonly PortReference _portB = new(); int? _resourceAIndexValue; int? _resourceBIndexValue; int? _groupAIndexValue; int? _groupBIndexValue; int? _portAIndex; int? _portBIndex; int? _resourceAIndex { get => _resourceAIndexValue; set { _resourceAIndexValue = value; if (value is null) { _resourceA = null; _groupA = null; _portAIndex = null; return; } _resourceA = HardwareWithPorts[value.Value]; _portA.Resource = ((Resource)_resourceA).Name; _groupAIndex = null; _portAIndex = null; } } int? _resourceBIndex { get => _resourceBIndexValue; set { _resourceBIndexValue = value; if (value is null) { _resourceB = null; _groupB = null; _portBIndex = null; return; } _resourceB = HardwareWithPorts[value.Value]; _portB.Resource = ((Resource)_resourceB).Name; _groupBIndex = null; _portBIndex = null; } } int? _groupAIndex { get => _groupAIndexValue; set { _groupAIndexValue = value; if (value is null || _resourceA == null) { _groupA = null; _portAIndex = null; return; } _groupA = _resourceA.Ports![value.Value]; _portA.PortGroup = value.Value; _portAIndex = null; } } int? _groupBIndex { get => _groupBIndexValue; set { _groupBIndexValue = value; if (value is null || _resourceB == null) { _groupB = null; _portBIndex = null; return; } _groupB = _resourceB.Ports![value.Value]; _portB.PortGroup = value.Value; _portBIndex = null; } } bool CanSubmit => _groupA != null && _groupB != null && _portAIndex != null && _portBIndex != null; protected override async Task OnParametersSetAsync() { if (!IsOpen) return; var all = await Repository.GetAllOfTypeAsync(); HardwareWithPorts = all .Where(h => h.Ports?.Any() == true) .ToList(); if (SeedPort != null) SeedSinglePortA(SeedPort); } async Task HandleLeftPortClicked(PortReference port) { var existing = await Repository.GetConnectionForPortAsync(port); if (existing != null) SeedConnection(existing); else SeedSinglePortA(port); } async Task HandleRightPortClicked(PortReference port) { var existing = await Repository.GetConnectionForPortAsync(port); if (existing != null) SeedConnection(existing); else SeedSinglePortB(port); } void SeedSinglePortA(PortReference port) { _resourceAIndex = HardwareWithPorts.FindIndex(r => ((Resource)r).Name == port.Resource); _groupAIndex = port.PortGroup; _portAIndex = port.PortIndex; } void SeedSinglePortB(PortReference port) { _resourceBIndex = HardwareWithPorts.FindIndex(r => ((Resource)r).Name == port.Resource); _groupBIndex = port.PortGroup; _portBIndex = port.PortIndex; } void SeedConnection(Connection conn) { SeedSinglePortA(conn.A); SeedSinglePortB(conn.B); } async Task HandleSubmit() { if (!CanSubmit) return; var a = new PortReference { Resource = _portA.Resource, PortGroup = _portA.PortGroup, PortIndex = _portAIndex!.Value }; var b = new PortReference { Resource = _portB.Resource, PortGroup = _portB.PortGroup, PortIndex = _portBIndex!.Value }; await AddConnectionUseCase.ExecuteAsync(a, b); await Cancel(); } async Task Cancel() { await IsOpenChanged.InvokeAsync(false); } }