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 a975a06..4f6681b 100644 --- a/Blazor/Blazor/Blazor.csproj +++ b/Blazor/Blazor/Blazor.csproj @@ -1,19 +1,37 @@ - - - - net7.0 - enable - enable - - - - - - - - - - - - - + + + + net7.0 + enable + 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..bf38444 --- /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.ViewClasses; + +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/Pages/Chapters.razor b/Blazor/Blazor/Pages/Chapters.razor index f705ba1..e600fad 100644 --- a/Blazor/Blazor/Pages/Chapters.razor +++ b/Blazor/Blazor/Pages/Chapters.razor @@ -1,30 +1,32 @@ -@page "/chapters" -@using Blazor.ViewClasses; -@using Blazorise.DataGrid -

Chapters

- -
- - Ajouter - - - Exporter - -
- - - - - - - Editer - - - +@page "/chapters" +@using Blazor.ViewClasses; +@using Blazorise.DataGrid +@using Blazored.Modal; +

Chapters

+ +
+ + Ajouter + + + Exporter + +
+ + + + + + + Editer + + + + \ No newline at end of file diff --git a/Blazor/Blazor/Pages/Chapters.razor.cs b/Blazor/Blazor/Pages/Chapters.razor.cs index be82912..289da8a 100644 --- a/Blazor/Blazor/Pages/Chapters.razor.cs +++ b/Blazor/Blazor/Pages/Chapters.razor.cs @@ -1,91 +1,119 @@ -using Blazored.LocalStorage; -using Blazorise; -using Blazor.Services; -using Blazorise.DataGrid; -using ChoETL; -using Microsoft.AspNetCore.Components; -using Microsoft.JSInterop; -using System.Text; -using Blazor.ViewClasses; - -namespace Blazor.Pages; -public partial class Chapters - { - public List chapters; - - private int totalChapter; - - [Inject] - public IDataService DataService { get; set; } - public IWebHostEnvironment WebHostEnvironment { get; set; } - - [Inject] - public HttpClient Http { get; set; } - - [Inject] - public ILocalStorageService LocalStorage { get; set; } - - [Inject] - public NavigationManager NavigationManager { get; set; } - - [Inject] - public IJSRuntime IJSRuntime { get; set; } - - protected override async Task OnAfterRenderAsync(bool firstRender) - { - // Do not treat this action if is not the first render - if (!firstRender) - { - return; - } - - var currentData = await LocalStorage.GetItemAsync("data"); - - // Check if data exist in the local storage - if (currentData == null) - { - // this code add in the local storage the fake data (we load the data sync for initialize the data before load the OnReadData method) - var originalData = Http.GetFromJsonAsync($"https://trusting-panini.87-106-126-109.plesk.page/api/chapters").Result; - await LocalStorage.SetItemAsync("data", originalData); - } - } - - private async Task OnReadData(DataGridReadDataEventArgs e) - { - if (e.CancellationToken.IsCancellationRequested) - { - return; - } - - // When you use a real API, we use this follow code - //var response = await Http.GetFromJsonAsync( $"https://trusting-panini.87-106-126-109.plesk.page/api/chapters?page={e.Page}&pageSize={e.PageSize}" ); - var response = Http.GetFromJsonAsync($"https://trusting-panini.87-106-126-109.plesk.page/api/chapters").Result; - - if (!e.CancellationToken.IsCancellationRequested) - { - totalChapter = (await LocalStorage.GetItemAsync>("data")).Count; - chapters = new List(response); // an actual data for the current page - } - } - - private async void Export() - { - StringBuilder sb = new StringBuilder(); - HttpResponseMessage response = await Http.GetAsync("https://trusting-panini.87-106-126-109.plesk.page/api/chapters"); - var json = await response.Content.ReadAsStringAsync(); - using (var jsonFile = ChoJSONReader.LoadText(json)) - { - using (var csvFile = new ChoCSVWriter(sb).WithFirstLineHeader()) - { - csvFile.Write(jsonFile); - } - } - - var sentFile = new MemoryStream(Encoding.UTF32.GetBytes(sb.ToString())); - - using (var streamRef = new DotNetStreamReference(stream: sentFile)) - { - await IJSRuntime.InvokeVoidAsync("downloadFileFromStream", "data.csv", streamRef); - } - } -} +using Blazored.LocalStorage; +using Blazor.Services; +using Blazor.Modals; +using Blazored.Modal; +using Blazored.Modal.Services; +using Blazor.ViewClasses; +using System.Text; +using Microsoft.JSInterop; +using Microsoft.AspNetCore.Components; +using Blazorise.DataGrid; +using ChoETL; + + +namespace Blazor.Pages; +public partial class Chapters +{ + public List chapters; + + private int totalChapter; + + [Inject] + public NavigationManager NavigationManager { get; set; } + + [CascadingParameter] + public IModalService Modal { get; set; } + + [Inject] + public IDataService DataService { get; set; } + public IWebHostEnvironment WebHostEnvironment { get; set; } + + [Inject] + public HttpClient Http { get; set; } + + [Inject] + public ILocalStorageService LocalStorage { get; set; } + + [Inject] + public IJSRuntime IJSRuntime { get; set; } + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + // Do not treat this action if is not the first render + if (!firstRender) + { + return; + } + + var currentData = await LocalStorage.GetItemAsync("data"); + + // Check if data exist in the local storage + if (currentData == null) + { + // this code add in the local storage the fake data (we load the data sync for initialize the data before load the OnReadData method) + var originalData = Http.GetFromJsonAsync($"https://trusting-panini.87-106-126-109.plesk.page/api/chapters").Result; + await LocalStorage.SetItemAsync("data", originalData); + } + } + + private async Task OnReadData(DataGridReadDataEventArgs e) + { + if (e.CancellationToken.IsCancellationRequested) + { + return; + } + + // When you use a real API, we use this follow code + //var response = await Http.GetFromJsonAsync( $"https://trusting-panini.87-106-126-109.plesk.page/api/chapters?page={e.Page}&pageSize={e.PageSize}" ); + var response = Http.GetFromJsonAsync($"https://trusting-panini.87-106-126-109.plesk.page/api/chapters").Result; + + if (!e.CancellationToken.IsCancellationRequested) + { + totalChapter = (await LocalStorage.GetItemAsync>("data")).Count; + chapters = new List(response); // an actual data for the current page + } + } + + private async void Export() + { + StringBuilder sb = new StringBuilder(); + HttpResponseMessage response = await Http.GetAsync("https://trusting-panini.87-106-126-109.plesk.page/api/chapters"); + var json = await response.Content.ReadAsStringAsync(); + using (var jsonFile = ChoJSONReader.LoadText(json)) + { + using (var csvFile = new ChoCSVWriter(sb).WithFirstLineHeader()) + { + csvFile.Write(jsonFile); + } + } + + var sentFile = new MemoryStream(Encoding.UTF32.GetBytes(sb.ToString())); + + using (var streamRef = new DotNetStreamReference(stream: sentFile)) + { + await IJSRuntime.InvokeVoidAsync("downloadFileFromStream", "data.csv", streamRef); + } + } + + + 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 5c73e4b..2ccc758 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); @@ -30,6 +31,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 54cb5de..8d7e2e2 100644 --- a/Blazor/Blazor/Services/DataLocalService.cs +++ b/Blazor/Blazor/Services/DataLocalService.cs @@ -83,6 +83,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 7c69a44..cb9ae80 100644 --- a/Blazor/Blazor/Services/IDataService.cs +++ b/Blazor/Blazor/Services/IDataService.cs @@ -23,5 +23,7 @@ namespace Blazor.Services Task CountAdmin(); Task> ListAdmin(int currentPage, int pageSize); + + Task Delete(int id); } } diff --git a/Blazor/Blazor/_Imports.razor b/Blazor/Blazor/_Imports.razor index 0bfa3cd..b92adac 100644 --- a/Blazor/Blazor/_Imports.razor +++ b/Blazor/Blazor/_Imports.razor @@ -9,3 +9,6 @@ @using Blazor @using Blazor.Shared @using Blazorise.DataGrid +@using Blazored.Modal +@using Blazored.Modal.Services +