@page "/media/libraries/local/{Id:int}/add" @using ErsatzTV.Application.Libraries @using ErsatzTV.Application.Libraries.Commands @using ErsatzTV.Application.Libraries.Queries @using ErsatzTV.Application.MediaSources.Commands @inject NavigationManager _navigationManager @inject ILogger _logger @inject ISnackbar _snackbar @inject IMediator _mediator @inject IEntityLocker _locker @inject ChannelWriter _channel @_library.Name - Add Local Library Path
@* TODO: replace this with a folder picker *@ Add Local Library Path
@code { [Parameter] public int Id { get; set; } private readonly LocalLibraryPathEditViewModel _model = new(); private EditContext _editContext; private ValidationMessageStore _messageStore; private LocalLibraryViewModel _library; protected override async Task OnParametersSetAsync() { Option maybeLibrary = await _mediator.Send(new GetLocalLibraryById(Id)); 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); 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 _channel.WriteAsync(new ScanLocalLibraryIfNeeded(_library.Id)); _navigationManager.NavigateTo("/media/libraries"); } }); } } }