Files
ersatztv/ErsatzTV/Pages/Schedules.razor
T

236 lines
11 KiB
Plaintext

@page "/schedules"
@using ErsatzTV.Application.Configuration
@using ErsatzTV.Application.ProgramSchedules
@implements IDisposable
@inject IDialogService Dialog
@inject IMediator Mediator
@inject NavigationManager NavigationManager
<MudForm Style="max-height: 100%">
<MudPaper Square="true" Style="display: flex; height: 64px; min-height: 64px; width: 100%; z-index: 100; align-items: center">
<MudButton Variant="Variant.Filled" Color="Color.Primary" Class="ml-6" StartIcon="@Icons.Material.Filled.Add" Href="schedules/add">
Add Schedule
</MudButton>
</MudPaper>
<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">Schedules</MudText>
<MudDivider Class="mb-6"/>
<MudTable Hover="true"
Dense="true"
Breakpoint="Breakpoint.None"
SelectedItemChanged="@(async (ProgramScheduleViewModel x) => await ScheduleSelected(x))"
@bind-RowsPerPage="@_rowsPerPage"
ServerData="@(new Func<TableState, CancellationToken, Task<TableData<ProgramScheduleViewModel>>>(ServerReload))"
@ref="_table"
RowClassFunc="@SelectedRowClassFunc">
<ColGroup>
<MudHidden Breakpoint="Breakpoint.Xs">
<col/>
<col style="width: 300px;"/>
</MudHidden>
</ColGroup>
<ToolBarContent>
<MudTextField T="string"
ValueChanged="@(s => OnSearch(s))"
Placeholder="Search for schedules"
Adornment="Adornment.Start"
AdornmentIcon="@Icons.Material.Filled.FilterList"
Clearable="true">
</MudTextField>
</ToolBarContent>
<RowTemplate>
<MudTd>@context.Name</MudTd>
<MudTd>
<div style="align-items: center; display: flex;">
<MudTooltip Text="Edit Properties">
<MudIconButton Icon="@Icons.Material.Filled.Edit"
Href="@($"schedules/{context.Id}")">
</MudIconButton>
</MudTooltip>
<MudTooltip Text="Edit Schedule Items">
<MudIconButton Icon="@Icons.Material.Filled.FormatListNumbered"
Href="@($"schedules/{context.Id}/items")">
</MudIconButton>
</MudTooltip>
<MudTooltip Text="Copy Schedule">
<MudIconButton Icon="@Icons.Material.Filled.ContentCopy"
OnClick="@(_ => CopySchedule(context))">
</MudIconButton>
</MudTooltip>
<MudTooltip Text="Troubleshoot">
<MudIconButton Icon="@Icons.Material.Filled.Info"
OnClick="@(_ => Troubleshoot(context))">
</MudIconButton>
</MudTooltip>
<MudTooltip Text="Delete Schedule">
<MudIconButton Icon="@Icons.Material.Filled.Delete"
OnClick="@(_ => DeleteSchedule(context))">
</MudIconButton>
</MudTooltip>
</div>
</MudTd>
</RowTemplate>
<PagerContent>
<MudTablePager/>
</PagerContent>
</MudTable>
@if (_selectedSchedule != null)
{
<MudHidden Breakpoint="Breakpoint.SmAndDown">
<MudTable Hover="true"
Class="mt-8"
@bind-RowsPerPage="@_detailRowsPerPage"
ServerData="@(new Func<TableState, CancellationToken, Task<TableData<ProgramScheduleItemViewModel>>>(DetailServerReload))"
@ref="_detailTable">
<ToolBarContent>
<MudText Typo="Typo.h6">@_selectedSchedule.Name Items</MudText>
</ToolBarContent>
<HeaderContent>
<MudTh>Start Time</MudTh>
<MudTh>Collection</MudTh>
<MudTh>Playout Mode</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="Start Time">
@(context.StartType == StartType.Fixed ? context.StartTime == null ? string.Empty : DateTime.Today.Add(context.StartTime.Value).ToShortTimeString() : "Dynamic")
</MudTd>
<MudTd DataLabel="Collection">@context.Name</MudTd>
<MudTd DataLabel="Playout Mode">@context.PlayoutMode</MudTd>
</RowTemplate>
<PagerContent>
<MudTablePager/>
</PagerContent>
</MudTable>
</MudHidden>
}
</MudContainer>
</div>
</MudForm>
@code {
private CancellationTokenSource _cts;
private MudTable<ProgramScheduleViewModel> _table;
private MudTable<ProgramScheduleItemViewModel> _detailTable;
private int _rowsPerPage = 10;
private int _detailRowsPerPage = 10;
private string _searchString;
private ProgramScheduleViewModel _selectedSchedule;
public void Dispose()
{
_cts?.Cancel();
_cts?.Dispose();
}
protected override async Task OnParametersSetAsync()
{
_cts?.Cancel();
_cts?.Dispose();
_cts = new CancellationTokenSource();
var token = _cts.Token;
try
{
_rowsPerPage = await Mediator.Send(new GetConfigElementByKey(ConfigElementKey.SchedulesPageSize), token)
.Map(maybeRows => maybeRows.Match(ce => int.TryParse(ce.Value, out int rows) ? rows : 10, () => 10));
_detailRowsPerPage = await Mediator.Send(new GetConfigElementByKey(ConfigElementKey.SchedulesDetailPageSize), token)
.Map(maybeRows => maybeRows.Match(ce => int.TryParse(ce.Value, out int rows) ? rows : 10, () => 10));
}
catch (OperationCanceledException)
{
// do nothing
}
}
private async Task ScheduleSelected(ProgramScheduleViewModel schedule)
{
_selectedSchedule = schedule;
if (_selectedSchedule != null && _detailTable != null)
{
await _detailTable.ReloadServerData();
}
}
private async Task DeleteSchedule(ProgramScheduleViewModel programSchedule)
{
var parameters = new DialogParameters { { "EntityType", "schedule" }, { "EntityName", programSchedule.Name } };
var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall };
IDialogReference dialog = await Dialog.ShowAsync<DeleteDialog>("Delete Schedule", parameters, options);
DialogResult result = await dialog.Result;
if (result is { Canceled: false })
{
await Mediator.Send(new DeleteProgramSchedule(programSchedule.Id), _cts.Token);
if (_table != null)
{
await _table.ReloadServerData();
}
if (_selectedSchedule == programSchedule)
{
_selectedSchedule = null;
}
}
}
private async Task CopySchedule(ProgramScheduleViewModel programSchedule)
{
var parameters = new DialogParameters { { "ProgramScheduleId", programSchedule.Id } };
var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall };
IDialogReference dialog = await Dialog.ShowAsync<CopyScheduleDialog>("Copy Schedule", parameters, options);
DialogResult dialogResult = await dialog.Result;
if (dialogResult is { Canceled: false, Data: ProgramScheduleViewModel data })
{
NavigationManager.NavigateTo($"schedules/{data.Id}/items");
}
}
private async Task<TableData<ProgramScheduleViewModel>> ServerReload(TableState state, CancellationToken cancellationToken)
{
await Mediator.Send(new SaveConfigElementByKey(ConfigElementKey.SchedulesPageSize, state.PageSize.ToString()), cancellationToken);
PagedProgramSchedulesViewModel data = await Mediator.Send(new GetPagedProgramSchedules(_searchString, state.Page, state.PageSize), cancellationToken);
return new TableData<ProgramScheduleViewModel> { TotalItems = data.TotalCount, Items = data.Page };
}
private async Task<TableData<ProgramScheduleItemViewModel>> DetailServerReload(TableState state, CancellationToken cancellationToken)
{
await Mediator.Send(new SaveConfigElementByKey(ConfigElementKey.SchedulesDetailPageSize, state.PageSize.ToString()), cancellationToken);
List<ProgramScheduleItemViewModel> scheduleItems = await Mediator.Send(new GetProgramScheduleItems(_selectedSchedule.Id), cancellationToken);
IOrderedEnumerable<ProgramScheduleItemViewModel> sorted = scheduleItems.OrderBy(s => s.Index);
// TODO: properly page this data
return new TableData<ProgramScheduleItemViewModel>
{
TotalItems = scheduleItems.Count,
Items = sorted.Skip(state.Page * state.PageSize).Take(state.PageSize)
};
}
private void OnSearch(string query)
{
_selectedSchedule = null;
_searchString = query;
_table.ReloadServerData();
}
private async Task Troubleshoot(ProgramScheduleViewModel schedule)
{
Option<IEnumerable<ProgramScheduleItemViewModel>> maybeResults = await Mediator.Send(new GetProgramScheduleItems(schedule.Id), _cts.Token);
foreach (IEnumerable<ProgramScheduleItemViewModel> results in maybeResults)
{
var sorted = results.OrderBy(i => i.Index).ToList();
var parameters = new DialogParameters { { "Schedule", schedule }, { "Items", sorted } };
var options = new DialogOptions { CloseButton = true, CloseOnEscapeKey = true, MaxWidth = MaxWidth.Medium, FullWidth = true };
IDialogReference dialog = await Dialog.ShowAsync<ScheduleItemsDialog>(schedule.Name, parameters, options);
DialogResult _ = await dialog.Result;
}
}
private string SelectedRowClassFunc(ProgramScheduleViewModel element, int rowNumber) => _selectedSchedule != null && _selectedSchedule == element ? "selected" : string.Empty;
}