@page "/media/libraries/local/{Id:int}/add" @using ErsatzTV.Application.Libraries @using ErsatzTV.Application.MediaSources @implements IDisposable @inject NavigationManager NavigationManager @inject ILogger Logger @inject ISnackbar Snackbar @inject IMediator Mediator @inject IEntityLocker Locker @inject ChannelWriter ScannerWorkerChannel @_library.Name - Add Local Library Path
@* TODO: replace this with a folder picker *@ Add Local Library Path
@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 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 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"); } }); } } }