ajout du DI et IOC pour la page List

blazor
Patrick BRUGIERE 1 year ago
parent 12510a1889
commit 882ee187e9

@ -9,6 +9,7 @@ using adminBlazor.Services;
using Blazored.Modal.Services; using Blazored.Modal.Services;
using Blazored.Modal; using Blazored.Modal;
using adminBlazor.Modals; using adminBlazor.Modals;
using Blazorise;
namespace adminBlazor.Pages namespace adminBlazor.Pages
{ {
@ -28,30 +29,34 @@ namespace adminBlazor.Pages
public NavigationManager NavigationManager { get; set; } public NavigationManager NavigationManager { get; set; }
[CascadingParameter] [CascadingParameter]
public IModalService Modal { get; set; } public Blazored.Modal.Services.IModalService Modal { get; set; }
[Inject] [Inject]
public IDataService DataService { get; set; } 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<User[]>("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 var currentData = await LocalStorage.GetItemAsync<User[]>("data");
if (currentData == null)
{ // Check if data exist in the local storage
// this code add in the local storage the fake data (we load the data sync for initialize the data before load the OnReadData method) if (currentData == null)
var originalData = Http.GetFromJsonAsync<User[]>($"{NavigationManager.BaseUri}user.json").Result; {
await LocalStorage.SetItemAsync("data", originalData); // 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<User[]>($"{NavigationManager.BaseUri}user.json").Result;
} await LocalStorage.SetItemAsync("data", originalData);
}
}
*/
private async Task OnReadData(DataGridReadDataEventArgs<User> e) private async Task OnReadData(DataGridReadDataEventArgs<User> e)
{ {
@ -62,12 +67,12 @@ namespace adminBlazor.Pages
// When you use a real API, we use this follow code // When you use a real API, we use this follow code
//var response = await Http.GetJsonAsync<Data[]>( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" ); //var response = await Http.GetJsonAsync<Data[]>( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" );
var response = (await LocalStorage.GetItemAsync<User[]>("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList(); //var response = (await LocalStorage.GetItemAsync<User[]>("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();
if (!e.CancellationToken.IsCancellationRequested) if (!e.CancellationToken.IsCancellationRequested)
{ {
totalUser = (await LocalStorage.GetItemAsync<List<User>>("data")).Count; _users = await DataService.List(e.Page, e.PageSize);
_users = new List<User>(response); // an actual data for the current page totalUser = await DataService.Count();// an actual data for the current page
} }
} }

@ -12,11 +12,12 @@ using System.Globalization;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<IDataService, DataLocalService>();
// Add services to the container. // Add services to the container.
builder.Services.AddRazorPages(); builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor(); builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>(); builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddScoped<IDataService, DataLocalService>();
builder.Services.AddHttpClient(); builder.Services.AddHttpClient();
builder.Services.AddBlazoredLocalStorage(); builder.Services.AddBlazoredLocalStorage();

@ -49,9 +49,20 @@ namespace adminBlazor.Services
} }
public Task<int> Count() public async Task<int> Count()
{ {
throw new NotImplementedException(); // Load data from the local storage
var currentData = await _localStorage.GetItemAsync<User[]>("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<User[]>($"{_navigationManager.BaseUri}fake-data.json");
await _localStorage.SetItemAsync("data", originalData);
}
return (await _localStorage.GetItemAsync<User[]>("data")).Length;
} }
public async Task<User> GetById(int id) public async Task<User> GetById(int id)
@ -69,9 +80,20 @@ namespace adminBlazor.Services
return user; return user;
} }
public Task<List<User>> List(int currentPage, int pageSize) public async Task<List<User>> List(int currentPage, int pageSize)
{ {
throw new NotImplementedException(); // Load data from the local storage
var currentData = await _localStorage.GetItemAsync<User[]>("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<User[]>($"{_navigationManager.BaseUri}user.json");
await _localStorage.SetItemAsync("data", originalData);
}
return (await _localStorage.GetItemAsync<User[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
} }
public async Task Update(int id, UserModel model) public async Task Update(int id, UserModel model)

Loading…
Cancel
Save