diff --git a/Blazor/Blazor/App.razor b/Blazor/Blazor/App.razor index 6fd3ed1..af15d68 100644 --- a/Blazor/Blazor/App.razor +++ b/Blazor/Blazor/App.razor @@ -1,12 +1,14 @@ - - - - - - - Not found - -

Sorry, there's nothing at this address.

-
-
-
+ + + + + + + + Not found + +

Sorry, there's nothing at this address.

+
+
+
+
diff --git a/Blazor/Blazor/Blazor.csproj b/Blazor/Blazor/Blazor.csproj index 47b71bb..160b19e 100644 --- a/Blazor/Blazor/Blazor.csproj +++ b/Blazor/Blazor/Blazor.csproj @@ -1,4 +1,4 @@ - + net7.0 @@ -6,11 +6,30 @@ enable + + + + + + + + + + + + + + + <_ContentIncludedByDefault Remove="Pages\_Layout.cshtml" /> + + + + diff --git a/Blazor/Blazor/Modals/DeleteConfirmation.razor b/Blazor/Blazor/Modals/DeleteConfirmation.razor new file mode 100644 index 0000000..959fbda --- /dev/null +++ b/Blazor/Blazor/Modals/DeleteConfirmation.razor @@ -0,0 +1,12 @@ +@page "/DeleteConfirmation" + +
+ +

+ Are you sure you want to delete @chapter.Name ? +

