Skip to content

Commit 39afc62

Browse files
Copilotfboucher
andcommitted
Add error handling and delete confirmation to NoteEditor
Co-authored-by: fboucher <2404846+fboucher@users.noreply.github.com>
1 parent 0e3a24a commit 39afc62

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

src/NoteBookmark.BlazorApp/Components/Pages/NoteEditor.razor

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
@inject PostNoteClient client
66
@inject NavigationManager Navigation
77
@inject IToastService toastService
8+
@inject IDialogService DialogService
89

910
@rendermode InteractiveServer
1011

@@ -44,7 +45,7 @@ else
4445
<FluentStack Orientation="Orientation.Horizontal" style="margin-top: 20px;">
4546
<FluentButton Type="ButtonType.Submit" Appearance="Appearance.Accent">Save</FluentButton>
4647
<FluentButton OnClick="BackToList" Appearance="Appearance.Neutral">Back to List</FluentButton>
47-
<FluentButton OnClick="DeleteNoteAsync" Appearance="Appearance.Accent" Color="Color.Danger">Delete</FluentButton>
48+
<FluentButton OnClick="ShowDeleteConfirmation" Appearance="Appearance.Accent" Color="Color.Danger">Delete</FluentButton>
4849
</FluentStack>
4950
</EditForm>
5051
}
@@ -61,23 +62,43 @@ else
6162
{
6263
if (!string.IsNullOrEmpty(noteId))
6364
{
64-
note = await client.GetNote(noteId);
65+
try
66+
{
67+
note = await client.GetNote(noteId);
68+
if (note == null)
69+
{
70+
toastService.ShowError("Note not found.");
71+
Navigation.NavigateTo("/posts");
72+
}
73+
}
74+
catch (Exception)
75+
{
76+
toastService.ShowError("Failed to load note. Please try again.");
77+
Navigation.NavigateTo("/posts");
78+
}
6579
}
6680
}
6781

6882
private async Task SaveNote()
6983
{
7084
if (note != null)
7185
{
72-
var result = await client.UpdateNote(note);
73-
if (result)
86+
try
7487
{
75-
toastService.ShowSuccess("Note updated successfully!");
76-
Navigation.NavigateTo("/posts");
88+
var result = await client.UpdateNote(note);
89+
if (result)
90+
{
91+
toastService.ShowSuccess("Note updated successfully!");
92+
Navigation.NavigateTo("/posts");
93+
}
94+
else
95+
{
96+
toastService.ShowError("Failed to update note. Please check your input and try again.");
97+
}
7798
}
78-
else
99+
catch (Exception)
79100
{
80-
toastService.ShowError("Failed to update note. Please try again.");
101+
toastService.ShowError("An error occurred while updating the note. Please try again.");
81102
}
82103
}
83104
}
@@ -87,19 +108,41 @@ else
87108
Navigation.NavigateTo("/posts");
88109
}
89110

111+
private async Task ShowDeleteConfirmation()
112+
{
113+
var dialog = await DialogService.ShowConfirmationAsync(
114+
"Are you sure you want to delete this note? This action cannot be undone.",
115+
"Confirm Delete",
116+
"Delete",
117+
"Cancel");
118+
119+
var result = await dialog.Result;
120+
if (!result.Cancelled)
121+
{
122+
await DeleteNoteAsync();
123+
}
124+
}
125+
90126
private async Task DeleteNoteAsync()
91127
{
92128
if (note != null)
93129
{
94-
var result = await client.DeleteNote(note.RowKey);
95-
if (result)
130+
try
96131
{
97-
toastService.ShowSuccess("Note deleted successfully!");
98-
Navigation.NavigateTo("/posts");
132+
var result = await client.DeleteNote(note.RowKey);
133+
if (result)
134+
{
135+
toastService.ShowSuccess("Note deleted successfully!");
136+
Navigation.NavigateTo("/posts");
137+
}
138+
else
139+
{
140+
toastService.ShowError("Failed to delete note. The note may not exist anymore.");
141+
}
99142
}
100-
else
143+
catch (Exception)
101144
{
102-
toastService.ShowError("Failed to delete note. Please try again.");
145+
toastService.ShowError("An error occurred while deleting the note. Please try again.");
103146
}
104147
}
105148
}

0 commit comments

Comments
 (0)