@page "/templates" @using ErsatzTV.Application.Scheduling @implements IDisposable @inject ILogger Logger @inject ISnackbar Snackbar @inject IMediator Mediator @inject IDialogService Dialog @inject NavigationManager NavigationManager
Template Groups
Template Group Name
Add Template Group
Templates
Template Group
@foreach (TemplateGroupViewModel templateGroup in _templateGroups) { @templateGroup.Name }
Template Name
Add Template
@($"{context.Key}")
@context.Name
@if (context.Id >= 0) { } else {
}
@code { private CancellationTokenSource _cts; private readonly List _templates = []; private List _templateGroups = []; private TemplateGroupViewModel _selectedTemplateGroup; private string _templateGroupName; private string _templateName; private string _searchString; public void Dispose() { _cts?.Cancel(); _cts?.Dispose(); } protected override async Task OnParametersSetAsync() { _cts?.Cancel(); _cts?.Dispose(); _cts = new CancellationTokenSource(); var token = _cts.Token; try { _templateGroups = await Mediator.Send(new GetAllTemplateGroups(), token); _templates.Clear(); _templates.AddRange(await Mediator.Send(new GetAllTemplates(), token)); } catch (OperationCanceledException) { // do nothing } } private readonly TableGroupDefinition _groupDefinition = new() { GroupName = "Group", Indentation = false, Expandable = true, Selector = (e) => e.GroupName }; private async Task AddTemplateGroup() { if (!string.IsNullOrWhiteSpace(_templateGroupName)) { Either result = await Mediator.Send(new CreateTemplateGroup(_templateGroupName), _cts.Token); foreach (BaseError error in result.LeftToSeq()) { Snackbar.Add(error.Value, Severity.Error); Logger.LogError("Unexpected error adding template group: {Error}", error.Value); } foreach (TemplateGroupViewModel templateGroup in result.RightToSeq()) { _templateGroupName = null; _templateGroups = await Mediator.Send(new GetAllTemplateGroups(), _cts.Token); _selectedTemplateGroup = _templateGroups.Find(tg => tg.Id == templateGroup.Id); _templates.Clear(); _templates.AddRange(await Mediator.Send(new GetAllTemplates(), _cts.Token)); await InvokeAsync(StateHasChanged); } } } private void UpdateSelectedTemplateGroup(string templateGroupName) { _selectedTemplateGroup = _templateGroups.Find(tg => tg.Name == templateGroupName); InvokeAsync(StateHasChanged); } private async Task AddTemplate() { if (_selectedTemplateGroup is not null && !string.IsNullOrWhiteSpace(_templateName)) { Either result = await Mediator.Send(new CreateTemplate(_selectedTemplateGroup.Id, _templateName), _cts.Token); foreach (BaseError error in result.LeftToSeq()) { Snackbar.Add(error.Value, Severity.Error); Logger.LogError("Unexpected error adding template: {Error}", error.Value); } if (result.IsRight) { _templates.Clear(); _templates.AddRange(await Mediator.Send(new GetAllTemplates(), _cts.Token)); _templateName = null; await InvokeAsync(StateHasChanged); } } } private void OnSearch(string query) { _searchString = query; } private bool FilterTemplates(TemplateViewModel template) => FilterTemplates(template, _searchString); private bool FilterTemplates(TemplateViewModel template, string searchString) { if (string.IsNullOrWhiteSpace(searchString)) { return true; } if (template.Name.Contains(searchString, StringComparison.OrdinalIgnoreCase)) { return true; } return false; } private async Task DeleteTemplateGroup(TemplateViewModel template) { if (template is null) { return; } var parameters = new DialogParameters { { "EntityType", "template group" }, { "EntityName", template.GroupName } }; var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall }; IDialogReference dialog = await Dialog.ShowAsync("Delete Template Group", parameters, options); DialogResult result = await dialog.Result; if (result is not null && !result.Canceled) { await Mediator.Send(new DeleteTemplateGroup(template.TemplateGroupId), _cts.Token); if (_selectedTemplateGroup?.Id == template.TemplateGroupId) { _selectedTemplateGroup = null; } _templateGroups = await Mediator.Send(new GetAllTemplateGroups(), _cts.Token); _templates.Clear(); _templates.AddRange(await Mediator.Send(new GetAllTemplates(), _cts.Token)); await InvokeAsync(StateHasChanged); } } private async Task CopyTemplate(TemplateViewModel template) { var parameters = new DialogParameters { { "TemplateId", template.Id } }; var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall }; IDialogReference dialog = await Dialog.ShowAsync("Copy Template", parameters, options); DialogResult dialogResult = await dialog.Result; if (dialogResult is { Canceled: false, Data: TemplateViewModel data }) { NavigationManager.NavigateTo($"templates/{data.Id}"); } } private async Task DeleteTemplate(TemplateViewModel template) { if (template is null) { return; } var parameters = new DialogParameters { { "EntityType", "template" }, { "EntityName", template.Name } }; var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall }; IDialogReference dialog = await Dialog.ShowAsync("Delete Template", parameters, options); DialogResult result = await dialog.Result; if (result is not null && !result.Canceled) { await Mediator.Send(new DeleteTemplate(template.Id), _cts.Token); _templates.Clear(); _templates.AddRange(await Mediator.Send(new GetAllTemplates(), _cts.Token)); await InvokeAsync(StateHasChanged); } } }