@page "/schedules" @using ErsatzTV.Application.Configuration @using ErsatzTV.Application.ProgramSchedules @implements IDisposable @inject IDialogService Dialog @inject IMediator Mediator @inject NavigationManager NavigationManager Add Schedule
Schedules @context.Name
@if (_selectedSchedule != null) { @_selectedSchedule.Name Items Start Time Collection Playout Mode @(context.StartType == StartType.Fixed ? context.StartTime == null ? string.Empty : DateTime.Today.Add(context.StartTime.Value).ToShortTimeString() : "Dynamic") @context.Name @context.PlayoutMode }
@code { private CancellationTokenSource _cts; private MudTable _table; private MudTable _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("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("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> 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 { TotalItems = data.TotalCount, Items = data.Page }; } private async Task> DetailServerReload(TableState state, CancellationToken cancellationToken) { await Mediator.Send(new SaveConfigElementByKey(ConfigElementKey.SchedulesDetailPageSize, state.PageSize.ToString()), cancellationToken); List scheduleItems = await Mediator.Send(new GetProgramScheduleItems(_selectedSchedule.Id), cancellationToken); IOrderedEnumerable sorted = scheduleItems.OrderBy(s => s.Index); // TODO: properly page this data return new TableData { 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> maybeResults = await Mediator.Send(new GetProgramScheduleItems(schedule.Id), _cts.Token); foreach (IEnumerable 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(schedule.Name, parameters, options); DialogResult _ = await dialog.Result; } } private string SelectedRowClassFunc(ProgramScheduleViewModel element, int rowNumber) => _selectedSchedule != null && _selectedSchedule == element ? "selected" : string.Empty; }