forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostingModal.razor
More file actions
94 lines (84 loc) · 2.76 KB
/
Copy pathPostingModal.razor
File metadata and controls
94 lines (84 loc) · 2.76 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
@inject AuthService AuthService
@inject ApiService ApiService
@inject IJSRuntime JSRuntime
<Modal IsOpen="IsOpen" OnClose="HandleCancel">
<div class="w-full mb-4">
<textarea @bind="content" @oninput="HandleContentChange"
placeholder="Enter your content."
disabled="@isLoading"
class="textarea-custom"
rows="6"></textarea>
</div>
@if (!string.IsNullOrEmpty(error))
{
<p class="text-red-500 text-sm mb-4 text-center">@error</p>
}
<div class="flex justify-center gap-4">
<button @onclick="HandleSubmit"
disabled="@(isLoading || string.IsNullOrWhiteSpace(content))"
class="btn btn-primary">
@(isLoading ? "Processing..." : "Submit")
</button>
<button @onclick="HandleCancel"
disabled="@isLoading"
class="btn btn-secondary">
Cancel
</button>
</div>
</Modal>
@code {
[Parameter] public bool IsOpen { get; set; }
[Parameter] public EventCallback OnClose { get; set; }
[Parameter] public EventCallback<Post> OnPostCreated { get; set; }
private string content = "";
private bool isLoading = false;
private string error = "";
private void HandleContentChange(ChangeEventArgs e)
{
content = e.Value?.ToString() ?? "";
if (!string.IsNullOrEmpty(error))
error = "";
StateHasChanged();
}
private async Task HandleSubmit()
{
if (string.IsNullOrWhiteSpace(content))
{
error = "Please enter content.";
return;
}
isLoading = true;
error = "";
StateHasChanged();
try
{
var user = AuthService.AuthState.User;
if (user == null) throw new InvalidOperationException("User not authenticated");
var createdPost = await ApiService.CreatePostAsync(content, user.Username);
content = "";
await OnClose.InvokeAsync();
await OnPostCreated.InvokeAsync(createdPost);
}
catch (Exception ex)
{
error = "An error occurred while creating the post. Please try again.";
Console.WriteLine($"Error creating post: {ex.Message}");
}
finally
{
isLoading = false;
StateHasChanged();
}
}
private async Task HandleCancel()
{
if (!string.IsNullOrWhiteSpace(content))
{
var shouldCancel = await JSRuntime.InvokeAsync<bool>("confirm", "You have unsaved content. Are you sure you want to cancel?");
if (!shouldCancel) return;
}
content = "";
error = "";
await OnClose.InvokeAsync();
}
}