Files
ersatztv/ErsatzTV/Pages/Blocks.razor
T
Jason DoveandGitHub 2a9f23cce6 update layout for group editors (#2140)
* update block group editor

* update playlist group editor

* update template group editor

* update deco group editor

* update deco template group editor

* update deco editor

* update logs layout

* update changelog
2025-07-12 13:45:50 +00:00

209 lines
9.5 KiB
Plaintext

@page "/blocks"
@using ErsatzTV.Application.Scheduling
@implements IDisposable
@inject ILogger<Blocks> Logger
@inject ISnackbar Snackbar
@inject IMediator Mediator
@inject IDialogService Dialog
<MudForm Style="max-height: 100%">
<div class="d-flex flex-column" style="height: 100vh; overflow-x: auto">
<MudContainer MaxWidth="MaxWidth.ExtraLarge" Class="pt-8">
<MudText Typo="Typo.h5" Class="mb-2">Block Groups</MudText>
<MudDivider Class="mb-6"/>
<MudStack Row="true" Breakpoint="Breakpoint.SmAndDown" Class="form-field-stack gap-md-8 mb-5">
<div class="d-flex">
<MudText>Block Group Name</MudText>
</div>
<MudTextField @bind-Value="_blockGroupName" For="@(() => _blockGroupName)"/>
</MudStack>
<MudStack Row="true" Breakpoint="Breakpoint.SmAndDown" Class="form-field-stack gap-md-8 mb-5">
<div class="d-flex"></div>
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@(_ => AddBlockGroup())" StartIcon="@Icons.Material.Filled.Add">
Add Block Group
</MudButton>
</MudStack>
<MudText Typo="Typo.h5" Class="mt-10 mb-2">Blocks</MudText>
<MudDivider Class="mb-6"/>
<MudStack Row="true" Breakpoint="Breakpoint.SmAndDown" Class="form-field-stack gap-md-8 mb-5">
<div class="d-flex">
<MudText>Block Group</MudText>
</div>
<MudSelect @bind-Value="_selectedBlockGroup">
@foreach (BlockGroupViewModel blockGroup in _blockGroups)
{
<MudSelectItem Value="@blockGroup">@blockGroup.Name</MudSelectItem>
}
</MudSelect>
</MudStack>
<MudStack Row="true" Breakpoint="Breakpoint.SmAndDown" Class="form-field-stack gap-md-8 mb-5">
<div class="d-flex">
<MudText>Block Name</MudText>
</div>
<MudTextField @bind-Value="_blockName" For="@(() => _blockName)"/>
</MudStack>
<MudStack Row="true" Breakpoint="Breakpoint.SmAndDown" Class="form-field-stack gap-md-8 mb-5">
<div class="d-flex"></div>
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@(_ => AddBlock())" StartIcon="@Icons.Material.Filled.Add">
Add Block
</MudButton>
</MudStack>
<MudStack Row="true" Breakpoint="Breakpoint.SmAndDown" Class="form-field-stack gap-md-8 mb-5">
<div class="d-flex"></div>
<MudTreeView T="BlockTreeItemViewModel" Items="@_treeItems" Hover="true" Style="width: 100%">
<ItemTemplate Context="item">
<MudTreeViewItem T="BlockTreeItemViewModel" Items="@item.Value!.TreeItems" Icon="@item.Value.Icon" Value="@item.Value">
<BodyContent>
<div style="display: grid; grid-template-columns: 1fr auto; align-items: center; width: 100%">
<MudGrid Justify="Justify.FlexStart">
<MudItem xs="5">
<MudText>@item.Value.Text</MudText>
</MudItem>
@if (!string.IsNullOrWhiteSpace(item.Value.EndText))
{
<MudItem xs="6">
<MudText>@item.Value.EndText</MudText>
</MudItem>
}
</MudGrid>
<div style="justify-self: end;">
@foreach (int blockId in Optional(item.Value.BlockId))
{
<MudIconButton Icon="@Icons.Material.Filled.Edit" Size="Size.Medium" Color="Color.Inherit" Href="@($"blocks/{blockId}")"/>
}
<MudIconButton Icon="@Icons.Material.Filled.Delete" Size="Size.Medium" Color="Color.Inherit" OnClick="@(_ => DeleteItem(item.Value))"/>
</div>
</div>
</BodyContent>
</MudTreeViewItem>
</ItemTemplate>
</MudTreeView>
</MudStack>
</MudContainer>
</div>
</MudForm>
@code {
private readonly CancellationTokenSource _cts = new();
private readonly List<TreeItemData<BlockTreeItemViewModel>> _treeItems = [];
private List<BlockGroupViewModel> _blockGroups = [];
private BlockGroupViewModel _selectedBlockGroup;
private string _blockGroupName;
private string _blockName;
public void Dispose()
{
_cts.Cancel();
_cts.Dispose();
}
protected override async Task OnParametersSetAsync()
{
await ReloadBlockTree();
await InvokeAsync(StateHasChanged);
}
private async Task ReloadBlockTree()
{
_blockGroups = await Mediator.Send(new GetAllBlockGroups(), _cts.Token);
_treeItems.Clear();
BlockTreeViewModel tree = await Mediator.Send(new GetBlockTree(), _cts.Token);
foreach (BlockTreeBlockGroupViewModel group in tree.Groups)
{
_treeItems.Add(new TreeItemData<BlockTreeItemViewModel> { Value = new BlockTreeItemViewModel(group) });
}
}
private async Task AddBlockGroup()
{
if (!string.IsNullOrWhiteSpace(_blockGroupName))
{
Either<BaseError, BlockGroupViewModel> result = await Mediator.Send(new CreateBlockGroup(_blockGroupName), _cts.Token);
foreach (BaseError error in result.LeftToSeq())
{
Snackbar.Add(error.Value, Severity.Error);
Logger.LogError("Unexpected error adding block group: {Error}", error.Value);
}
foreach (BlockGroupViewModel blockGroup in result.RightToSeq())
{
_treeItems.Add(new TreeItemData<BlockTreeItemViewModel> { Value = new BlockTreeItemViewModel(blockGroup) });
_blockGroupName = null;
_blockGroups = await Mediator.Send(new GetAllBlockGroups(), _cts.Token);
await InvokeAsync(StateHasChanged);
}
}
}
private async Task AddBlock()
{
if (_selectedBlockGroup is not null && !string.IsNullOrWhiteSpace(_blockName))
{
Either<BaseError, BlockViewModel> result = await Mediator.Send(new CreateBlock(_selectedBlockGroup.Id, _blockName), _cts.Token);
foreach (BaseError error in result.LeftToSeq())
{
Snackbar.Add(error.Value, Severity.Error);
Logger.LogError("Unexpected error adding block: {Error}", error.Value);
}
foreach (BlockViewModel block in result.RightToSeq())
{
foreach (BlockTreeItemViewModel item in _treeItems.Map(i => i.Value).Where(item => item.BlockGroupId == _selectedBlockGroup.Id))
{
item.TreeItems.Add(new TreeItemData<BlockTreeItemViewModel> { Value = new BlockTreeItemViewModel(block) });
}
_blockName = null;
await InvokeAsync(StateHasChanged);
}
}
}
private async Task DeleteItem(BlockTreeItemViewModel treeItem)
{
foreach (int blockGroupId in Optional(treeItem.BlockGroupId))
{
var parameters = new DialogParameters { { "EntityType", "block group" }, { "EntityName", treeItem.Text } };
var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall };
IDialogReference dialog = await Dialog.ShowAsync<DeleteDialog>("Delete Block Group", parameters, options);
DialogResult result = await dialog.Result;
if (result is not null && !result.Canceled)
{
await Mediator.Send(new DeleteBlockGroup(blockGroupId), _cts.Token);
_treeItems.RemoveAll(i => i.Value?.BlockGroupId == blockGroupId);
if (_selectedBlockGroup?.Id == blockGroupId)
{
_selectedBlockGroup = null;
}
_blockGroups = await Mediator.Send(new GetAllBlockGroups(), _cts.Token);
await InvokeAsync(StateHasChanged);
}
}
foreach (int blockId in Optional(treeItem.BlockId))
{
var parameters = new DialogParameters { { "EntityType", "block" }, { "EntityName", treeItem.Text } };
var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall };
IDialogReference dialog = await Dialog.ShowAsync<DeleteDialog>("Delete Block", parameters, options);
DialogResult result = await dialog.Result;
if (result is not null && !result.Canceled)
{
await Mediator.Send(new DeleteBlock(blockId), _cts.Token);
foreach (BlockTreeItemViewModel parent in _treeItems.Map(i => i.Value))
{
parent.TreeItems.RemoveAll(i => i.Value == treeItem);
}
await InvokeAsync(StateHasChanged);
}
}
}
}