LANCommander/LANCommander.Server/UI/Pages/Issues/Edit.razor

180 lines
6.4 KiB
Text

@page "/Issues/{id:guid}"
@page "/Issues/Add"
@using Microsoft.AspNetCore.Components.Authorization
@attribute [Authorize(Roles = RoleService.AdministratorRoleName)]
@inject GameService GameService
@inject IssueService IssueService
@inject UserService UserService
@inject IMessageService MessageService
@inject NavigationManager NavigationManager
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject ILogger<Edit> Logger
<Layout Class="panel-layout">
<Content>
<PageHeader>
<PageHeaderTitle>
@if (Id == null || Id == Guid.Empty)
{
<Text>Add New Issue</Text>
}
else
{
<Text>Edit Issue</Text>
}
</PageHeaderTitle>
<PageHeaderExtra>
<Flex Gap="FlexGap.Small" Justify="FlexJustify.End">
@if (Id != Guid.Empty)
{
<Button OnClick="Resolve" Disabled="@(Issue?.ResolvedOn != null)">Resolve</Button>
}
<Button Type="@ButtonType.Primary" OnClick="Save" Disabled="@(Issue?.ResolvedOn != null)">Save</Button>
</Flex>
</PageHeaderExtra>
</PageHeader>
<div class="panel-layout-content">
@if (Issue?.ResolvedOn != null)
{
<Alert Type="@AlertType.Success" Message="@($"This issue was resolved on {Issue.ResolvedOn} by {Issue?.ResolvedBy?.UserName} and was reported by {Issue?.CreatedBy?.UserName} on {Issue.CreatedOn}")" ShowIcon="true" Style="margin-bottom: 16px" />
}
else if (Id != Guid.Empty)
{
<Alert Type="@AlertType.Info" Message="@($"This issue was reported by {Issue?.CreatedBy?.UserName} on {Issue.CreatedOn}")" ShowIcon="true" Style="margin-bottom: 16px" />
}
<Form Model="@Issue" Layout="@FormLayout.Vertical">
<FormItem Label="Game">
<GridRow Wrap="false" Gutter="16">
<GridCol Flex=@("auto")>
<Select TItem="Game"
TItemValue="Guid"
DataSource="@Games?.OrderBy(g => String.IsNullOrWhiteSpace(g.SortTitle) ? g.Title : g.SortTitle)"
@bind-Value="@context.GameId"
LabelName="Title"
ValueName="Id"
Placeholder="Select a Game"
DefaultActiveFirstOption="false"
EnableSearch
Disabled="context?.ResolvedOn != null">
<ItemTemplate Context="game">
<Image Src="@GetIcon(game)" Height="32" Width="32" Preview="false"></Image>
@game.Title
</ItemTemplate>
</Select>
</GridCol>
<GridCol Flex=@("none")>
@if (context.Game != null && context.GameId != Guid.Empty)
{
<a href="/Games/@(context.Game?.Id)" target="_blank" class="ant-btn ant-btn-primary">Edit</a>
}
else
{
<Button Type="@ButtonType.Primary" Disabled="true">Edit</Button>
}
</GridCol>
</GridRow>
</FormItem>
<FormItem Label="Description">
<TextArea AutoFocus @bind-Value="@context.Description" ShowCount MinRows="30" Disabled="context?.ResolvedOn != null" />
</FormItem>
</Form>
</div>
</Content>
</Layout>
@code {
[Parameter] public Guid Id { get; set; }
Issue Issue = new Issue();
IEnumerable<Game> Games = new List<Game>();
protected override async Task OnInitializedAsync()
{
await LoadData();
}
private async Task LoadData()
{
if (Id == Guid.Empty)
Issue = new Issue();
else
Issue = await IssueService
.Include(
i => i.Game,
i => i.CreatedBy,
i => i.UpdatedBy,
i => i.ResolvedBy
).GetAsync(Id);
Games = await GameService.Include(g => g.Media.Where(m => m.Type == SDK.Enums.MediaType.Icon)).GetAsync();
}
private async Task Save()
{
try
{
if (Issue.Id != Guid.Empty)
{
Issue = await IssueService.UpdateAsync(Issue);
MessageService.Success("Issue updated!");
}
else
{
Issue = await IssueService.AddAsync(Issue);
NavigationManager.LocationChanged += NotifyIssueAdded;
NavigationManager.NavigateTo($"/Issues/{Issue.Id}");
}
}
catch (Exception ex)
{
MessageService.Error("Could not save issue!");
Logger?.LogError(ex, "Could not save issue!");
}
}
private async Task Resolve()
{
try
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = await UserService.GetAsync(authState.User.Identity.Name);
await IssueService.ResolveAsync(Issue.Id, user.Id);
Issue = await IssueService.GetAsync(Issue.Id);
MessageService.Success("Issue resolved!");
await LoadData();
}
catch (Exception ex)
{
MessageService.Error("Could not resolve issue!");
Logger?.LogError(ex, "Could not resolve issue!");
}
}
private 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";
}
private void NotifyIssueAdded(object? sender, LocationChangedEventArgs e)
{
NavigationManager.LocationChanged -= NotifyIssueAdded;
MessageService.Success("Issue added!");
}
}