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

68 lines
2 KiB
Text

@page "/Profile"
@page "/Profile/General"
@using Microsoft.AspNetCore.Components.Authorization
@inject UserService UserService
@inject IMessageService MessageService
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager NavigationManager
@inject ILogger<Index> Logger
@attribute [Authorize]
<PageHeader Title="Profile">
<PageHeaderExtra>
<Button Type="@ButtonType.Primary" Disabled="!Form.IsModified" OnClick="Save">Save</Button>
</PageHeaderExtra>
</PageHeader>
<div style="padding: 0 24px;">
<Form @ref="Form" Model="User" Layout="@FormLayout.Vertical" ValidateOnChange="true">
<FormItem Label="Avatar">
<AvatarUploader UserId="User.Id" />
</FormItem>
<FormItem Label="Username" Help="Username changes require a relog">
<Input @bind-Value="context.UserName" />
</FormItem>
<FormItem Label="Alias">
<Input @bind-Value="context.Alias" />
</FormItem>
<FormItem Label="Email Address" Help="Email changes require a relog">
<Input @bind-Value="context.Email" />
</FormItem>
</Form>
</div>
@code {
User User = new();
Form<User> Form;
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (authState.User.Identity.IsAuthenticated)
User = await UserService.GetAsync(authState.User.Identity.Name);
}
private async Task Save()
{
try
{
if (Form.IsModified)
{
await UserService.UpdateAsync(User);
MessageService.Success("Profile updated!");
NavigationManager.NavigateTo("/Logout?force=true", true);
}
}
catch (Exception ex)
{
MessageService.Error("An unknown error occurred.");
Logger.LogError(ex, "An unknown error occurred.");
}
}
}