forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostCard.razor
More file actions
85 lines (77 loc) · 2.56 KB
/
Copy pathPostCard.razor
File metadata and controls
85 lines (77 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
@inject NavigationManager Navigation
@inject ApiService ApiService
@inject AuthService AuthService
<div class="post-card" @onclick="HandlePostClick">
<div class="flex items-center mb-2">
<div class="w-10 h-10 rounded-full bg-gray-200 mr-2"></div>
<div class="text-base font-bold text-gray-900">@Post.Username</div>
</div>
<div class="mb-2">
<p class="text-base text-gray-900 leading-relaxed break-words">@Post.Content</p>
</div>
<div class="flex gap-6 mt-2">
<button @onclick="HandleLikeToggle" @onclick:stopPropagation="true"
class="flex items-center gap-1 @(isLiked ? "text-red-500" : "text-gray-500 hover-text-red-500")"
aria-label="Like">
<HeartIcon Filled="@isLiked" />
@if (likesCount > 0)
{
<span class="text-xs">@likesCount</span>
}
</button>
<button @onclick="HandleCommentClick" @onclick:stopPropagation="true"
class="flex items-center gap-1 text-gray-500 hover-text-red-500"
aria-label="Comment">
<CommentIcon />
@if (Post.CommentsCount > 0)
{
<span class="text-xs">@Post.CommentsCount</span>
}
</button>
</div>
</div>
@code {
[Parameter] public Post Post { get; set; } = new();
[Parameter] public EventCallback OnPostDeleted { get; set; }
[Parameter] public EventCallback OnPostUpdated { get; set; }
private bool isLiked;
private int likesCount;
protected override void OnInitialized()
{
isLiked = Post.IsLiked;
likesCount = Post.LikesCount;
}
private void HandlePostClick()
{
Navigation.NavigateTo($"/post/{Post.Id}");
}
private async Task HandleLikeToggle()
{
try
{
var user = AuthService.AuthState.User;
if (user == null) return;
if (isLiked)
{
await ApiService.UnlikePostAsync(Post.Id, user.Username);
likesCount--;
isLiked = false;
}
else
{
var result = await ApiService.LikePostAsync(Post.Id, user.Username);
likesCount = result.LikesCount;
isLiked = true;
}
StateHasChanged();
}
catch (Exception ex)
{
Console.WriteLine($"Error occurred while processing like: {ex.Message}");
}
}
private void HandleCommentClick()
{
Navigation.NavigateTo($"/post/{Post.Id}");
}
}