+ + + + +
diff --git a/Blazor/Blazor/Modals/DeleteConfirmation.razor.cs b/Blazor/Blazor/Modals/DeleteConfirmation.razor.cs new file mode 100644 index 0000000..40de040 --- /dev/null +++ b/Blazor/Blazor/Modals/DeleteConfirmation.razor.cs @@ -0,0 +1,38 @@ +using Blazor.Services; +using Blazored.Modal.Services; +using Blazored.Modal; +using Microsoft.AspNetCore.Components; +using Blazor.Models; + +namespace Blazor.Modals +{ + public partial class DeleteConfirmation + { + [CascadingParameter] + public BlazoredModalInstance ModalInstance { get; set; } + + [Inject] + public IDataService DataService { get; set; } + + [Parameter] + public int Id { get; set; } + + private Chapter chapter = new Chapter(); + + protected override async Task OnInitializedAsync() + { + // Get the chapter + chapter = await DataService.GetById(Id); + } + + void ConfirmDelete() + { + ModalInstance.CloseAsync(ModalResult.Ok(true)); + } + + void Cancel() + { + ModalInstance.CancelAsync(); + } + } +} diff --git a/Blazor/Blazor/Models/Answer.cs b/Blazor/Blazor/Models/Answer.cs index a27ec6f..94ca51f 100644 --- a/Blazor/Blazor/Models/Answer.cs +++ b/Blazor/Blazor/Models/Answer.cs @@ -1,16 +1,16 @@ -namespace Blazor.Models -{ - public class Answer - { - public int Id { get; private set; } - public string Content { get; set; } - public int IdQuestion { get; private set; } - +namespace Blazor.Models +{ + public class Answer + { + public int Id { get; private set; } + public string Content { get; set; } + public int IdQuestion { get; private set; } + public Answer(int id, string content, int idQuestion) { Id = id; Content = content; IdQuestion = idQuestion; - } - } -} + } + } +} diff --git a/Blazor/Blazor/Pages/Chapters.razor b/Blazor/Blazor/Pages/Chapters.razor index 5504b64..3e4f9cb 100644 --- a/Blazor/Blazor/Pages/Chapters.razor +++ b/Blazor/Blazor/Pages/Chapters.razor @@ -1,6 +1,7 @@ @page "/chapters" @using Blazor.Models; @using Blazorise.DataGrid +@using Blazored.Modal;

Chapters

@@ -21,6 +22,7 @@ Editer + diff --git a/Blazor/Blazor/Pages/Chapters.razor.cs b/Blazor/Blazor/Pages/Chapters.razor.cs index 2dd498f..bb9c476 100644 --- a/Blazor/Blazor/Pages/Chapters.razor.cs +++ b/Blazor/Blazor/Pages/Chapters.razor.cs @@ -2,8 +2,10 @@ using Blazor.Models; using Blazorise.DataGrid; using Blazored.LocalStorage; -using Blazorise; using Blazor.Services; +using Blazor.Modals; +using Blazored.Modal; +using Blazored.Modal.Services; namespace Blazor.Pages { @@ -13,6 +15,12 @@ namespace Blazor.Pages private int totalItem; + [Inject] + public NavigationManager NavigationManager { get; set; } + + [CascadingParameter] + public IModalService Modal { get; set; } + [Inject] public IDataService DataService { get; set; } @@ -32,5 +40,26 @@ namespace Blazor.Pages totalItem = await DataService.Count(); } } + + + private async void OnDelete(int id) + { + var parameters = new ModalParameters(); + parameters.Add(nameof(Chapter.Id), id); + + var modal = Modal.Show("Delete Confirmation", parameters); + var result = modal.Result; + + if (result.IsCanceled) + { + return; + } + + await DataService.Delete(id); + + // Reload the page + NavigationManager.NavigateTo("chapters", true); + } + } } diff --git a/Blazor/Blazor/Pages/_Layout.cshtml b/Blazor/Blazor/Pages/_Layout.cshtml new file mode 100644 index 0000000..c993101 --- /dev/null +++ b/Blazor/Blazor/Pages/_Layout.cshtml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Blazor/Blazor/Program.cs b/Blazor/Blazor/Program.cs index 2d9e404..c97103c 100644 --- a/Blazor/Blazor/Program.cs +++ b/Blazor/Blazor/Program.cs @@ -6,6 +6,7 @@ using Blazorise.Bootstrap; using Blazorise.Icons.FontAwesome; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; +using Blazored.Modal; var builder = WebApplication.CreateBuilder(args); @@ -23,6 +24,9 @@ builder.Services builder.Services.AddBlazoredLocalStorage(); +builder.Services.AddBlazoredModal(); + + builder.Services.AddScoped(); var app = builder.Build(); diff --git a/Blazor/Blazor/Services/DataLocalService.cs b/Blazor/Blazor/Services/DataLocalService.cs index 37c6ea2..9df1ada 100644 --- a/Blazor/Blazor/Services/DataLocalService.cs +++ b/Blazor/Blazor/Services/DataLocalService.cs @@ -82,6 +82,22 @@ namespace Blazor.Services await _localStorage.SetItemAsync("data", currentData); } + public async Task Delete(int id) + { + // Get the current data + var currentData = await _localStorage.GetItemAsync>("data"); + + // Get the chapter int the list + var chapter = currentData.FirstOrDefault(w => w.Id == id); + + // Delete chapter in + currentData.Remove(chapter); + + // Save the data + await _localStorage.SetItemAsync("data", currentData); + } + + public async Task Count() { diff --git a/Blazor/Blazor/Services/IDataService.cs b/Blazor/Blazor/Services/IDataService.cs index 5a6bd6c..221b3fa 100644 --- a/Blazor/Blazor/Services/IDataService.cs +++ b/Blazor/Blazor/Services/IDataService.cs @@ -13,5 +13,7 @@ namespace Blazor.Services Task GetById(int id); Task Update(int id, ChapterModel model); + + Task Delete(int id); } } diff --git a/Blazor/Blazor/_Imports.razor b/Blazor/Blazor/_Imports.razor index 9665bbe..b92adac 100644 --- a/Blazor/Blazor/_Imports.razor +++ b/Blazor/Blazor/_Imports.razor @@ -9,4 +9,6 @@ @using Blazor @using Blazor.Shared @using Blazorise.DataGrid +@using Blazored.Modal +@using Blazored.Modal.Services