Got a model to populate a grouping creator

This commit is contained in:
Vincent Allen
2025-09-02 15:00:23 -04:00
parent ff34eb87b9
commit e136fa8b3d
42 changed files with 238 additions and 56 deletions

View File

@@ -83,17 +83,20 @@
FilesUploaded="OnFilesUploaded"
ClearClicked="OnClearFilesClicked"></UploadDropBox>
</MudPaper>
@for (int index = 0; index < Model.ArtifactEntries.Count; ++index)
@if (Model is not null)
{
// Capture the current item in a local variable for the lambda
var currentEntry = Model.ArtifactEntries[index];
@for (int index = 0; index < Model.ArtifactEntries.Count; ++index)
{
// Capture the current item in a local variable for the lambda
var currentEntry = Model.ArtifactEntries[index];
<ArchiveEntryCreatorCard Model="currentEntry"
ModelChanged="(updatedEntry) => HandleEntryUpdate(currentEntry, updatedEntry)"
InputsChanged="OnChanged"
@key="currentEntry"
ArtifactEntryIndex="index"
OnEntryDeletedClicked="() => OnDeleteEntryClicked(index)"/>
<ArchiveEntryCreatorCard Model="currentEntry"
ModelChanged="(updatedEntry) => HandleEntryUpdate(currentEntry, updatedEntry)"
InputsChanged="OnChanged"
@key="currentEntry"
ArtifactEntryIndex="index"
OnEntryDeletedClicked="OnDeleteEntryClicked"/>
}
}
</div>
@@ -163,6 +166,18 @@
public List<ValidationResult> ValidationResults { get; private set; } = [];
protected override async Task OnParametersSetAsync()
{
// Ensure to reload the component if a model has been supplied so that the full
// component will render
if (Model?.Category is not null)
{
await OnCategoryChanged();
}
_isFormDivVisible = true;
StateHasChanged();
}
private async Task PublishClicked(MouseEventArgs args)
{
var validationContext = new ValidationContext(Model);
@@ -199,7 +214,7 @@
}
}
private void HandleEntryUpdate(ArtifactEntryValidationModel originalEntry, ArtifactEntryValidationModel updatedEntry)
private async Task HandleEntryUpdate(ArtifactEntryValidationModel originalEntry, ArtifactEntryValidationModel updatedEntry)
{
// Find the index of the original object in our list
var index = Model.ArtifactEntries.IndexOf(originalEntry);
@@ -211,7 +226,7 @@
}
// Now, run the validation logic
OnChanged();
await OnChanged();
}
// You can now simplify your OnFilesUploaded method slightly
@@ -256,7 +271,7 @@
async Task OnCategoryChanged()
{
if (Model.Category is not null)
if (Model.Category is not null && _identifierTextBox is not null)
{
_identifierTextBox.VerifyFormatCategory = Model.Category;
_isFormDivVisible = true;
@@ -303,6 +318,7 @@
private async void OnDeleteEntryClicked(int index)
{
Model.ArtifactEntries.RemoveAt(index);
_uploadComponent.Files.RemoveAt(index);
StateHasChanged();
}
}

View File

@@ -1,6 +1,6 @@
<MudDialog>
<TitleContent>
<MudText Typo="Typo.h6">Create a Category</MudText>
<MudText Typo="Typo.h6">Edit a Category</MudText>
</TitleContent>
<DialogContent>
@@ -17,16 +17,19 @@
[Parameter]
public required ArtifactGroupingValidationModel Model { get; set; }
[CascadingParameter]
IMudDialogInstance MudDialog { get; set; }
[Parameter]
public bool IsUpdate { get; set; } = false;
private void OnCancel(MouseEventArgs args)
{
throw new NotImplementedException();
MudDialog.Cancel();
}
private void OnSubmit(MouseEventArgs args)
{
throw new NotImplementedException();
MudDialog.Close(DialogResult.Ok(Model));
}
}

View File

