mirror of https://github.com/ErsatzTV/ErsatzTV.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
4.4 KiB
129 lines
4.4 KiB
@using ErsatzTV.Application.MediaCollections |
|
@using Microsoft.Extensions.Caching.Memory |
|
@implements IDisposable |
|
@inject IMediator Mediator |
|
@inject IMemoryCache MemoryCache |
|
@inject ISnackbar Snackbar |
|
@inject ILogger<SaveAsSmartCollectionDialog> Logger |
|
|
|
<MudDialog> |
|
<DialogContent> |
|
<EditForm Model="@_dummyModel" OnSubmit="@(_ => Submit())"> |
|
<MudContainer Class="mb-6"> |
|
<MudText Class="mud-primary-text" |
|
Style="background-color: transparent; font-weight: bold"> |
|
Select the desired smart collection |
|
</MudText> |
|
</MudContainer> |
|
<MudSelect Label="Collection" @bind-Value="_selectedCollection" Class="mb-6 mx-4"> |
|
@foreach (SmartCollectionViewModel collection in _collections) |
|
{ |
|
<MudSelectItem Value="@collection">@collection.Name</MudSelectItem> |
|
} |
|
</MudSelect> |
|
<MudTextField T="string" Label="New Collection Name" |
|
Disabled="@(_selectedCollection != _newCollection)" |
|
@bind-Text="@_newCollectionName" |
|
Class="mb-6 mx-4"> |
|
</MudTextField> |
|
</EditForm> |
|
</DialogContent> |
|
<DialogActions> |
|
<MudButton OnClick="Cancel" ButtonType="ButtonType.Reset">Cancel</MudButton> |
|
<MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="Submit"> |
|
Save As Smart Collection |
|
</MudButton> |
|
</DialogActions> |
|
</MudDialog> |
|
|
|
|
|
@code { |
|
private readonly CancellationTokenSource _cts = new(); |
|
|
|
[CascadingParameter] |
|
IMudDialogInstance MudDialog { get; set; } |
|
|
|
[Parameter] |
|
public string Query { get; set; } |
|
|
|
private readonly SmartCollectionViewModel _newCollection = new(-1, "(New Collection)", string.Empty); |
|
private string _newCollectionName; |
|
|
|
private List<SmartCollectionViewModel> _collections = new(); |
|
|
|
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<BaseError, SmartCollectionViewModel> maybeResult = |
|
await Mediator.Send(new CreateSmartCollection(Query, _newCollectionName), _cts.Token); |
|
|
|
maybeResult.Match( |
|
collection => |
|
{ |
|
MemoryCache.Set("SaveAsSmartCollectionDialog.SelectedCollectionId", collection.Id); |
|
MudDialog.Close(DialogResult.Ok(new Tuple<SmartCollectionViewModel, bool>(collection, false))); |
|
}, |
|
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(new Tuple<SmartCollectionViewModel, bool>(_selectedCollection, true))); |
|
} |
|
} |
|
|
|
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(); |
|
} |
|
} |
|
|
|
} |