@using Microsoft.Extensions.Caching.Memory @using ErsatzTV.Application.MediaCollections @implements IDisposable @inject IMediator _mediator @inject IMemoryCache _memoryCache @inject ISnackbar _snackbar @inject ILogger _logger @foreach (SmartCollectionViewModel collection in _collections) { @collection.Name } Cancel Save As Smart Collection @code { private readonly CancellationTokenSource _cts = new(); [CascadingParameter] MudDialogInstance MudDialog { get; set; } private readonly SmartCollectionViewModel _newCollection = new(-1, "(New Collection)", string.Empty); private string _newCollectionName; private List _collections; private SmartCollectionViewModel _selectedCollection; private record DummyModel; private readonly DummyModel _dummyModel = new(); public void Dispose() { _cts.Cancel(); _cts.Dispose(); } private bool CanSubmit() => _selectedCollection != null && (_selectedCollection != _newCollection || !string.IsNullOrWhiteSpace(_newCollectionName)); protected override async Task OnParametersSetAsync() { _collections = await _mediator.Send(new GetAllSmartCollections(), _cts.Token) .Map(list => new[] { _newCollection }.Append(list.OrderBy(vm => vm.Name, StringComparer.CurrentCultureIgnoreCase)).ToList()); if (_memoryCache.TryGetValue("SaveAsSmartCollectionDialog.SelectedCollectionId", out int id)) { _selectedCollection = _collections.SingleOrDefault(c => c.Id == id) ?? _newCollection; } else { _selectedCollection = _newCollection; } } private async Task Submit() { if (!CanSubmit()) { return; } if (_selectedCollection == _newCollection) { Either maybeResult = await _mediator.Send(new CreateSmartCollection(string.Empty, _newCollectionName), _cts.Token); maybeResult.Match( collection => { _memoryCache.Set("SaveAsSmartCollectionDialog.SelectedCollectionId", collection.Id); MudDialog.Close(DialogResult.Ok(collection)); }, error => { _snackbar.Add(error.Value, Severity.Error); _logger.LogError("Error creating new collection: {Error}", error.Value); MudDialog.Close(DialogResult.Cancel()); }); } else { _memoryCache.Set("SaveAsSmartCollectionDialog.SelectedCollectionId", _selectedCollection.Id); MudDialog.Close(DialogResult.Ok(_selectedCollection)); } } 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(); } } }