Skip to content

Commit

Permalink
feat: Profile button
Browse files Browse the repository at this point in the history
  • Loading branch information
Gzozo committed Sep 26, 2024
1 parent a3ba9d1 commit ecf4c00
Show file tree
Hide file tree
Showing 7 changed files with 1,102 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="MudBlazor" Version="6.16.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
34 changes: 29 additions & 5 deletions grade-management-new/GradeManagement.Client/Layout/AppBar.razor
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
@using System.IdentityModel.Tokens.Jwt
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@inject SubjectService SubjectService
@inject NavigationManager Navigation
@inject IAccessTokenProvider TokenProvider

<MudAppBar Elevation="5" Class="pb-1">
<MudIconButton Icon="@(DrawerOpened ? Icons.Material.Filled.Close : Icons.Material.Filled.Menu)" Color="Color.Inherit" Edge="Edge.Start" OnClick="@((e) => DrawerToggle())"/>
<MudText Typo="Typo.h3" Class="ml-3">AHK Review UI</MudText>
<MudSpacer/>
<SelectSubject @bind-Value="@SubjectService.CurrentSubject" PossibleValues="@SubjectService.Subjects"/>
<MudIconButton Icon="@Icons.Material.Filled.Add" Color="Color.Inherit" Edge="Edge.End" Size="Size.Large" Href="/subjects"/>
@* <MudIconButton Icon="@Icons.Material.Filled.Add" Color="Color.Inherit" Edge="Edge.End" Size="Size.Large" Href="/subjects"/> *@

<!-- Profile Dropdown Menu -->
<MudMenu>
<MudMenu Class="ms-4">
<ActivatorContent>
<MudAvatar Size="Size.Medium" ImageUrl="@UserProfilePictureUrl"/>
<MudAvatar Size="Size.Medium">
<MudImage Src="@UserProfilePictureUrl"></MudImage>
</MudAvatar>
</ActivatorContent>
<ChildContent>
<MudMenuItem>
<MudMenuItem Disabled="true">
<MudText Typo="Typo.subtitle1">@UserName</MudText>
</MudMenuItem>
<MudMenuItem Disabled="true">
<MudText Typo="Typo.subtitle1">@EmailAddress</MudText>
</MudMenuItem>
<MudDivider/>
<MudMenuItem OnClick="@SignOut">
<MudText Typo="Typo.subtitle1">Sign Out</MudText>
</MudMenuItem>
Expand All @@ -26,16 +34,32 @@
</MudAppBar>

@code{
private string UserProfilePictureUrl { get; set; } = "https://via.placeholder.com/40"; // Default placeholder
private string UserName { get; set; } = "Loading...";
private string EmailAddress { get; set; } = "Loading...";
private string UserProfilePictureUrl { get; set; } = @"https://ui-avatars.com/api/?rounded=true&name=John+Doe"; // Default placeholder
[Parameter] public Action DrawerToggle { get; set; }
[Parameter] public bool DrawerOpened { get; set; }


private string ImageLinkGenerator(string name) => $"https://ui-avatars.com/api/?rounded=true&name={name}";

protected override async Task OnInitializedAsync()
{
await SubjectService.LoadSubjects();
SubjectService.OnChange += StateHasChanged;

var result = await TokenProvider.RequestAccessToken();
if (result.TryGetToken(out var token))
{
var handler = new JwtSecurityTokenHandler();
var jwtToken = handler.ReadJwtToken(token.Value);

// Extract user information from the token
UserName = jwtToken.Claims.FirstOrDefault(c => c.Type == "name")?.Value ?? "Unknown User";
EmailAddress = jwtToken.Claims.FirstOrDefault(c => c.Type == "email")?.Value ?? "Unknown Email";
UserProfilePictureUrl = ImageLinkGenerator(UserName);
StateHasChanged();
}
}

private void SignOut()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
// Call backend for user data
Navigation.NavigateTo("/");
}

if (Action == "logout-callback")
{
Navigation.NavigateTo("/");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
</TemplateColumn>
</Columns>
<PagerContent>
<MudDataGridPager T="Assignment"/>
<MudDataGridPager T="Dashboard"/>
</PagerContent>
</MudDataGrid>
</LoadingComponent>
Expand Down
Loading

0 comments on commit ecf4c00

Please sign in to comment.