@@ -1,9 +1,9 @@
<MudDataGrid
T="ArtifactGroupingRowElement"
MultiSelection=true
Items="ArtifactGroupingRows"
Filterable=false
SelectOnRowClick=true
ServerData="new Func<GridState<ArtifactGroupingRowElement>, Task<GridData<ArtifactGroupingRowElement>>>(ServerReload)"
@ref=@DataGrid>
<ToolBarContent>
@@ -94,9 +94,28 @@
}
}
private void OnRowEditClick(ArtifactGroupingRowElement row)
private async Task OnRowEditClick(ArtifactGroupingRowElement row)
{
throw new NotImplementedException();
var parameters = new DialogParameters();
var model = await GroupingProvider.GetGroupingAsync(row.Id);
parameters.Add("Model", ArtifactGroupingValidationModel.ToValidationModel(model));
var options = new DialogOptions()
{
MaxWidth = MaxWidth.ExtraExtraLarge,
FullWidth = true
};
var dialog = await DialogService.ShowAsync<AddGroupingDialog>("Edit Grouping", parameters, options);
var result = await dialog.Result;
if (!result.Canceled)
{
await DataGrid.ReloadServerData();
}
}
private async Task OnDeleteClicked(MouseEventArgs args)
@@ -120,6 +139,27 @@
StateHasChanged();
}
}
}
private async Task<GridData<ArtifactGroupingRowElement>> ServerReload(GridState<ArtifactGroupingRowElement> state)
{
int totalItems = await GroupingProvider.GetTotalCount();
IEnumerable<ArtifactGrouping> groupings = await GroupingProvider.GetGroupingsPaged(state.Page + 1, state.PageSize);
var pagedItems = groupings.Select(grouping => new ArtifactGroupingRowElement()
{
Id = grouping.Id,
Title = grouping.Title,
ArtifactGroupingIdentifier = grouping.ArtifactGroupingIdentifier ?? throw new ArgumentNullException(nameof(grouping), "Got a null ArtifactGroupingIdentifier"),
CategoryName = grouping.Category.Name,
IsPublicallyVisible = grouping.IsPublicallyVisible,
});
return new GridData<ArtifactGroupingRowElement>()
{
TotalItems = totalItems,
Items = pagedItems
};
}
}

View File

@@ -40,6 +40,7 @@
public required string FieldSeparator { get; set; } = "-";
private List<IdentifierFieldValidationModel> _identifierFields = new();
[Parameter]
public required List<IdentifierFieldValidationModel> IdentifierFields
{

View File

@@ -1,6 +1,7 @@
using Microsoft.IdentityModel.Tokens;
using OpenArchival.DataAccess;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace OpenArchival.Blazor;
@@ -54,6 +55,62 @@ public class ArtifactGroupingValidationModel : IValidatableObject
return grouping;
}
public static ArtifactGroupingValidationModel ToValidationModel(ArtifactGrouping grouping)
{
var entries = new List<ArtifactEntryValidationModel>();
foreach (var entry in grouping.ChildArtifactEntries)
{
var defects = new List<string>();
if (entry.Defects is not null)
{
defects.AddRange(entry.Defects.Select(defect => defect.Description));
}
var validationModel = new ArtifactEntryValidationModel()
{
Title = entry.Title,
StorageLocation = entry.StorageLocation.Location,
ArtifactNumber = entry.ArtifactNumber,
AssociatedDates = entry.AssociatedDates,
Defects = entry?.Defects?.Select(defect => defect.Description).ToList(),
Description = entry?.Description,
Files = entry?.Files,
FileTextContent = entry?.FileTextContent,
IsPublicallyVisible = entry.IsPubliclyVisible,
Links = entry.Links,
ListedNames = entry?.ListedNames?.Select(name => name.Value).ToList(),
RelatedArtifacts = entry.RelatedTo,
Tags = entry?.Tags?.Select(tag => tag.Name).ToList(),
Type = entry?.Type.Name,
};
entries.Add(validationModel);
}
var identifierFieldsStrings = grouping.IdentifierFields.Values;
List<IdentifierFieldValidationModel> identifierFields = new();
for (int index = 0; index < identifierFieldsStrings.Count; ++index)
{
identifierFields.Add(new IdentifierFieldValidationModel()
{
Value = identifierFieldsStrings[index],
Name = grouping.Category.FieldNames[index]
});
}
return new ArtifactGroupingValidationModel()
{
Title = grouping.Title,
ArtifactEntries = entries,
Category = grouping.Category,
Description = grouping.Description,
IdentifierFieldValues = identifierFields,
IsPublicallyVisible = grouping.IsPublicallyVisible,
Type = grouping.Type,
};
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
foreach (var entry in ArtifactEntries)