@using Microsoft.Extensions.Caching.Memory @using ErsatzTV.Application.Libraries @using ErsatzTV.Application.Libraries.Commands @using ErsatzTV.Application.Libraries.Queries @inject IMediator _mediator @inject IMemoryCache _memoryCache @inject ISnackbar _snackbar @inject ILogger _logger Select the destination library @foreach (LocalLibraryViewModel library in _libraries) { @library.Name } Cancel Move To Library @code { [CascadingParameter] MudDialogInstance MudDialog { get; set; } [Parameter] public LibraryMediaKind MediaKind { get; set; } [Parameter] public int SourceLibraryId { get; set; } private LocalLibraryViewModel _newLibrary; private string _newLibraryName; private List _libraries; private LocalLibraryViewModel _selectedLibrary; private record DummyModel; private readonly DummyModel _dummyModel = new(); private bool CanSubmit() => _selectedLibrary != null && (_selectedLibrary != _newLibrary || !string.IsNullOrWhiteSpace(_newLibraryName)); protected override async Task OnParametersSetAsync() { _newLibrary = new LocalLibraryViewModel(-1, "(New Library)", MediaKind); _libraries = await _mediator.Send(new GetAllLocalLibraries()) .Map(list => list.Filter(ll => ll.MediaKind == MediaKind && ll.Id != SourceLibraryId)) .Map(list => new[] { _newLibrary }.Append(list.OrderBy(vm => vm.Name, StringComparer.CurrentCultureIgnoreCase)).ToList()); if (_memoryCache.TryGetValue("MoveLocalLibraryPathDialog.SelectedLibraryId", out int id)) { _selectedLibrary = _libraries.SingleOrDefault(c => c.Id == id) ?? _newLibrary; } else { _selectedLibrary = _newLibrary; } } private async Task Submit() { if (!CanSubmit()) { return; } if (_selectedLibrary == _newLibrary) { Either maybeResult = await _mediator.Send(new CreateLocalLibrary(_newLibraryName, MediaKind, new List())); maybeResult.Match( collection => { _memoryCache.Set("MoveLocalLibraryPathDialog.SelectedLibraryId", collection.Id); MudDialog.Close(DialogResult.Ok(collection)); }, error => { _snackbar.Add(error.Value, Severity.Error); _logger.LogError("Error creating new local library: {Error}", error.Value); MudDialog.Close(DialogResult.Cancel()); }); } else { _memoryCache.Set("MoveLocalLibraryPathDialog.SelectedLibraryId", _selectedLibrary.Id); MudDialog.Close(DialogResult.Ok(_selectedLibrary)); } } private async Task Cancel(MouseEventArgs e) { // this is gross, but [enter] seems to sometimes trigger cancel instead of submit if (e.Detail == 0) { await Submit(); } else { MudDialog.Cancel(); } } }