RobotApp/RobotApp.Client/Pages/Order/ImportOrderDialog.razor
2025-12-22 09:49:02 +07:00

105 lines
2.9 KiB
Plaintext

@using System.Text.Json
@using MudBlazor
<MudDialog>
<TitleContent>
<MudText Typo="Typo.h6">Import Order JSON</MudText>
</TitleContent>
<DialogContent>
@if (ShowWarning)
{
<MudAlert Severity="Severity.Warning"
Variant="Variant.Outlined"
Class="mb-4">
Only valid <b>VDA 5050 Order JSON</b> is accepted.<br />
Invalid structure will be rejected.
</MudAlert>
}
<MudTextField @bind-Value="JsonText"
Label="Paste Order JSON"
Lines="20"
Variant="Variant.Filled"
Immediate
Style="font-family: monospace" />
@if (!string.IsNullOrEmpty(ErrorMessage))
{
<MudAlert Severity="Severity.Error" Class="mt-3">
@ErrorMessage
</MudAlert>
}
</DialogContent>
<DialogActions>
<MudButton OnClick="Cancel">Cancel</MudButton>
<MudButton Variant="Variant.Filled"
Color="Color.Primary"
OnClick="ValidateAndImport">
Import
</MudButton>
</DialogActions>
</MudDialog>
@code {
[CascadingParameter] public IMudDialogInstance MudDialog { get; set; } = default!;
public string JsonText { get; set; } = "";
public string? ErrorMessage;
public bool ShowWarning { get; set; } = false;
private void Cancel() => MudDialog.Cancel();
private void ValidateAndImport()
{
ErrorMessage = null;
ShowWarning = false;
try
{
using var doc = JsonDocument.Parse(JsonText);
var root = doc.RootElement;
// ===== BASIC STRUCTURE CHECK =====
if (!root.TryGetProperty("nodes", out _) ||
!root.TryGetProperty("edges", out _))
throw new Exception("Missing 'nodes' or 'edges' field.");
var order = OrderMessage.FromSchemaObject(root);
ValidateOrder(order);
MudDialog.Close(DialogResult.Ok(order));
}
catch (JsonException)
{
ShowWarning = true;
ErrorMessage = "Invalid JSON format.";
}
catch (Exception ex)
{
ShowWarning = true;
ErrorMessage = ex.Message;
}
}
private void ValidateOrder(OrderMessage order)
{
if (order.Nodes.Count == 0)
throw new Exception("Order must contain at least one node.");
var nodeIds = order.Nodes.Select(n => n.NodeId).ToHashSet();
foreach (var e in order.Edges)
{
if (!nodeIds.Contains(e.StartNodeId) ||
!nodeIds.Contains(e.EndNodeId))
throw new Exception(
$"Edge '{e.EdgeId}' references unknown node."
);
}
}
}