Skip to content

Commit

Permalink
(#433) wasm: update post razor components params
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed Oct 13, 2024
1 parent 35550ac commit 9ade62d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 22 deletions.
38 changes: 26 additions & 12 deletions MiniSpace.Web/src/Astravent.Web.Wasm/Pages/Feeds/Home.razor
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
<MudItem xs="12">
<PostItem
Post="@post"
Username="@GetCachedUserName(post.UserId)"
FirstName="@GetCachedUserFirstName(post.UserId)"
LastName="@GetCachedUserLastName(post.UserId)"
UserProfileImage="@GetCachedUserAvatar(post.UserId)"
PostDate="@post.CreatedAt.ToString("g")"
ReactionsSummaries="@reactionsSummaries"
Expand Down Expand Up @@ -83,7 +84,8 @@
private Dictionary<Guid, ReactionsSummaryDto> reactionsSummaries = new Dictionary<Guid, ReactionsSummaryDto>();

// Cache for user names and avatars
private Dictionary<Guid, string> userNames = new Dictionary<Guid, string>();
private Dictionary<Guid, string> userFirstNames = new Dictionary<Guid, string>();
private Dictionary<Guid, string> userLastNames = new Dictionary<Guid, string>();
private Dictionary<Guid, string> userAvatars = new Dictionary<Guid, string>();

private List<BreadcrumbItem> _items = new List<BreadcrumbItem>
Expand Down Expand Up @@ -126,11 +128,13 @@
var userIds = posts.Select(p => p.UserId).Where(id => id.HasValue).Select(id => id.Value).Distinct().ToList();
foreach (var userId in userIds)
{
if (!userNames.ContainsKey(userId))
if (!userFirstNames.ContainsKey(userId))
{
var user = await StudentsService.GetStudentAsync(userId);
if (user != null)
{
userFirstNames[userId] = user.FirstName ?? "Unknown";
userLastNames[userId] = user.LastName ?? "User";
userAvatars[userId] = user.ProfileImageUrl ?? string.Empty;
}
}
Expand All @@ -144,15 +148,7 @@
}

}
// Method to get cached user name
private string GetCachedUserName(Guid? userId)
{
if (userId.HasValue && userNames.ContainsKey(userId.Value))
{
return userNames[userId.Value];
}
return "Unknown User";
}


// Method to get cached user avatar
private string GetCachedUserAvatar(Guid? userId)
Expand All @@ -164,6 +160,24 @@
return string.Empty;
}

private string GetCachedUserFirstName(Guid? userId)
{
if (userId.HasValue && userFirstNames.ContainsKey(userId.Value))
{
return userFirstNames[userId.Value];
}
return "Unknown";
}

private string GetCachedUserLastName(Guid? userId)
{
if (userId.HasValue && userLastNames.ContainsKey(userId.Value))
{
return userLastNames[userId.Value];
}
return "User";
}

private async Task HandleReaction((Guid postId, ReactionType reactionType) reactionInfo)
{
var (postId, reactionType) = reactionInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
</CardHeaderAvatar>

<CardHeaderContent>
<MudText Typo="Typo.subtitle1" Class="mb-1 cursor-pointer" @onclick="NavigateToUserProfile">@Username</MudText>
<MudText Typo="Typo.subtitle1" Class="mb-1 cursor-pointer" @onclick="NavigateToUserProfile">
@($"{FirstName} {LastName}")
</MudText>
<MudText Typo="Typo.caption" Class="text-muted">@PostDate</MudText>
</CardHeaderContent>
</MudCardHeader>
Expand Down Expand Up @@ -89,7 +91,8 @@

@code {
[Parameter] public PostDto Post { get; set; }
[Parameter] public string Username { get; set; }
[Parameter] public string FirstName { get; set; }
[Parameter] public string LastName { get; set; }
[Parameter] public string UserProfileImage { get; set; }
[Parameter] public string PostDate { get; set; }
[Parameter] public Dictionary<Guid, ReactionsSummaryDto> ReactionsSummaries { get; set; } = new();
Expand All @@ -102,7 +105,6 @@

private bool isCommentSectionVisible;

// Property to determine which image to show
private string DisplayedUserProfileImage => string.IsNullOrEmpty(UserProfileImage) ? "images/default_profile_image.webp" : UserProfileImage;

private void ToggleCommentSection()
Expand All @@ -115,7 +117,7 @@
var parameters = new DialogParameters
{
{ "Post", Post },
{ "Username", Username },
{ "Username", $"{FirstName} {LastName}" },
{ "UserProfileImage", DisplayedUserProfileImage },
{ "IssuerId", IdentityService.GetCurrentUserIdFromJwtAsync() }
};
Expand Down
22 changes: 21 additions & 1 deletion MiniSpace.Web/src/Astravent.Web.Wasm/Pages/Posts/PostsList.razor
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
<MudItem xs="12">
<PostItem
Post="@post"
Username="@GetUserName(post.UserId)"
FirstName="@GetUserFirstName(post.UserId)"
LastName="@GetUserLastName(post.UserId)"
UserProfileImage="@GetUserAvatar(post.UserId)"
PostDate="@post.CreatedAt.ToString("g")"
ReactionsSummaries="@reactionsSummaries"
Expand Down Expand Up @@ -221,4 +222,23 @@
Snackbar.Add($"Failed to add comment: {response.ErrorMessage?.Reason}", Severity.Error);
}
}

private string GetUserFirstName(Guid? userId)
{
if (userId.HasValue && studentsCache.ContainsKey(userId.Value))
{
return studentsCache[userId.Value].FirstName ?? "Unknown";
}
return "Unknown";
}

private string GetUserLastName(Guid? userId)
{
if (userId.HasValue && studentsCache.ContainsKey(userId.Value))
{
return studentsCache[userId.Value].LastName ?? "User";
}
return "User";
}

}
19 changes: 14 additions & 5 deletions MiniSpace.Web/src/Astravent.Web.Wasm/Pages/Posts/PostsMy.razor
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
<MudItem xs="12">
<PostItem
Post="@post"
Username="@GetUserName(post.UserId)"
FirstName="@GetUserFirstName(post.UserId)"
LastName="@GetUserLastName(post.UserId)"
UserProfileImage="@GetUserAvatar(post.UserId)"
PostDate="@post.CreatedAt.ToString("g")"
ReactionsSummaries="@reactionsSummaries"
Expand Down Expand Up @@ -67,7 +68,6 @@
{
isLoading = true;

// Initialize the authentication and get the current user ID
await IdentityService.InitializeAuthenticationState();

if (IdentityService.IsAuthenticated)
Expand Down Expand Up @@ -172,13 +172,22 @@
return string.Empty;
}

private string GetUserName(Guid? userId)
private string GetUserFirstName(Guid? userId)
{
if (userId.HasValue && studentsCache.ContainsKey(userId.Value))
{
return $"{studentsCache[userId.Value].FirstName} {studentsCache[userId.Value].LastName}";
return studentsCache[userId.Value].FirstName ?? "Unknown";
}
return "Unknown User";
return "Unknown";
}

private string GetUserLastName(Guid? userId)
{
if (userId.HasValue && studentsCache.ContainsKey(userId.Value))
{
return studentsCache[userId.Value].LastName ?? "User";
}
return "User";
}

private async Task OnPageChanged(int newPage)
Expand Down

0 comments on commit 9ade62d

Please sign in to comment.