From c358e55aa22a2075c2aeec49eadfbc3e98913fd3 Mon Sep 17 00:00:00 2001 From: Louis GUICHARD-MONTGUERS Date: Sat, 8 Feb 2025 19:02:06 +0100 Subject: [PATCH] =?UTF-8?q?Mise=20=C3=A0=20jour=20de=20'WF-WebAdmin/WF-Web?= =?UTF-8?q?Admin/Pages/ModifQuiz.razor.cs'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WF-WebAdmin/Pages/ModifQuiz.razor.cs | 324 +++++++++--------- 1 file changed, 162 insertions(+), 162 deletions(-) diff --git a/WF-WebAdmin/WF-WebAdmin/Pages/ModifQuiz.razor.cs b/WF-WebAdmin/WF-WebAdmin/Pages/ModifQuiz.razor.cs index 170cc53..1ac9887 100644 --- a/WF-WebAdmin/WF-WebAdmin/Pages/ModifQuiz.razor.cs +++ b/WF-WebAdmin/WF-WebAdmin/Pages/ModifQuiz.razor.cs @@ -1,162 +1,162 @@ -using Blazorise.DataGrid; -using Microsoft.AspNetCore.Components; -using Microsoft.Extensions.Localization; -using System.Security.Claims; -using WF_WebAdmin.Model; -using WF_WebAdmin.Service; - -namespace WF_WebAdmin.Pages -{ - public partial class ModifQuiz - { - private Quiz[] quiz; - - private int MaxValue = 5; - - private int totalItem; - - private bool showEditQuiz = false; - - private Quiz? selectedQuiz; - - private bool showPopupDelete = false; - - private int page = 1; - - [Inject] - public ILogger Logger { get; set; } - - [Inject] - public IStringLocalizer Localizer { get; set; } - - [Inject] - public IQuizService QuizService { get; set; } - - /// - /// Handles the data reading event for a data grid, fetching quiz data based on the specified page and page size. - /// This method makes an asynchronous call to retrieve a specific page of quizzes and updates the `quiz` list and pagination details. - /// If the cancellation token is requested, it exits early without making further calls or updates. - /// - /// The event arguments containing pagination details such as page size and page number. - private async Task OnReadData(DataGridReadDataEventArgs e) - { - // Check if the cancellation token has been requested - if (e.CancellationToken.IsCancellationRequested) - { - return; - } - - // Fetch the quiz data for the specified page and page size - var response = await QuizService.getSommeQuiz(e.PageSize, e.Page); - - // If cancellation hasn't been requested, process the data - if (!e.CancellationToken.IsCancellationRequested) - { - // Get the total number of quizzes for pagination purposes - totalItem = await QuizService.getNbQuiz(); - - // Update the quiz data for the current page - quiz = response.ToArray(); - - // Update the current page number - page = e.Page; - } - } - - /// - /// Handles the event when the "Edit" button is clicked for a quiz. - /// This method checks if a valid quiz is passed. If so, it sets the `selectedQuiz` to the clicked quiz and shows the quiz edit modal. - /// - /// The quiz object that was clicked for editing. - private void OnEditButtonClicked(Quiz quiz) - { - // If the quiz is null, return early - if (quiz == null) return; - - // Set the selected quiz to the one clicked by the user - selectedQuiz = quiz; - - // Show the modal or UI for editing the quiz - showEditQuiz = true; - } - - - /// - /// Closes the open popups and resets any related states. - /// This method hides the quiz edit popup, the delete confirmation popup, and resets the selected quiz to `null`. - /// - private void ClosePopup() - { - // Hide the edit quiz popup - showEditQuiz = false; - - // Hide the delete confirmation popup - showPopupDelete = false; - - // Reset the selected quiz to null - selectedQuiz = null; - } - - - - /// - /// Edits the selected quiz by updating it in the quiz service. - /// This method asynchronously sends the updated quiz data to the service for persistence. - /// After updating the quiz, it clears the selected quiz and closes any open popups. - /// - private async Task EditQuiz() - { - // Update the quiz in the service - LoggerSaveStub.Log(Logger, LogLevel.Information, $"Editing the question {selectedQuiz.Question}"); - await QuizService.updateQuiz(selectedQuiz); - - // Clear the selected quiz after successful update - selectedQuiz = null; - - // Close the popups after the edit operation - ClosePopup(); - } - - - /// - /// Handles the event when the delete action is triggered for a quiz. - /// This method sets the selected quiz to the one passed as a parameter and shows the delete confirmation popup. - /// - /// The quiz to be deleted. - private void OnDelete(Quiz q) - { - // Set the selected quiz to the one passed in - selectedQuiz = q; - - // Show the delete confirmation popup - showPopupDelete = true; - } - - - /// - /// Removes the selected quiz from the quiz service and updates the quiz list. - /// This method first checks if a quiz is selected, and if so, it deletes the quiz by calling the service. - /// After removal, it clears the `selectedQuiz`, updates the quiz list, and closes the delete confirmation popup. - /// - private async void RemoveQuote() - { - // Check if a quiz is selected for deletion - if (selectedQuiz != null) - { - // Remove the selected quiz from the service by its ID - LoggerSaveStub.Log(Logger, LogLevel.Information, $"Delete the question {selectedQuiz.Question}"); - await QuizService.removeQuiz(selectedQuiz.Id); - - // Clear the selected quiz after successful removal - selectedQuiz = null; - - // Update the quiz list by fetching the latest data - var response = await QuizService.getSommeQuiz(MaxValue, page); - quiz = response.ToArray(); - } - - // Close the delete confirmation popup - showPopupDelete = false; - } - } -} +using Blazorise.DataGrid; +using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.Localization; +using System.Security.Claims; +using WF_WebAdmin.Model; +using WF_WebAdmin.Service; + +namespace WF_WebAdmin.Pages +{ + public partial class ModifQuiz + { + private Quiz[] quiz; + + private int MaxValue = 5; + + private int totalItem; + + private bool showEditQuiz = false; + + private Quiz? selectedQuiz; + + private bool showPopupDelete = false; + + private int page = 1; + + [Inject] + public ILogger Logger { get; set; } + + [Inject] + public IStringLocalizer Localizer { get; set; } + + [Inject] + public IQuizService QuizService { get; set; } + + /// + /// Handles the data reading event for a data grid, fetching quiz data based on the specified page and page size. + /// This method makes an asynchronous call to retrieve a specific page of quizzes and updates the `quiz` list and pagination details. + /// If the cancellation token is requested, it exits early without making further calls or updates. + /// + /// The event arguments containing pagination details such as page size and page number. + private async Task OnReadData(DataGridReadDataEventArgs e) + { + // Check if the cancellation token has been requested + if (e.CancellationToken.IsCancellationRequested) + { + return; + } + + // Fetch the quiz data for the specified page and page size + var response = await QuizService.getSommeQuiz(e.PageSize, e.Page); + + // If cancellation hasn't been requested, process the data + if (!e.CancellationToken.IsCancellationRequested) + { + // Get the total number of quizzes for pagination purposes + totalItem = await QuizService.getNbQuiz(); + + // Update the quiz data for the current page + quiz = response.ToArray(); + + // Update the current page number + page = e.Page; + } + } + + /// + /// Handles the event when the "Edit" button is clicked for a quiz. + /// This method checks if a valid quiz is passed. If so, it sets the `selectedQuiz` to the clicked quiz and shows the quiz edit modal. + /// + /// The quiz object that was clicked for editing. + private void OnEditButtonClicked(Quiz quiz) + { + // If the quiz is null, return early + if (quiz == null) return; + + // Set the selected quiz to the one clicked by the user + selectedQuiz = quiz; + + // Show the modal or UI for editing the quiz + showEditQuiz = true; + } + + + /// + /// Closes the open popups and resets any related states. + /// This method hides the quiz edit popup, the delete confirmation popup, and resets the selected quiz to `null`. + /// + private void ClosePopup() + { + // Hide the edit quiz popup + showEditQuiz = false; + + // Hide the delete confirmation popup + showPopupDelete = false; + + // Reset the selected quiz to null + selectedQuiz = null; + } + + + + /// + /// Edits the selected quiz by updating it in the quiz service. + /// This method asynchronously sends the updated quiz data to the service for persistence. + /// After updating the quiz, it clears the selected quiz and closes any open popups. + /// + private async Task EditQuiz() + { + // Update the quiz in the service + LoggerSaveStub.Log(Logger, LogLevel.Information, $"Editing the question {selectedQuiz.Question}"); + await QuizService.updateQuiz(selectedQuiz); + + // Clear the selected quiz after successful update + selectedQuiz = null; + + // Close the popups after the edit operation + ClosePopup(); + } + + + /// + /// Handles the event when the delete action is triggered for a quiz. + /// This method sets the selected quiz to the one passed as a parameter and shows the delete confirmation popup. + /// + /// The quiz to be deleted. + private void OnDelete(Quiz q) + { + // Set the selected quiz to the one passed in + selectedQuiz = q; + + // Show the delete confirmation popup + showPopupDelete = true; + } + + + /// + /// Removes the selected quiz from the quiz service and updates the quiz list. + /// This method first checks if a quiz is selected, and if so, it deletes the quiz by calling the service. + /// After removal, it clears the `selectedQuiz`, updates the quiz list, and closes the delete confirmation popup. + /// + private async Task RemoveQuote() + { + // Check if a quiz is selected for deletion + if (selectedQuiz != null) + { + // Remove the selected quiz from the service by its ID + LoggerSaveStub.Log(Logger, LogLevel.Information, $"Delete the question {selectedQuiz.Question}"); + await QuizService.removeQuiz(selectedQuiz.Id); + + // Clear the selected quiz after successful removal + selectedQuiz = null; + + // Update the quiz list by fetching the latest data + var response = await QuizService.getSommeQuiz(MaxValue, page); + quiz = response.ToArray(); + } + + // Close the delete confirmation popup + showPopupDelete = false; + } + } +}