From b513054114fac4dc392d22f4eb0e975accac86ff Mon Sep 17 00:00:00 2001 From: "yvan.calatayud" Date: Fri, 12 Jan 2024 11:30:34 +0100 Subject: [PATCH] feat : pagination --- .../Blazor/Pages/Admins/Administrators.razor.cs | 11 ++++------- Blazor/Blazor/Pages/Chapters/Chapters.razor.cs | 12 +++++------- Blazor/Blazor/Pages/Players/Players.razor.cs | 12 +++++------- Blazor/Blazor/Pages/Questions/Questions.razor.cs | 16 +++++----------- 4 files changed, 19 insertions(+), 32 deletions(-) diff --git a/Blazor/Blazor/Pages/Admins/Administrators.razor.cs b/Blazor/Blazor/Pages/Admins/Administrators.razor.cs index 1e9cfea..75b8fbd 100644 --- a/Blazor/Blazor/Pages/Admins/Administrators.razor.cs +++ b/Blazor/Blazor/Pages/Admins/Administrators.razor.cs @@ -43,19 +43,16 @@ public partial class Administrators { return; } + // Calculez l'index de départ pour la pagination + int page = ((e.Page - 1) * e.PageSize) / 10 + 1; - var response = Http.GetFromJsonAsync(API.API_URL+"administrators/"+API.TOKEN).Result; + // Envoyez une requête à l'API pour récupérer les questions de la page actuelle + var response = await Http.GetFromJsonAsync(API.API_URL + "administrators/" + API.TOKEN + $"?page={page}"); if (!e.CancellationToken.IsCancellationRequested && response != null) { administrators = new List(response); // an actual data for the current page totalItem = administrators.Count; - var currentData = await LocalStorage.GetItemAsync("data"); - if (currentData == null || currentData.Length != administrators.Count) - { - var originalData = Http.GetFromJsonAsync(API.API_URL + "administrators/" + API.TOKEN).Result; - await LocalStorage.SetItemAsync("data", originalData); - } } } diff --git a/Blazor/Blazor/Pages/Chapters/Chapters.razor.cs b/Blazor/Blazor/Pages/Chapters/Chapters.razor.cs index 1f7e36d..65c33b0 100644 --- a/Blazor/Blazor/Pages/Chapters/Chapters.razor.cs +++ b/Blazor/Blazor/Pages/Chapters/Chapters.razor.cs @@ -77,18 +77,16 @@ public partial class Chapters return; } - var response = Http.GetFromJsonAsync(API.API_URL+"chapters/"+API.TOKEN).Result; + // Calculez l'index de départ pour la pagination + int page = ((e.Page - 1) * e.PageSize) / 10 + 1; + + // Envoyez une requête à l'API pour récupérer les questions de la page actuelle + var response = await Http.GetFromJsonAsync(API.API_URL + "chapters/" + API.TOKEN + $"?page={page}"); if (!e.CancellationToken.IsCancellationRequested) { chapters = new List(response); // an actual data for the current page totalChapter = chapters.Count; - var currentData = await LocalStorage.GetItemAsync("data"); - if (currentData == null || currentData.Length != chapters.Count) - { - var originalData = Http.GetFromJsonAsync(API.API_URL+"chapters/"+API.TOKEN).Result; - await LocalStorage.SetItemAsync("data", originalData); - } } } } diff --git a/Blazor/Blazor/Pages/Players/Players.razor.cs b/Blazor/Blazor/Pages/Players/Players.razor.cs index 9e01e42..b63eb82 100644 --- a/Blazor/Blazor/Pages/Players/Players.razor.cs +++ b/Blazor/Blazor/Pages/Players/Players.razor.cs @@ -78,18 +78,16 @@ public partial class Players return; } - var response = Http.GetFromJsonAsync(API.API_URL+"players/"+API.TOKEN).Result; + // Calculez l'index de départ pour la pagination + int page = ((e.Page - 1) * e.PageSize) / 10 + 1; + + // Envoyez une requête à l'API pour récupérer les questions de la page actuelle + var response = await Http.GetFromJsonAsync(API.API_URL + "players/" + API.TOKEN + $"?page={page}"); if (!e.CancellationToken.IsCancellationRequested) { players = new List(response); // an actual data for the current page totalPlayer = players.Count; - var currentData = await LocalStorage.GetItemAsync("data"); - if (currentData == null || currentData.Length != players.Count) - { - var originalData = Http.GetFromJsonAsync(API.API_URL+"players/"+API.TOKEN).Result; - await LocalStorage.SetItemAsync("data", originalData); - } } } } diff --git a/Blazor/Blazor/Pages/Questions/Questions.razor.cs b/Blazor/Blazor/Pages/Questions/Questions.razor.cs index 3a22cde..6ad0be9 100644 --- a/Blazor/Blazor/Pages/Questions/Questions.razor.cs +++ b/Blazor/Blazor/Pages/Questions/Questions.razor.cs @@ -84,8 +84,11 @@ public partial class Questions return; } - - var response = Http.GetFromJsonAsync(API.API_URL+"questions/"+API.TOKEN).Result; + // Calculez l'index de départ pour la pagination + int page = ((e.Page - 1) * e.PageSize)/10 + 1; + + // Envoyez une requête à l'API pour récupérer les questions de la page actuelle + var response = await Http.GetFromJsonAsync(API.API_URL + "questions/" + API.TOKEN + $"?page={page}"); if (!e.CancellationToken.IsCancellationRequested && response != null) { @@ -99,15 +102,6 @@ public partial class Questions questions = selectedQuestions; totalQuestion = questions.Count; - var currentData = await LocalStorage.GetItemAsync("data"); - - // Check if data exist in the local storage - if (currentData == null || currentData.Length != questions.Count) - { - // 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(API.API_URL+"chapters/"+API.TOKEN).Result; - await LocalStorage.SetItemAsync("data", originalData); - } } }