Initial commit
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
@page "/channels/{Id:int?}"
|
||||
@page "/channels/add"
|
||||
@using ErsatzTV.Application.FFmpegProfiles
|
||||
@using ErsatzTV.Application.FFmpegProfiles.Queries
|
||||
@using ErsatzTV.Application.Images.Commands
|
||||
@using ErsatzTV.Application.Channels
|
||||
@using ErsatzTV.Application.Channels.Queries
|
||||
@inject NavigationManager NavigationManager
|
||||
@inject ILogger<ChannelEditor> Logger
|
||||
@inject ISnackbar Snackbar
|
||||
@inject IMediator Mediator
|
||||
|
||||
<div style="max-width: 400px;">
|
||||
<MudText Typo="Typo.h4" Class="mb-4">@(IsEdit ? "Edit Channel" : "Add Channel")</MudText>
|
||||
|
||||
<EditForm EditContext="_editContext" OnSubmit="@HandleSubmitAsync">
|
||||
<FluentValidator/>
|
||||
<MudCard>
|
||||
<MudCardContent>
|
||||
<MudTextField Label="Number" @bind-Value="_model.Number" For="@(() => _model.Number)" Immediate="true"/>
|
||||
<MudTextField Class="mt-3" Label="Name" @bind-Value="_model.Name" For="@(() => _model.Name)"/>
|
||||
<MudSelect Class="mt-3" Label="Streaming Mode" @bind-Value="_model.StreamingMode" For="@(() => _model.StreamingMode)">
|
||||
@foreach (StreamingMode streamingMode in Enum.GetValues<StreamingMode>())
|
||||
{
|
||||
<MudSelectItem Value="@streamingMode">@streamingMode</MudSelectItem>
|
||||
}
|
||||
</MudSelect>
|
||||
<MudSelect Class="mt-3" Label="FFmpeg Profile" @bind-Value="_model.FFmpegProfileId" For="@(() => _model.FFmpegProfileId)"
|
||||
Disabled="@(_model.StreamingMode != StreamingMode.TransportStream)">
|
||||
@foreach (FFmpegProfileViewModel profile in _ffmpegProfiles)
|
||||
{
|
||||
<MudSelectItem Value="@profile.Id">@profile.Name</MudSelectItem>
|
||||
}
|
||||
</MudSelect>
|
||||
<MudGrid Class="mt-3" Style="align-items: center" Justify="Justify.Center">
|
||||
<MudItem xs="6">
|
||||
<InputFile id="fileInput" OnChange="UploadLogo" hidden/>
|
||||
@if (!string.IsNullOrWhiteSpace(_model.Logo))
|
||||
{
|
||||
<MudElement HtmlTag="img" src="@($"iptv/images/{_model.Logo}")" Style="max-height: 50px"/>
|
||||
}
|
||||
</MudItem>
|
||||
<MudItem xs="6">
|
||||
<MudButton Class="ml-auto" HtmlTag="label"
|
||||
Variant="Variant.Filled"
|
||||
Color="Color.Primary"
|
||||
StartIcon="@Filled.CloudUpload"
|
||||
for="fileInput">
|
||||
Upload Logo
|
||||
</MudButton>
|
||||
</MudItem>
|
||||
</MudGrid>
|
||||
</MudCardContent>
|
||||
<MudCardActions>
|
||||
<MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary">
|
||||
@(IsEdit ? "Save Changes" : "Add Channel")
|
||||
</MudButton>
|
||||
</MudCardActions>
|
||||
</MudCard>
|
||||
</EditForm>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
|
||||
[Parameter]
|
||||
public int? Id { get; set; }
|
||||
|
||||
private readonly ChannelEditViewModel _model = new();
|
||||
private EditContext _editContext;
|
||||
private ValidationMessageStore _messageStore;
|
||||
|
||||
private List<FFmpegProfileViewModel> _ffmpegProfiles;
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
await LoadFFmpegProfilesAsync();
|
||||
|
||||
if (Id.HasValue)
|
||||
{
|
||||
Option<ChannelViewModel> maybeChannel = await Mediator.Send(new GetChannelById(Id.Value));
|
||||
maybeChannel.Match(
|
||||
channelViewModel =>
|
||||
{
|
||||
_model.Id = channelViewModel.Id;
|
||||
_model.Name = channelViewModel.Name;
|
||||
_model.Number = channelViewModel.Number;
|
||||
_model.FFmpegProfileId = channelViewModel.FFmpegProfileId;
|
||||
_model.Logo = channelViewModel.Logo;
|
||||
_model.StreamingMode = channelViewModel.StreamingMode;
|
||||
},
|
||||
() => NavigationManager.NavigateTo("404"));
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: command for new channel
|
||||
int maxNumber = await Mediator.Send(new GetAllChannels()).Map(channels => channels.Max(c => c.Number));
|
||||
_model.Number = maxNumber + 1;
|
||||
_model.Name = "New Channel";
|
||||
_model.FFmpegProfileId = _ffmpegProfiles.Head().Id;
|
||||
_model.StreamingMode = StreamingMode.TransportStream;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
_editContext = new EditContext(_model);
|
||||
_messageStore = new ValidationMessageStore(_editContext);
|
||||
}
|
||||
|
||||
private bool IsEdit => Id.HasValue;
|
||||
|
||||
private async Task LoadFFmpegProfilesAsync() =>
|
||||
_ffmpegProfiles = await Mediator.Send(new GetAllFFmpegProfiles());
|
||||
|
||||
private async Task HandleSubmitAsync()
|
||||
{
|
||||
_messageStore.Clear();
|
||||
if (_editContext.Validate())
|
||||
{
|
||||
Seq<BaseError> errorMessage = IsEdit ?
|
||||
(await Mediator.Send(_model.ToUpdate())).LeftToSeq() :
|
||||
(await Mediator.Send(_model.ToCreate())).LeftToSeq();
|
||||
|
||||
errorMessage.HeadOrNone().Match(
|
||||
error =>
|
||||
{
|
||||
Snackbar.Add($"Unexpected error saving channel: {error.Value}");
|
||||
Logger.LogError("Unexpected error saving channel: {Error}", error.Value);
|
||||
},
|
||||
() => NavigationManager.NavigateTo("/channels"));
|
||||
}
|
||||
}
|
||||
|
||||
private async Task UploadLogo(InputFileChangeEventArgs e)
|
||||
{
|
||||
var buffer = new byte[e.File.Size];
|
||||
await e.File.OpenReadStream().ReadAsync(buffer);
|
||||
Either<BaseError, string> maybeCacheFileName = await Mediator.Send(new SaveImageToDisk(buffer));
|
||||
maybeCacheFileName.Match(
|
||||
relativeFileName => _model.Logo = relativeFileName,
|
||||
error =>
|
||||
{
|
||||
Snackbar.Add($"Unexpected error saving channel logo: {error.Value}");
|
||||
Logger.LogError("Unexpected error saving channel logo: {Error}", error.Value);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user