Files
ersatztv/ErsatzTV/Pages/LocalLibraryPathEditor.razor
T
Jason DoveandGitHub 087901d177 adjust block unique constraint (#1634)
* upgrade dependencies

* allow blocks with same name in different groups

* code cleanup
2024-03-05 10:39:06 -06:00

91 lines
3.3 KiB
Plaintext

@page "/media/libraries/local/{Id:int}/add"
@using ErsatzTV.Application.Libraries
@using ErsatzTV.Application.MediaSources
@implements IDisposable
@inject NavigationManager _navigationManager
@inject ILogger<LocalLibraryPathEditor> _logger
@inject ISnackbar _snackbar
@inject IMediator _mediator
@inject IEntityLocker _locker
@inject ChannelWriter<IScannerBackgroundServiceRequest> _scannerWorkerChannel;
<MudContainer MaxWidth="MaxWidth.ExtraLarge" Class="pt-8">
<MudText Typo="Typo.h4" Class="mb-4">@_library.Name - Add Local Library Path</MudText>
<div style="max-width: 400px;">
<EditForm EditContext="_editContext" OnSubmit="@HandleSubmitAsync">
<FluentValidationValidator/>
<MudCard>
<MudCardContent>
<MudTextField T="string" Label="Media Kind" Disabled="true" Value="@(Enum.GetName(typeof(LibraryMediaKind), _library.MediaKind))"/>
@* TODO: replace this with a folder picker *@
<MudTextField Label="Path" @bind-Value="_model.Path" For="@(() => _model.Path)"/>
</MudCardContent>
<MudCardActions>
<MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary" Class="ml-auto">
Add Local Library Path
</MudButton>
</MudCardActions>
</MudCard>
</EditForm>
</div>
</MudContainer>
@code {
private readonly CancellationTokenSource _cts = new();
[Parameter]
public int Id { get; set; }
private readonly LocalLibraryPathEditViewModel _model = new();
private EditContext _editContext;
private ValidationMessageStore _messageStore;
private LocalLibraryViewModel _library;
public void Dispose()
{
_cts.Cancel();
_cts.Dispose();
}
protected override async Task OnParametersSetAsync()
{
Option<LocalLibraryViewModel> maybeLibrary = await _mediator.Send(new GetLocalLibraryById(Id), _cts.Token);
maybeLibrary.Match(
library => _library = library,
() => _navigationManager.NavigateTo("404"));
}
protected override void OnInitialized()
{
_editContext = new EditContext(_model);
_messageStore = new ValidationMessageStore(_editContext);
}
private async Task HandleSubmitAsync()
{
_messageStore.Clear();
if (_editContext.Validate())
{
var command = new CreateLocalLibraryPath(_library.Id, _model.Path);
Either<BaseError, LocalLibraryPathViewModel> result = await _mediator.Send(command, _cts.Token);
await result.Match(
Left: error =>
{
_snackbar.Add(error.Value, Severity.Error);
_logger.LogError("Unexpected error saving local library path: {Error}", error.Value);
return Task.CompletedTask;
},
Right: async _ =>
{
if (_locker.LockLibrary(_library.Id))
{
await _scannerWorkerChannel.WriteAsync(new ScanLocalLibraryIfNeeded(_library.Id), _cts.Token);
_navigationManager.NavigateTo("media/libraries");
}
});
}
}
}