mirror of https://github.com/ErsatzTV/ErsatzTV.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
6.5 KiB
165 lines
6.5 KiB
@using ErsatzTV.Application.MediaSources |
|
@implements IDisposable |
|
@inject NavigationManager NavigationManager |
|
@inject ILogger<RemoteMediaSourcePathReplacementsEditor> Logger |
|
@inject ISnackbar Snackbar |
|
@inject IMediator Mediator |
|
|
|
<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" OnClick="@(_ => SaveChanges())" StartIcon="@Icons.Material.Filled.Save" Class="ml-8"> |
|
Save Path Replacements |
|
</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">@_source?.Name</MudText> |
|
<MudDivider Class="mb-6"/> |
|
<MudTable Hover="true" Items="_pathReplacements.OrderBy(r => r.Id)" Dense="true" @bind-SelectedItem="_selectedItem"> |
|
<ColGroup> |
|
<MudHidden Breakpoint="Breakpoint.Xs"> |
|
<col/> |
|
<col/> |
|
<col style="width: 60px;"/> |
|
</MudHidden> |
|
</ColGroup> |
|
<HeaderContent> |
|
<MudTh>@Name Path</MudTh> |
|
<MudTh>Local Path</MudTh> |
|
<MudTh/> |
|
</HeaderContent> |
|
<RowTemplate> |
|
<MudTd DataLabel="@($"{Name} Path")"> |
|
<MudText Typo="@(context == _selectedItem ? Typo.subtitle2 : Typo.body2)"> |
|
@context.RemotePath |
|
</MudText> |
|
</MudTd> |
|
<MudTd DataLabel="Local Path"> |
|
<MudText Typo="@(context == _selectedItem ? Typo.subtitle2 : Typo.body2)"> |
|
@context.LocalPath |
|
</MudText> |
|
</MudTd> |
|
<MudTd> |
|
<MudTooltip Text="Delete Path Replacement"> |
|
<MudIconButton Icon="@Icons.Material.Filled.Delete" |
|
OnClick="@(_ => RemovePathReplacement(context))"> |
|
</MudIconButton> |
|
</MudTooltip> |
|
</MudTd> |
|
</RowTemplate> |
|
</MudTable> |
|
<MudButton Variant="Variant.Filled" Color="Color.Default" OnClick="@(_ => AddPathReplacement())" Class="mt-4"> |
|
Add Path Replacement |
|
</MudButton> |
|
<MudText Typo="Typo.h5" Class="mt-10 mb-2">Path Replacement</MudText> |
|
<MudDivider Class="mb-6"/> |
|
@if (_selectedItem is not null) |
|
{ |
|
<MudStack Row="true" Breakpoint="Breakpoint.SmAndDown" Class="form-field-stack gap-md-8 mb-5"> |
|
<div class="d-flex"> |
|
<MudText>@($"{Name} Path")</MudText> |
|
</div> |
|
<MudTextField @bind-Value="@_selectedItem.RemotePath" |
|
For="@(() => _selectedItem.RemotePath)" |
|
Required="true" |
|
RequiredError="Remote path is required!"/> |
|
</MudStack> |
|
<MudStack Row="true" Breakpoint="Breakpoint.SmAndDown" Class="form-field-stack gap-md-8 mb-5"> |
|
<div class="d-flex"> |
|
<MudText>Local Path</MudText> |
|
</div> |
|
<MudTextField @bind-Value="@_selectedItem.LocalPath" |
|
For="@(() => _selectedItem.LocalPath)" |
|
Required="true" |
|
RequiredError="Local path is required!"/> |
|
</MudStack> |
|
} |
|
</MudContainer> |
|
</div> |
|
</MudForm> |
|
|
|
@code { |
|
private CancellationTokenSource _cts; |
|
|
|
[Parameter] |
|
public int Id { get; set; } |
|
|
|
[Parameter] |
|
public string Name { get; set; } |
|
|
|
[Parameter] |
|
public Func<int, CancellationToken, Task<Option<RemoteMediaSourceViewModel>>> GetMediaSourceById { get; set; } |
|
|
|
[Parameter] |
|
public Func<int, CancellationToken, Task<List<RemoteMediaSourcePathReplacementEditViewModel>>> GetPathReplacementsBySourceId { get; set; } |
|
|
|
[Parameter] |
|
public Func<List<RemoteMediaSourcePathReplacementEditViewModel>, IRequest<Either<BaseError, Unit>>> GetUpdatePathReplacementsRequest { get; set; } |
|
|
|
private RemoteMediaSourceViewModel _source; |
|
private List<RemoteMediaSourcePathReplacementEditViewModel> _pathReplacements = new(); |
|
|
|
private RemoteMediaSourcePathReplacementEditViewModel _selectedItem; |
|
|
|
public void Dispose() |
|
{ |
|
_cts?.Cancel(); |
|
_cts?.Dispose(); |
|
} |
|
|
|
protected override async Task OnParametersSetAsync() |
|
{ |
|
_cts?.Cancel(); |
|
_cts?.Dispose(); |
|
_cts = new CancellationTokenSource(); |
|
var token = _cts.Token; |
|
|
|
try |
|
{ |
|
Option<RemoteMediaSourceViewModel> maybeSource = await GetMediaSourceById(Id, token); |
|
await maybeSource.Match( |
|
async source => |
|
{ |
|
_source = source; |
|
_pathReplacements = await GetPathReplacementsBySourceId(Id, token); |
|
}, |
|
() => |
|
{ |
|
NavigationManager.NavigateTo("404"); |
|
return Task.CompletedTask; |
|
}); |
|
} |
|
catch (OperationCanceledException) |
|
{ |
|
// do nothing |
|
} |
|
} |
|
|
|
private void AddPathReplacement() |
|
{ |
|
var item = new RemoteMediaSourcePathReplacementEditViewModel(); |
|
_pathReplacements.Add(item); |
|
_selectedItem = item; |
|
} |
|
|
|
private void RemovePathReplacement(RemoteMediaSourcePathReplacementEditViewModel item) |
|
{ |
|
_selectedItem = null; |
|
_pathReplacements.Remove(item); |
|
} |
|
|
|
private async Task SaveChanges() |
|
{ |
|
Seq<BaseError> errorMessages = await Mediator.Send(GetUpdatePathReplacementsRequest(_pathReplacements), _cts.Token) |
|
.Map(e => e.LeftToSeq()); |
|
|
|
errorMessages.HeadOrNone().Match( |
|
error => |
|
{ |
|
Snackbar.Add($"Unexpected error saving path replacements: {error.Value}", Severity.Error); |
|
Logger.LogError("Unexpected error saving path replacements: {Error}", error.Value); |
|
}, |
|
() => NavigationManager.NavigateTo($"media/sources/{Name.ToLowerInvariant()}")); |
|
} |
|
|
|
}
|
|
|