diff --git a/Project/adminBlazor/adminBlazor/Pages/List.razor.cs b/Project/adminBlazor/adminBlazor/Pages/List.razor.cs index af8583b..a05fe67 100644 --- a/Project/adminBlazor/adminBlazor/Pages/List.razor.cs +++ b/Project/adminBlazor/adminBlazor/Pages/List.razor.cs @@ -9,6 +9,7 @@ using adminBlazor.Services; using Blazored.Modal.Services; using Blazored.Modal; using adminBlazor.Modals; +using Blazorise; namespace adminBlazor.Pages { @@ -28,30 +29,34 @@ namespace adminBlazor.Pages public NavigationManager NavigationManager { get; set; } [CascadingParameter] - public IModalService Modal { get; set; } + public Blazored.Modal.Services.IModalService Modal { get; set; } [Inject] public IDataService DataService { get; set; } + [Inject] + public IWebHostEnvironment WebHostEnvironment { 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"); + /* protected override async Task OnAfterRenderAsync(bool firstRender) + { + // Do not treat this action if is not the first render + if (!firstRender) + { + return; + } - // 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($"{NavigationManager.BaseUri}user.json").Result; - await LocalStorage.SetItemAsync("data", originalData); - } - } + 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($"{NavigationManager.BaseUri}user.json").Result; + await LocalStorage.SetItemAsync("data", originalData); + } + } + */ private async Task OnReadData(DataGridReadDataEventArgs e) { @@ -62,12 +67,12 @@ namespace adminBlazor.Pages // When you use a real API, we use this follow code //var response = await Http.GetJsonAsync( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" ); - var response = (await LocalStorage.GetItemAsync("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList(); + //var response = (await LocalStorage.GetItemAsync("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList(); if (!e.CancellationToken.IsCancellationRequested) { - totalUser = (await LocalStorage.GetItemAsync>("data")).Count; - _users = new List(response); // an actual data for the current page + _users = await DataService.List(e.Page, e.PageSize); + totalUser = await DataService.Count();// an actual data for the current page } } diff --git a/Project/adminBlazor/adminBlazor/Program.cs b/Project/adminBlazor/adminBlazor/Program.cs index 918ed6c..017d360 100644 --- a/Project/adminBlazor/adminBlazor/Program.cs +++ b/Project/adminBlazor/adminBlazor/Program.cs @@ -12,11 +12,12 @@ using System.Globalization; using Microsoft.Extensions.Options; var builder = WebApplication.CreateBuilder(args); -builder.Services.AddScoped(); + // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); builder.Services.AddSingleton(); +builder.Services.AddScoped(); builder.Services.AddHttpClient(); builder.Services.AddBlazoredLocalStorage(); diff --git a/Project/adminBlazor/adminBlazor/Services/DataLocalService.cs b/Project/adminBlazor/adminBlazor/Services/DataLocalService.cs index e42d7f4..02ca198 100644 --- a/Project/adminBlazor/adminBlazor/Services/DataLocalService.cs +++ b/Project/adminBlazor/adminBlazor/Services/DataLocalService.cs @@ -49,9 +49,20 @@ namespace adminBlazor.Services } - public Task Count() + public async Task Count() { - throw new NotImplementedException(); + // Load data from the local storage + 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 + var originalData = await _http.GetFromJsonAsync($"{_navigationManager.BaseUri}fake-data.json"); + await _localStorage.SetItemAsync("data", originalData); + } + + return (await _localStorage.GetItemAsync("data")).Length; } public async Task GetById(int id) @@ -69,9 +80,20 @@ namespace adminBlazor.Services return user; } - public Task> List(int currentPage, int pageSize) + public async Task> List(int currentPage, int pageSize) { - throw new NotImplementedException(); + // Load data from the local storage + 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 + var originalData = await _http.GetFromJsonAsync($"{_navigationManager.BaseUri}user.json"); + await _localStorage.SetItemAsync("data", originalData); + } + + return (await _localStorage.GetItemAsync("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList(); } public async Task Update(int id, UserModel model)