156 lines
6.1 KiB
Plaintext
156 lines
6.1 KiB
Plaintext
@page "/folder-structure"
|
|
@attribute [Authorize]
|
|
@inject IUserSessionService Session
|
|
@inject IUserContextAccessor UserContext
|
|
@inject ISessionManager SessionMgr
|
|
@inject IElevationCoordinator Elevation
|
|
@inject IFolderStructureService FolderSvc
|
|
@inject ICsvValidationService CsvValidation
|
|
@inject TranslationSource T
|
|
@rendermode InteractiveServer
|
|
|
|
<h1 class="page-title">@T["tab.folderStructure"]</h1>
|
|
<p class="page-subtitle">@T["folderstruct.subtitle"]</p>
|
|
|
|
@if (!Session.HasProfile) { <NoProfilePrompt /> return; }
|
|
@if (UserContext.Role < UserRole.TechN1) { <WriteGuard /> return; }
|
|
|
|
<div class="card">
|
|
<SitePicker Profile="Session.CurrentProfile!" @bind-SelectedSites="_sites" Single="true" />
|
|
<div class="form-row mt-8">
|
|
<LibraryPicker Profile="Session.CurrentProfile!" SiteUrl="@(_sites.FirstOrDefault()?.Url)" @bind-Library="_libraryTitle" Label="@T["folderstruct.lbl.libraryTitle"]" />
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label class="form-label">@T["folderstruct.lbl.source"]</label>
|
|
<div class="flex-row">
|
|
<button class="btn btn-sm @(_mode == InputMode.Csv ? "btn-primary" : "btn-secondary")"
|
|
type="button" @onclick="() => SetMode(InputMode.Csv)">@T["folderstruct.btn.uploadCsv"]</button>
|
|
<button class="btn btn-sm @(_mode == InputMode.Builder ? "btn-primary" : "btn-secondary")"
|
|
type="button" @onclick="() => SetMode(InputMode.Builder)">@T["folderstruct.btn.buildVisually"]</button>
|
|
</div>
|
|
</div>
|
|
|
|
@if (_mode == InputMode.Csv)
|
|
{
|
|
<div class="form-group">
|
|
<label class="form-label">@T["folderstruct.lbl.csvFile"]</label>
|
|
<InputFile OnChange="LoadFile" accept=".csv" class="form-input" />
|
|
</div>
|
|
}
|
|
else
|
|
{
|
|
<div class="form-group">
|
|
<label class="form-label">@T["tab.folderStructure"]</label>
|
|
<div class="folder-builder">
|
|
@foreach (var root in _tree)
|
|
{
|
|
<FolderTreeNode Node="root" Depth="1" OnRemove="RemoveRoot" OnChanged="RebuildFromTree" />
|
|
}
|
|
@if (_tree.Count == 0)
|
|
{
|
|
<p class="text-muted">@T["folderstruct.builder.empty"]</p>
|
|
}
|
|
</div>
|
|
<button class="btn btn-secondary btn-sm mt-8" type="button" @onclick="AddRoot">@T["folderstruct.btn.addRoot"]</button>
|
|
</div>
|
|
}
|
|
|
|
@if (_rows.Count > 0)
|
|
{
|
|
<div class="alert alert-info mt-8">@string.Format(T["folderstruct.rowsSummary"], _rows.Count(r => r.IsValid), _rows.Count(r => !r.IsValid))</div>
|
|
}
|
|
|
|
<div class="flex-row mt-8">
|
|
<button class="btn btn-primary" @onclick="RunCreate" disabled="@(_running || _rows.Count == 0 || string.IsNullOrWhiteSpace(_libraryTitle))">
|
|
@(_running ? T["folderstruct.btn.creating"] : T["folderstruct.execute"])
|
|
</button>
|
|
@if (_running) { <button class="btn btn-secondary" @onclick="Cancel">@T["btn.cancel"]</button> }
|
|
</div>
|
|
<ProgressPanel IsRunning="_running" StatusMessage="@_status" Current="_current" Total="_total" />
|
|
</div>
|
|
|
|
@if (!string.IsNullOrEmpty(_error)) { <div class="alert alert-error">@_error</div> }
|
|
|
|
@if (_summary != null)
|
|
{
|
|
<div class="card">
|
|
<div class="alert @(_summary.HasFailures ? "alert-warn" : "alert-success")">
|
|
@string.Format(T["folderstruct.result"], _summary.SuccessCount, _summary.FailedCount)
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
@code {
|
|
private enum InputMode { Csv, Builder }
|
|
private InputMode _mode = InputMode.Csv;
|
|
|
|
private List<SiteInfo> _sites = new();
|
|
private string _libraryTitle = string.Empty;
|
|
private List<CsvValidationRow<FolderStructureRow>> _rows = new();
|
|
private readonly List<FolderNode> _tree = new();
|
|
private bool _running; private string _status = string.Empty, _error = string.Empty;
|
|
private int _current, _total;
|
|
private BulkOperationSummary<string>? _summary;
|
|
private CancellationTokenSource? _cts;
|
|
|
|
private void SetMode(InputMode mode)
|
|
{
|
|
_mode = mode;
|
|
_rows.Clear();
|
|
_summary = null; _error = string.Empty;
|
|
if (mode == InputMode.Builder) RebuildFromTree();
|
|
}
|
|
|
|
private async Task LoadFile(InputFileChangeEventArgs e)
|
|
{
|
|
_rows.Clear();
|
|
using var stream = e.File.OpenReadStream(maxAllowedSize: 5 * 1024 * 1024);
|
|
_rows = CsvValidation.ParseAndValidateFolders(stream);
|
|
}
|
|
|
|
private void AddRoot()
|
|
{
|
|
_tree.Add(new FolderNode());
|
|
RebuildFromTree();
|
|
}
|
|
|
|
private void RemoveRoot(FolderNode node)
|
|
{
|
|
_tree.Remove(node);
|
|
RebuildFromTree();
|
|
}
|
|
|
|
// Convert the visual tree into validation rows so the count + Create button share the CSV path.
|
|
private void RebuildFromTree()
|
|
{
|
|
_rows = FolderNode.Flatten(_tree)
|
|
.Select(r => new CsvValidationRow<FolderStructureRow>(r, new List<string>()))
|
|
.ToList();
|
|
}
|
|
|
|
private async Task RunCreate()
|
|
{
|
|
_error = string.Empty; _summary = null; _running = true;
|
|
_cts = new CancellationTokenSource();
|
|
var validRows = _rows.Where(r => r.IsValid && r.Record != null).Select(r => r.Record!).ToList();
|
|
var siteUrl = _sites.FirstOrDefault()?.Url;
|
|
if (string.IsNullOrWhiteSpace(siteUrl)) { _error = T["folderstruct.err.selectSite"]; _running = false; return; }
|
|
var progress = new Progress<OperationProgress>(p => { _status = p.Message; _current = p.Current; _total = p.Total; InvokeAsync(StateHasChanged); });
|
|
try
|
|
{
|
|
_summary = await Elevation.RunAsync(async c =>
|
|
{
|
|
var ctx = await SessionMgr.GetOrCreateContextAsync(siteUrl, Session.CurrentProfile!, c);
|
|
return await FolderSvc.CreateFoldersAsync(ctx, _libraryTitle, validRows, progress, c);
|
|
}, _cts.Token);
|
|
_status = string.Format(T["folderstruct.status.complete"], _summary.SuccessCount);
|
|
}
|
|
catch (OperationCanceledException) { _status = T["status.cancelled"]; }
|
|
catch (Exception ex) { _error = ex.Message; }
|
|
finally { _running = false; await InvokeAsync(StateHasChanged); }
|
|
}
|
|
|
|
private void Cancel() => _cts?.Cancel();
|
|
}
|