154 lines
7.3 KiB
Plaintext
154 lines
7.3 KiB
Plaintext
@page "/templates"
|
|
@attribute [Authorize]
|
|
@inject IUserSessionService Session
|
|
@inject IUserContextAccessor UserContext
|
|
@inject ISessionManager SessionMgr
|
|
@inject IElevationCoordinator Elevation
|
|
@inject ITemplateService TemplateSvc
|
|
@inject SharepointToolbox.Web.Infrastructure.Persistence.TemplateRepository TemplateRepo
|
|
@inject TranslationSource T
|
|
@rendermode InteractiveServer
|
|
|
|
<h1 class="page-title">@T["templates.page.title"]</h1>
|
|
<p class="page-subtitle">@T["templates.page.subtitle"]</p>
|
|
|
|
@if (!Session.HasProfile) { <NoProfilePrompt /> return; }
|
|
@if (UserContext.Role < UserRole.TechN1) { <WriteGuard /> return; }
|
|
|
|
<div style="display:grid;grid-template-columns:1fr 1fr;gap:16px">
|
|
<div class="card">
|
|
<div class="card-title">@T["templates.capture"]</div>
|
|
<SitePicker Profile="Session.CurrentProfile!" @bind-SelectedSites="_captureSites" Single="true" />
|
|
<div class="form-group mt-8">
|
|
<label class="form-label">@T["templates.name"]</label>
|
|
<input class="form-input" @bind="_captureName" placeholder="@T["templates.name.placeholder"]" />
|
|
</div>
|
|
<div class="flex-row" style="flex-wrap:wrap">
|
|
<label><input type="checkbox" @bind="_capLibraries" /> @T["templates.opt.libraries"]</label>
|
|
<label><input type="checkbox" @bind="_capFolders" /> @T["templates.opt.folders"]</label>
|
|
<label><input type="checkbox" @bind="_capGroups" /> @T["templates.opt.permissions"]</label>
|
|
</div>
|
|
<button class="btn btn-primary mt-8" @onclick="CaptureTemplate" disabled="@_running">
|
|
@(_running ? T["templates.btn.capturing"] : T["templates.btn.capture"])
|
|
</button>
|
|
<ProgressPanel IsRunning="_running" StatusMessage="@_status" />
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-title">@T["templates.apply"]</div>
|
|
@if (_selectedTemplate == null)
|
|
{
|
|
<div class="alert alert-info">@T["templates.apply.selectprompt"]</div>
|
|
}
|
|
else
|
|
{
|
|
<div class="alert alert-info">@T["templates.apply.selectedlabel"] <strong>@_selectedTemplate.Name</strong></div>
|
|
<div class="form-group">
|
|
<label class="form-label">@T["templates.newtitle"]</label>
|
|
<input class="form-input" @bind="_newTitle" />
|
|
</div>
|
|
<div class="form-group">
|
|
<label class="form-label">@T["templates.newalias"]</label>
|
|
<input class="form-input" @bind="_newAlias" />
|
|
</div>
|
|
<div class="form-group">
|
|
<label class="form-label">@T["templates.adminurl"]</label>
|
|
<input class="form-input" @bind="_adminUrl" placeholder="https://contoso-admin.sharepoint.com" />
|
|
</div>
|
|
<button class="btn btn-primary" @onclick="ApplyTemplate" disabled="@_running">
|
|
@(_running ? T["templates.btn.applying"] : T["templates.apply"])
|
|
</button>
|
|
}
|
|
</div>
|
|
</div>
|
|
|
|
@if (!string.IsNullOrEmpty(_error)) { <div class="alert alert-error mt-8">@_error</div> }
|
|
@if (!string.IsNullOrEmpty(_successMsg)) { <div class="alert alert-success mt-8">@_successMsg</div> }
|
|
|
|
<div class="card" style="margin-top:16px">
|
|
<div class="card-title">@T["templates.list"]</div>
|
|
@if (_templates.Count == 0)
|
|
{
|
|
<div class="text-muted">@T["templates.empty"]</div>
|
|
}
|
|
@foreach (var t in _templates)
|
|
{
|
|
<div class="flex-row" style="padding:8px 0;border-bottom:1px solid var(--border)">
|
|
<div>
|
|
<div style="font-weight:600">@t.Name</div>
|
|
<div class="text-muted">@t.SiteType · @t.CapturedAt.ToString("yyyy-MM-dd") · @string.Format(T["templates.libraries.suffix"], t.Libraries.Count)</div>
|
|
</div>
|
|
<div class="spacer"></div>
|
|
<button class="btn btn-secondary btn-sm" @onclick="() => _selectedTemplate = t">@T["templates.btn.use"]</button>
|
|
<button class="btn btn-danger btn-sm" @onclick="() => DeleteTemplate(t)">@T["templates.delete"]</button>
|
|
</div>
|
|
}
|
|
</div>
|
|
|
|
@code {
|
|
private List<SiteInfo> _captureSites = new();
|
|
private string _captureName = string.Empty;
|
|
private bool _capLibraries = true, _capFolders = true, _capGroups = true;
|
|
private SiteTemplate? _selectedTemplate;
|
|
private string _newTitle = string.Empty, _newAlias = string.Empty, _adminUrl = string.Empty;
|
|
private bool _running; private string _status = string.Empty, _error = string.Empty, _successMsg = string.Empty;
|
|
private List<SiteTemplate> _templates = new();
|
|
private CancellationTokenSource? _cts;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
_templates = (await TemplateRepo.GetAllAsync()).ToList();
|
|
}
|
|
|
|
private async Task CaptureTemplate()
|
|
{
|
|
_error = string.Empty; _successMsg = string.Empty; _running = true;
|
|
_cts = new CancellationTokenSource();
|
|
var siteUrl = _captureSites.FirstOrDefault()?.Url ?? Session.CurrentProfile!.TenantUrl;
|
|
var progress = new Progress<OperationProgress>(p => { _status = p.Message; InvokeAsync(StateHasChanged); });
|
|
try
|
|
{
|
|
var opts = new SiteTemplateOptions { CaptureLibraries = _capLibraries, CaptureFolders = _capFolders, CapturePermissionGroups = _capGroups };
|
|
var template = await Elevation.RunAsync(async c =>
|
|
{
|
|
var ctx = await SessionMgr.GetOrCreateContextAsync(siteUrl, Session.CurrentProfile!, c);
|
|
return await TemplateSvc.CaptureTemplateAsync(ctx, opts, progress, c);
|
|
}, _cts.Token);
|
|
template.Name = string.IsNullOrWhiteSpace(_captureName) ? $"Template-{DateTime.Now:yyyyMMdd}" : _captureName;
|
|
await TemplateRepo.SaveAsync(template);
|
|
_templates = (await TemplateRepo.GetAllAsync()).ToList();
|
|
_successMsg = string.Format(T["templates.status.saved"], template.Name);
|
|
}
|
|
catch (OperationCanceledException) { _status = T["templates.status.cancelled"]; }
|
|
catch (Exception ex) { _error = ex.Message; }
|
|
finally { _running = false; await InvokeAsync(StateHasChanged); }
|
|
}
|
|
|
|
private async Task ApplyTemplate()
|
|
{
|
|
if (_selectedTemplate == null) return;
|
|
_error = string.Empty; _successMsg = string.Empty; _running = true;
|
|
_cts = new CancellationTokenSource();
|
|
var adminUrl = string.IsNullOrWhiteSpace(_adminUrl)
|
|
? Session.CurrentProfile!.TenantUrl.Replace(".sharepoint.com", "-admin.sharepoint.com")
|
|
: _adminUrl.Trim();
|
|
var progress = new Progress<OperationProgress>(p => { _status = p.Message; InvokeAsync(StateHasChanged); });
|
|
try
|
|
{
|
|
var ctx = await SessionMgr.GetOrCreateContextAsync(adminUrl, Session.CurrentProfile!, _cts.Token);
|
|
var url = await TemplateSvc.ApplyTemplateAsync(ctx, _selectedTemplate, _newTitle, _newAlias, progress, _cts.Token);
|
|
_successMsg = string.Format(T["templates.status.sitecreated"], url);
|
|
}
|
|
catch (OperationCanceledException) { _status = T["templates.status.cancelled"]; }
|
|
catch (Exception ex) { _error = ex.Message; }
|
|
finally { _running = false; await InvokeAsync(StateHasChanged); }
|
|
}
|
|
|
|
private async Task DeleteTemplate(SiteTemplate t)
|
|
{
|
|
await TemplateRepo.DeleteAsync(t.Id);
|
|
_templates.RemoveAll(x => x.Id == t.Id);
|
|
if (_selectedTemplate?.Id == t.Id) _selectedTemplate = null;
|
|
}
|
|
}
|