forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentInput.razor
More file actions
90 lines (80 loc) · 2.92 KB
/
Copy pathCommentInput.razor
File metadata and controls
90 lines (80 loc) · 2.92 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
86
87
88
89
90
@inject AuthService AuthService
@inject ApiService ApiService
<div class="bg-white border border-gray-200 rounded-md p-4">
<div class="flex gap-2">
<div class="w-8 h-8 rounded-full bg-gray-200 flex-shrink-0"></div>
<div class="flex-1">
<textarea @bind="content" @onkeypress="HandleKeyPress"
placeholder="Write a comment..."
disabled="@isLoading"
class="w-full p-2 text-sm text-gray-900 bg-gray-100 rounded border-none resize-none focus-outline-none"
rows="2"></textarea>
@if (!string.IsNullOrEmpty(error))
{
<p class="text-red-500 text-xs mt-1">@error</p>
}
<div class="flex justify-end gap-2 mt-2">
<button @onclick="HandleCancel"
disabled="@(isLoading || string.IsNullOrWhiteSpace(content))"
class="px-3 py-1 text-xs text-gray-600 hover-text-gray-800 disabled-opacity-70">
Cancel
</button>
<button @onclick="HandleSubmit"
disabled="@(isLoading || string.IsNullOrWhiteSpace(content))"
class="px-3 py-1 text-xs bg-blue-600 text-white rounded hover-bg-blue-700 disabled-opacity-70">
@(isLoading ? "Posting..." : "Post")
</button>
</div>
</div>
</div>
</div>
@code {
[Parameter] public string PostId { get; set; } = string.Empty;
[Parameter] public EventCallback OnCommentAdded { get; set; }
private string content = "";
private bool isLoading = false;
private string error = "";
private async Task HandleKeyPress(KeyboardEventArgs e)
{
if (e.Key == "Enter" && !e.ShiftKey && !isLoading && !string.IsNullOrWhiteSpace(content))
{
await HandleSubmit();
}
}
private async Task HandleSubmit()
{
var trimmedContent = content.Trim();
if (string.IsNullOrEmpty(trimmedContent))
{
error = "Please enter a comment.";
return;
}
isLoading = true;
error = "";
StateHasChanged();
try
{
var user = AuthService.AuthState.User;
if (user == null) throw new InvalidOperationException("User not authenticated");
await ApiService.CreateCommentAsync(PostId, trimmedContent, user.Username);
content = "";
await OnCommentAdded.InvokeAsync();
}
catch (Exception ex)
{
error = "An error occurred while posting your comment.";
Console.WriteLine($"Error creating comment: {ex.Message}");
}
finally
{
isLoading = false;
StateHasChanged();
}
}
private void HandleCancel()
{
content = "";
error = "";
StateHasChanged();
}
}