92 lines
3.8 KiB
Plaintext
92 lines
3.8 KiB
Plaintext
@page "/folder-structure"
|
|
@attribute [Authorize]
|
|
@inject IUserSessionService Session
|
|
@inject IUserContextAccessor UserContext
|
|
@inject ISessionManager SessionMgr
|
|
@inject IFolderStructureService FolderSvc
|
|
@inject ICsvValidationService CsvValidation
|
|
@rendermode InteractiveServer
|
|
|
|
<h1 class="page-title">Folder Structure</h1>
|
|
<p class="page-subtitle">Create folder hierarchies in a document library from a CSV template.</p>
|
|
|
|
@if (!Session.HasProfile) { <NoProfilePrompt /> return; }
|
|
@if (UserContext.Role < UserRole.TechN1) { <WriteGuard /> return; }
|
|
|
|
<div class="card">
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label class="form-label">Site URL</label>
|
|
<input class="form-input" @bind="_siteUrl" placeholder="@Session.CurrentProfile!.TenantUrl" />
|
|
</div>
|
|
<div class="form-group">
|
|
<label class="form-label">Library Title</label>
|
|
<input class="form-input" @bind="_libraryTitle" placeholder="Shared Documents" />
|
|
</div>
|
|
</div>
|
|
<div class="form-group">
|
|
<label class="form-label">CSV File (Level1, Level2, Level3, Level4)</label>
|
|
<InputFile OnChange="LoadFile" accept=".csv" class="form-input" />
|
|
</div>
|
|
|
|
@if (_rows.Count > 0)
|
|
{
|
|
<div class="alert alert-info mt-8">@_rows.Count(r => r.IsValid) valid rows, @_rows.Count(r => !r.IsValid) errors.</div>
|
|
}
|
|
|
|
<div class="flex-row mt-8">
|
|
<button class="btn btn-primary" @onclick="RunCreate" disabled="@(_running || _rows.Count == 0 || string.IsNullOrWhiteSpace(_libraryTitle))">
|
|
@(_running ? "Creating…" : "Create Folders")
|
|
</button>
|
|
@if (_running) { <button class="btn btn-secondary" @onclick="Cancel">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")">
|
|
Created: @_summary.SuccessCount folders. Failures: @_summary.FailedCount
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
@code {
|
|
private string _siteUrl = string.Empty, _libraryTitle = string.Empty;
|
|
private List<CsvValidationRow<FolderStructureRow>> _rows = new();
|
|
private bool _running; private string _status = string.Empty, _error = string.Empty;
|
|
private int _current, _total;
|
|
private BulkOperationSummary<string>? _summary;
|
|
private CancellationTokenSource? _cts;
|
|
|
|
private async Task LoadFile(InputFileChangeEventArgs e)
|
|
{
|
|
_rows.Clear();
|
|
using var stream = e.File.OpenReadStream(maxAllowedSize: 5 * 1024 * 1024);
|
|
_rows = CsvValidation.ParseAndValidateFolders(stream);
|
|
}
|
|
|
|
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 = string.IsNullOrWhiteSpace(_siteUrl) ? Session.CurrentProfile!.TenantUrl : _siteUrl.Trim();
|
|
var progress = new Progress<OperationProgress>(p => { _status = p.Message; _current = p.Current; _total = p.Total; InvokeAsync(StateHasChanged); });
|
|
try
|
|
{
|
|
var ctx = await SessionMgr.GetOrCreateContextAsync(siteUrl, Session.CurrentProfile!, _cts.Token);
|
|
_summary = await FolderSvc.CreateFoldersAsync(ctx, _libraryTitle, validRows, progress, _cts.Token);
|
|
_status = $"Complete: {_summary.SuccessCount} folders created.";
|
|
}
|
|
catch (OperationCanceledException) { _status = "Cancelled."; }
|
|
catch (Exception ex) { _error = ex.Message; }
|
|
finally { _running = false; await InvokeAsync(StateHasChanged); }
|
|
}
|
|
|
|
private void Cancel() => _cts?.Cancel();
|
|
}
|