@page "/add"
@using OpenArchival.Blazor.Components.CustomComponents;
@using OpenArchival.Blazor.Components.Pages.Administration.Categories
@using OpenArchival.DataAccess;
Add an Archive Item
@if (!IsValid && _isFormDivVisible)
{
All identifier fields must be filled in.
}
Archive Item Identifier
@foreach (FilePathListing listing in _filePathListings)
{
}
@**@
Cancel
Publish
@using System.ComponentModel.DataAnnotations
@inject IDialogService DialogService
@inject NavigationManager NavigationManager;
@inject IArchiveCategoryProvider CategoryProvider;
@code {
private IdentifierTextBox _identifierTextBox = default!;
private ElementReference _formDiv = default!;
private bool _isFormDivVisible = false;
private string _formDivStyle => _isFormDivVisible ? "" : "display: none;";
public List DatesData { get; set; } = [];
public List Categories { get; set; } = new();
private List _filePathListings = new();
private bool _categorySelected = false;
//public List IdentifierFields { get; set; } = [new IdentifierFieldValidationModel() { Name = "Field One", Value = "" }, new IdentifierFieldValidationModel() { Name = "Field Two", Value = "" }, new IdentifierFieldValidationModel() { Name = "Field Three", Value = "" }];
public ArchiveItemValidationModel Model { get; set; } = new();
public bool IsValid { get; set; } = false;
///
/// The URI to navigate to if cancel is pressed
///
public string? BackLink { get; set; } = "/";
public string? ForwardLink { get; set; } = "/";
private void CancelClicked(MouseEventArgs args)
{
if (BackLink is not null) {
NavigationManager.NavigateTo(BackLink);
}
else
{
throw new ArgumentNullException("No back link provided for the add archive item page.");
}
}
private async Task OnFilesUploaded(List args)
{
_filePathListings = args;
StateHasChanged();
await OnChanged();
}
private async Task OnClearFilesClicked()
{
_filePathListings = [];
StateHasChanged();
await OnChanged();
}
private void PublishClicked(MouseEventArgs args)
{
var validationContext = new ValidationContext(Model);
var validationResult = new List();
IsValid = Validator.TryValidateObject(Model, validationContext, validationResult);
/*
if (ForwardLink is not null)
{
if (IsValid)
{
NavigationManager.NavigateTo(ForwardLink);
}
else
{
StateHasChanged();
}
}
else
{
throw new ArgumentNullException("No forward link provided for the add archive item page.");
}
// TODO: assign parents to all file path listings
throw new NotImplementedException();
*/
}
async Task OnChanged()
{
var validationContext = new ValidationContext(Model);
var validationResult = new List();
IsValid = Validator.TryValidateObject(Model, validationContext, validationResult);
}
async Task OnCategoryChanged()
{
List? newCategory = await CategoryProvider.GetArchiveCategory(Model.Category);
if (newCategory.Count != 1)
{
throw new ArgumentException(nameof(Model.Category), $"Got {newCategory.Count} rows for category name={Model.Category}");
}
if (newCategory is not null)
{
_identifierTextBox.VerifyFormatCategory = newCategory[0];
_isFormDivVisible = true;
StateHasChanged();
}
if (!_categorySelected)
{
_categorySelected = true;
StateHasChanged();
}
await OnChanged();
}
public async Task OnAddCategoryClicked()
{
var options = new DialogOptions { CloseOnEscapeKey = true, BackdropClick = false };
var dialog = await DialogService.ShowAsync("Create a Category", options);
var result = await dialog.Result;
if (result is not null && !result.Canceled)
{
await CategoryProvider.CreateCategoryAsync(CategoryValidationModel.ToArchiveCategory((CategoryValidationModel)result.Data));
StateHasChanged();
await OnChanged();
}
}
private async Task> SearchCategory(string value, CancellationToken cancellationToken)
{
List categories;
if (string.IsNullOrEmpty(value))
{
categories = new(await CategoryProvider.Top(25) ?? []);
}
else
{
categories = new((await CategoryProvider.Search(value) ?? []));
}
List categoryStrings = [];
foreach (var category in categories)
{
categoryStrings.Add(category.Name);
}
return categoryStrings;
}
}