@page "/materials/new" @page "/materials/{Id:int}" @inject MaterialService MaterialService @inject NavigationManager Navigation @(IsNew ? "Add Material" : "Edit Material")

@(IsNew ? "Add Material" : "Edit Material")

@if (loading) {

Loading...

} else {
@foreach (var shape in MaterialService.CommonShapes) { }
Examples: "1" OD x 0.065 wall", "2x2", "1.5 x 1.5 x 0.125"
@if (!string.IsNullOrEmpty(errorMessage)) {
@errorMessage
}
Cancel
} @code { [Parameter] public int? Id { get; set; } private Material material = new(); private bool loading = true; private bool saving; private string? errorMessage; private bool IsNew => !Id.HasValue; protected override async Task OnInitializedAsync() { if (Id.HasValue) { var existing = await MaterialService.GetByIdAsync(Id.Value); if (existing == null) { Navigation.NavigateTo("materials"); return; } material = existing; } loading = false; } private async Task SaveAsync() { errorMessage = null; saving = true; try { if (string.IsNullOrWhiteSpace(material.Shape)) { errorMessage = "Shape is required"; return; } if (string.IsNullOrWhiteSpace(material.Size)) { errorMessage = "Size is required"; return; } // Check for duplicates if (await MaterialService.ExistsAsync(material.Shape, material.Size, Id)) { errorMessage = "A material with this shape and size already exists"; return; } if (IsNew) { await MaterialService.CreateAsync(material); } else { await MaterialService.UpdateAsync(material); } Navigation.NavigateTo("materials"); } finally { saving = false; } } }