117 lines
3.9 KiB
Plaintext
117 lines
3.9 KiB
Plaintext
@using System.Net
|
|
@using Fluxor
|
|
@using Koogle.Application.DTOs
|
|
@using Koogle.Web.Store.AuthState
|
|
@using Koogle.Application.Interfaces
|
|
|
|
@inject IState<AuthState> AuthState
|
|
@inject NavigationManager NavigationManager
|
|
@inject IUserService UserService
|
|
@inject HttpClient HttpClient;
|
|
@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Antiforgery
|
|
@inject IHttpContextAccessor HttpContextAccessor
|
|
@inject IDispatcher Dispatcher
|
|
|
|
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
|
|
|
|
@if (AuthState.Value.IsAuthenticated && AuthState.Value.CurrentClub != null)
|
|
{
|
|
@if (AuthState.Value.AvailableClubs.Count > 1)
|
|
{
|
|
<MudMenu AnchorOrigin="Origin.BottomLeft" TransformOrigin="Origin.TopLeft">
|
|
<ActivatorContent>
|
|
<MudButton Variant="Variant.Text" Color="Color.Inherit" EndIcon="@Icons.Material.Filled.ArrowDropDown">
|
|
<MudText Typo="Typo.caption">@AuthState.Value.CurrentClub.ClubName</MudText>
|
|
</MudButton>
|
|
</ActivatorContent>
|
|
<ChildContent>
|
|
@foreach (var club in AuthState.Value.AvailableClubs)
|
|
{
|
|
<MudMenuItem OnClick="@(() => SwitchClubAsync(club.ClubId))"
|
|
Disabled="@(club.ClubId == AuthState.Value.CurrentClub?.ClubId)">
|
|
@if (club.ClubId == AuthState.Value.CurrentClub?.ClubId)
|
|
{
|
|
<MudIcon Icon="@Icons.Material.Filled.Check" Size="Size.Small" Class="mr-2"/>
|
|
}
|
|
@club.ClubName
|
|
</MudMenuItem>
|
|
}
|
|
</ChildContent>
|
|
</MudMenu>
|
|
}
|
|
else
|
|
{
|
|
<MudText Typo="Typo.caption">Club: @AuthState.Value.CurrentClub.ClubName</MudText>
|
|
}
|
|
}
|
|
else if (AuthState.Value.IsAuthenticated && AuthState.Value.HasNoClub)
|
|
{
|
|
<MudButton Variant="Variant.Text" Color="Color.Warning" Href="/account/club-setup">
|
|
<MudIcon Icon="@Icons.Material.Filled.Warning" Size="Size.Small" Class="mr-1" />
|
|
<MudText Typo="Typo.caption">Kein Club</MudText>
|
|
</MudButton>
|
|
}
|
|
|
|
@code {
|
|
private string _antiToken;
|
|
|
|
protected override void OnAfterRender(bool firstRender)
|
|
{
|
|
if (firstRender)
|
|
{
|
|
var http = HttpContextAccessor.HttpContext!;
|
|
var tokens = Antiforgery.GetAndStoreTokens(http);
|
|
_antiToken = tokens.RequestToken!;
|
|
}
|
|
}
|
|
|
|
private async Task SwitchClubAsync(Guid clubId)
|
|
{
|
|
if (AuthState.Value.CurrentUser == null)
|
|
return;
|
|
|
|
var model = new SwitchClubFormDto()
|
|
{
|
|
ClubId = clubId,
|
|
UserProfileId = AuthState.Value.CurrentUser.ProfileId
|
|
};
|
|
|
|
try
|
|
{
|
|
HttpClient.DefaultRequestHeaders.Remove("RequestVerificationToken");
|
|
HttpClient.DefaultRequestHeaders.Add(
|
|
"RequestVerificationToken",
|
|
_antiToken
|
|
);
|
|
|
|
var basepath = NavigationManager.BaseUri;
|
|
var url = $"{basepath}auth/switch-club";
|
|
await HttpClient.PostAsJsonAsync(url, model);
|
|
|
|
// Dispatcher.Dispatch(new AuthState.InitializeAuthSuccessAction(model.UserProfileId, model.ClubId, roles));
|
|
|
|
|
|
NavigationManager.NavigateTo("/dashboard", forceLoad: true);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
throw;
|
|
}
|
|
|
|
// try
|
|
// {
|
|
// var success = await UserService.SwitchClubAsync(AuthState.Value.CurrentUser.ProfileId, clubId);
|
|
// if (success)
|
|
// {
|
|
// // Force page reload to refresh claims
|
|
// NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true);
|
|
// }
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// Console.WriteLine($"Club switch failed: {ex.Message}");
|
|
// }
|
|
}
|
|
}
|