forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentSection.razor
More file actions
160 lines (144 loc) · 5.21 KB
/
Copy pathCommentSection.razor
File metadata and controls
160 lines (144 loc) · 5.21 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
@using ContosoSnsWebApp.Models
@using ContosoSnsWebApp.Services
@inject ApiService ApiService
<div class="mt-4">
<h4 class="mb-3">댓글 (@(comments?.Count ?? 0))</h4>
@if (isLoading)
{
<p>댓글 로딩 중...</p>
}
else if (!string.IsNullOrEmpty(loadError))
{
<div class="alert alert-warning">@loadError</div>
}
else if (comments == null || comments.Count == 0)
{
<p class="text-muted">아직 댓글이 없습니다.</p>
}
else
{
<div class="list-group list-group-flush mb-3">
@foreach (var comment in comments.OrderByDescending(c => c.CreatedAt))
{
<div class="list-group-item px-0">
<div class="d-flex w-100 justify-content-between">
<h6 class="mb-1">@comment.UserName</h6>
<small class="text-muted">@FormatDate(comment.CreatedAt)</small>
</div>
<p class="mb-1">@comment.Content</p>
</div>
}
</div>
}
<EditForm Model="newCommentRequest" OnValidSubmit="HandleCommentSubmitAsync" FormName="newCommentForm">
<DataAnnotationsValidator />
<div class="mb-2">
<InputTextArea @bind-Value="newCommentRequest.Content" class="form-control" rows="2" placeholder="댓글을 입력하세요..." required />
<ValidationMessage For="@(() => newCommentRequest.Content)" />
</div>
@if (!string.IsNullOrEmpty(submitError))
{
<div class="alert alert-danger mt-2 py-1">@submitError</div>
}
<Button Type="submit" Variant="secondary" Small="true" Disabled="isSubmitting">
@(isSubmitting ? "등록 중..." : "댓글 등록")
</Button>
</EditForm>
</div>
@code {
[Parameter, EditorRequired] public int PostId { get; set; }
[Parameter] public string? UserName { get; set; } // Current user's name
private List<Comment>? comments;
private NewCommentRequest newCommentRequest = new("", "");
private bool isLoading = true;
private bool isSubmitting = false;
private string? loadError = null;
private string? submitError = null;
protected override async Task OnInitializedAsync()
{
await LoadCommentsAsync();
if (!string.IsNullOrEmpty(UserName))
{
// newCommentRequest = newCommentRequest with { UserName = UserName };
newCommentRequest.UserName = UserName; // Ensure username is set
}
}
protected override async Task OnParametersSetAsync()
{
// Reload comments if PostId changes
// This might happen if the modal is reused without full disposal
if (PostId > 0 && (comments == null || comments.All(c => c.PostId != PostId)))
{
await LoadCommentsAsync();
}
// Update username for new comment if it changes
if (!string.IsNullOrEmpty(UserName) && newCommentRequest.UserName != UserName)
{
// newCommentRequest = newCommentRequest with { UserName = UserName };
newCommentRequest.UserName = UserName; // Ensure username is set
}
}
private async Task LoadCommentsAsync()
{
if (PostId <= 0) return;
isLoading = true;
loadError = null;
StateHasChanged();
try
{
comments = await ApiService.GetCommentsAsync(PostId);
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error loading comments: {ex.Message}");
loadError = "댓글을 불러오는 데 실패했습니다.";
}
finally
{
isLoading = false;
StateHasChanged();
}
}
private async Task HandleCommentSubmitAsync()
{
if (isSubmitting || PostId <= 0 || string.IsNullOrWhiteSpace(UserName))
{
submitError = "사용자 이름이 필요합니다."; // Should ideally not happen if UserName is passed correctly
return;
}
isSubmitting = true;
submitError = null;
StateHasChanged();
// newCommentRequest = newCommentRequest with { UserName = UserName }; // Ensure username is set
newCommentRequest.UserName = UserName; // Ensure username is set
try
{
var createdComment = await ApiService.CreateCommentAsync(PostId, newCommentRequest);
if (createdComment != null)
{
comments ??= [];
comments.Add(createdComment);
newCommentRequest = new(UserName, ""); // Reset content, keep username
}
else
{
submitError = "댓글 등록에 실패했습니다.";
}
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error submitting comment: {ex.Message}");
submitError = "댓글 등록 중 오류가 발생했습니다.";
}
finally
{
isSubmitting = false;
StateHasChanged();
}
}
private string FormatDate(DateTime? date)
{
// More detailed format for comments might be nice
return date?.ToString("yyyy-MM-dd HH:mm") ?? string.Empty;
}
}