LANCommander/LANCommander.Server/UI/Pages/Profile/Saves.razor

121 lines
4 KiB
Text

@page "/Profile/Saves"
@using Microsoft.AspNetCore.Components.Authorization;
@using Microsoft.EntityFrameworkCore;
@inject UserService UserService
@inject GameSaveService GameSaveService
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject IJSRuntime JSRuntime
@attribute [Authorize]
<PageHeader Title="Game Saves" />
<DataTable
@ref="Table"
TItem="GameSave"
Query="gs => gs.Game != null && (GameId == Guid.Empty || gs.Game.Id == GameId)"
Searchable
SearchProperty="c => c.Game.Title">
<RightToolbar>
<Select TItem="Game"
TItemValue="Guid"
AutoFocus="true"
DataSource="@Games.OrderBy(g => String.IsNullOrWhiteSpace(g.SortTitle) ? g.Title : g.SortTitle)"
@bind-Value="@GameId"
LabelName="Title"
ValueName="Id"
Placeholder="Select a Game"
DefaultActiveFirstOption="false"
EnableSearch
OnSelectedItemChanged="() => Table.Reload()"
Style="min-width: 250px;">
<ItemTemplate Context="game">
<Image Src="@GetIcon(game)" Height="32" Width="32" Preview="false"></Image>
@game.Title
</ItemTemplate>
</Select>
</RightToolbar>
<Columns>
<BoundDataColumn Property="s => s.Game.Title" Sortable Title="Game" Include="Game" />
<BoundDataColumn Property="s => s.CreatedOn" Format="MM/dd/yyyy hh:mm tt" Sortable Title="Created On" DefaultSortOrder="SortDirection.Descending" />
<BoundDataColumn Property="s => s.Size" Sortable Title="Size">
<ByteSize Value="context.Size" />
</BoundDataColumn>
<DataActions TData="string">
<a href="/Download/Save/@(context.Id)" target="_blank" class="ant-btn ant-btn-text ant-btn-icon-only">
<Icon Type="@IconType.Outline.Download"/>
</a>
<Popconfirm OnConfirm="() => Delete(context)" Title="Are you sure you want to delete this game save?">
<Button Icon="@IconType.Outline.Close" Type="@ButtonType.Text" Danger/>
</Popconfirm>
</DataActions>
</Columns>
</DataTable>
@code {
DataTable<GameSave> Table;
User User = new User();
ICollection<GameSave> GameSaves = new List<GameSave>();
IEnumerable<Game> Games = new List<Game>();
bool Loading = true;
Guid GameId = Guid.Empty;
protected override async Task OnInitializedAsync()
{
await LoadData();
}
async Task LoadData() {
Loading = true;
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (authState.User.Identity.IsAuthenticated)
User = await UserService
.GetAsync(authState.User.Identity.Name);
GameSaves = (await GameSaveService
.Query(q =>
{
return q
.Include(gs => gs.Game)
.ThenInclude(g => g.Media);
})
.SortBy(gs => gs.Game.Title)
.GetAsync(gs => gs.UserId == User.Id)).OrderByDescending(gs => gs.UpdatedOn).ToList();
if (GameSaves != null && GameSaves.Count > 0)
Games = GameSaves.Where(gs => gs.Game != null).Select(gs => gs.Game).DistinctBy(g => g.Id).ToList();
Loading = false;
}
async Task Download(Guid id)
{
await JSRuntime.InvokeAsync<object>("open", $"/Saves/Download/{id}", "_blank");
}
async Task Delete(GameSave gameSave)
{
GameSaves = new List<GameSave>();
Loading = true;
await GameSaveService.DeleteAsync(gameSave);
await LoadData();
Loading = false;
}
string GetIcon(Game game)
{
var media = game?.Media?.FirstOrDefault(m => m.Type == SDK.Enums.MediaType.Icon);
if (media != null)
return $"/api/Media/{media.Id}/Download?fileId={media.FileId}";
else
return "/favicon.ico";
}
}