LANCommander/LANCommander.Launcher/UI/Components/ProfileButton.razor

92 lines
No EOL
2.5 KiB
Text

@using LANCommander.Launcher.Data.Models
@using LANCommander.Launcher.Models
@inject UserService UserService
@inject ProfileService ProfileService
@inject AuthenticationService AuthenticationService
@inject NavigationManager NavigationManager
@inject ModalService ModalService
<Dropdown>
<Overlay>
<Menu>
<MenuItem OnClick="ChangeAlias" Disabled="ConnectionState.OfflineModeEnabled">
Change Name
</MenuItem>
<MenuItem OnClick="@(() => NavigationManager.NavigateTo("/Settings"))">
Settings
</MenuItem>
@if (!ConnectionState.OfflineModeEnabled)
{
<MenuItem OnClick="Logout">
Logout
</MenuItem>
}
</Menu>
</Overlay>
<ChildContent>
<Button Type="ButtonType.Primary" Class="appbar-profile-button">
@if (AvatarId != Guid.Empty)
{
<MediaImage Id="AvatarId" />
}
<span>@Alias</span>
</Button>
</ChildContent>
</Dropdown>
@code {
[CascadingParameter] public ConnectionState ConnectionState { get; set; }
Models.Settings Settings = null;
User User;
Guid AvatarId = Guid.Empty;
string Alias = "Player";
protected override async Task OnInitializedAsync()
{
ProfileService.OnProfileDownloaded += async (sender, args) =>
{
User = await UserService.GetCurrentUser();
AvatarId = User?.Avatar?.Id ?? Guid.Empty;
Alias = String.IsNullOrWhiteSpace(User?.Alias) ? User?.UserName ?? "Player" : User.Alias;
};
await ProfileService.DownloadProfileInfoAsync();
}
async Task ChangeAlias()
{
var modalOptions = new ModalOptions()
{
Title = "Change Name",
Maximizable = false,
DefaultMaximized = false,
Closable = true,
Centered = true
};
var modalRef = ModalService.CreateModal<ChangeAliasDialog, string, string>(modalOptions, User.Alias);
modalRef.OnOk = async (newName) =>
{
await ProfileService.ChangeAlias(newName);
await ProfileService.DownloadProfileInfoAsync();
Settings = SettingService.GetSettings();
await InvokeAsync(StateHasChanged);
};
}
async Task Logout()
{
await AuthenticationService.Logout();
NavigationManager.NavigateTo("/Authenticate");
}
}