Mise à jour de 'WF-WebAdmin/WF-WebAdmin/Pages/ModifQuiz.razor.cs'
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
f6bd5d2fc1
commit
c358e55aa2
@ -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<ModifQuiz> Logger { get; set; }
|
||||
|
||||
[Inject]
|
||||
public IStringLocalizer<ModifQuiz> Localizer { get; set; }
|
||||
|
||||
[Inject]
|
||||
public IQuizService QuizService { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="e">The event arguments containing pagination details such as page size and page number.</param>
|
||||
private async Task OnReadData(DataGridReadDataEventArgs<Quiz> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="quiz">The quiz object that was clicked for editing.</param>
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 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`.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="q">The quiz to be deleted.</param>
|
||||
private void OnDelete(Quiz q)
|
||||
{
|
||||
// Set the selected quiz to the one passed in
|
||||
selectedQuiz = q;
|
||||
|
||||
// Show the delete confirmation popup
|
||||
showPopupDelete = true;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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<ModifQuiz> Logger { get; set; }
|
||||
|
||||
[Inject]
|
||||
public IStringLocalizer<ModifQuiz> Localizer { get; set; }
|
||||
|
||||
[Inject]
|
||||
public IQuizService QuizService { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="e">The event arguments containing pagination details such as page size and page number.</param>
|
||||
private async Task OnReadData(DataGridReadDataEventArgs<Quiz> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="quiz">The quiz object that was clicked for editing.</param>
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 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`.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="q">The quiz to be deleted.</param>
|
||||
private void OnDelete(Quiz q)
|
||||
{
|
||||
// Set the selected quiz to the one passed in
|
||||
selectedQuiz = q;
|
||||
|
||||
// Show the delete confirmation popup
|
||||
showPopupDelete = true;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue