Mise à jour de 'WF-WebAdmin/WF-WebAdmin/Pages/DeleteUser.razor.cs'
continuous-integration/drone/push Build is failing Details

master
parent f6936c7caa
commit 6b6a859982

@ -1,7 +1,7 @@
using Blazorise.DataGrid; using Blazorise.DataGrid;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Configuration.UserSecrets; using Microsoft.Extensions.Configuration.UserSecrets;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
using System.Collections.Generic; using System.Collections.Generic;
using WF_WebAdmin.Model; using WF_WebAdmin.Model;
@ -11,12 +11,12 @@ namespace WF_WebAdmin.Pages
{ {
public partial class DeleteUser public partial class DeleteUser
{ {
[Inject] [Inject]
public ILogger<DeleteUser> Logger { get; set; } public ILogger<DeleteUser> Logger { get; set; }
private List<User> users;
private List<User> users;
private bool showDeletePopup = false;
private bool showModifyPopup = false; private bool showModifyPopup = false;
private User userToDelete = null; private User userToDelete = null;
private User selectedUser; private User selectedUser;
@ -37,180 +37,180 @@ namespace WF_WebAdmin.Pages
[Inject] [Inject]
private IUserService userService { get; set; } private IUserService userService { get; set; }
[Inject] [Inject]
public IStringLocalizer<DeleteUser> Localizer { get; set; } public IStringLocalizer<DeleteUser> Localizer { get; set; }
/// <summary> /// <summary>
/// This method is called when the component is initialized. /// This method is called when the component is initialized.
/// It is an asynchronous method that retrieves a list of users from the user service. /// It is an asynchronous method that retrieves a list of users from the user service.
/// The method fetches a subset of users with a specified maximum value and page number (1 in this case). /// The method fetches a subset of users with a specified maximum value and page number (1 in this case).
/// </summary> /// </summary>
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
// Retrieve a list of users using the user service. The number of users and page number are specified. // Retrieve a list of users using the user service. The number of users and page number are specified.
// MaxValue determines how many users to retrieve, and '1' refers to the first page of results. // MaxValue determines how many users to retrieve, and '1' refers to the first page of results.
users = await userService.getSomeUser(MaxValue, 1); users = await userService.getSomeUser(MaxValue, 1);
} }
/// <summary> /// <summary>
/// Handles the event when data is read in the data grid. /// Handles the event when data is read in the data grid.
/// This method is triggered during pagination or when data is loaded into the grid. /// This method is triggered during pagination or when data is loaded into the grid.
/// It asynchronously fetches a page of users based on the requested page size and page number, /// It asynchronously fetches a page of users based on the requested page size and page number,
/// and updates the list of users and total item count if the operation is not cancelled. /// and updates the list of users and total item count if the operation is not cancelled.
/// </summary> /// </summary>
/// <param name="e">The event arguments containing pagination details (page size and page number) and a cancellation token.</param> /// <param name="e">The event arguments containing pagination details (page size and page number) and a cancellation token.</param>
private async Task OnReadData(DataGridReadDataEventArgs<User> e) private async Task OnReadData(DataGridReadDataEventArgs<User> e)
{ {
// If the cancellation token is requested, exit the method without processing the request. // If the cancellation token is requested, exit the method without processing the request.
if (e.CancellationToken.IsCancellationRequested) if (e.CancellationToken.IsCancellationRequested)
{ {
return; return;
} }
// Fetch a page of users from the user service using the page size and page number provided by the event arguments. // Fetch a page of users from the user service using the page size and page number provided by the event arguments.
var response = await userService.getSomeUser(e.PageSize, e.Page); var response = await userService.getSomeUser(e.PageSize, e.Page);
// If the operation is not cancelled, update the total number of users and the list of users. // If the operation is not cancelled, update the total number of users and the list of users.
if (!e.CancellationToken.IsCancellationRequested) if (!e.CancellationToken.IsCancellationRequested)
{ {
totalItem = await userService.getNbUser(); // Get the total number of users totalItem = await userService.getNbUser(); // Get the total number of users
users = new List<User>(response.ToArray()); // Store the retrieved users in the users list users = new List<User>(response.ToArray()); // Store the retrieved users in the users list
page = e.Page; // Update the current page number page = e.Page; // Update the current page number
} }
} }
// ------- Popup remove user ------- // ------- Popup remove user -------
/// <summary> /// <summary>
/// Displays a confirmation popup to confirm the deletion of a user. /// Displays a confirmation popup to confirm the deletion of a user.
/// This method is triggered when the user intends to delete a user, /// This method is triggered when the user intends to delete a user,
/// and it sets the user to be deleted and shows the confirmation popup. /// and it sets the user to be deleted and shows the confirmation popup.
/// </summary> /// </summary>
/// <param name="user">The user to be deleted, which is passed to the method for confirmation.</param> /// <param name="user">The user to be deleted, which is passed to the method for confirmation.</param>
private void ShowConfirmation(User user) private void ShowConfirmation(User user)
{ {
// Set the user to be deleted and show the confirmation popup. // Set the user to be deleted and show the confirmation popup.
userToDelete = user; // Store the user to be deleted in a variable userToDelete = user; // Store the user to be deleted in a variable
showPopupDelete = true; // Display the confirmation popup showPopupDelete = true; // Display the confirmation popup
} }
/// <summary> /// <summary>
/// Displays a confirmation popup for modifying a user's information. /// Displays a confirmation popup for modifying a user's information.
/// This method is triggered when the user intends to modify a specific user's data, /// This method is triggered when the user intends to modify a specific user's data,
/// and it sets the selected user and shows the modification confirmation popup. /// and it sets the selected user and shows the modification confirmation popup.
/// </summary> /// </summary>
/// <param name="user">The user whose information is to be modified, passed to the method for confirmation.</param> /// <param name="user">The user whose information is to be modified, passed to the method for confirmation.</param>
private void ShowModifyConfirmation(User user) private void ShowModifyConfirmation(User user)
{ {
// Set the selected user and show the modification confirmation popup. // Set the selected user and show the modification confirmation popup.
selectedUser = user; // Store the user to be modified selectedUser = user; // Store the user to be modified
showModifyPopup = true; // Display the confirmation popup for modification showModifyPopup = true; // Display the confirmation popup for modification
} }
/// <summary> /// <summary>
/// Removes the specified user from the system. /// Removes the specified user from the system.
/// This method is triggered when the user confirms the deletion of a user. /// This method is triggered when the user confirms the deletion of a user.
/// It calls the user service to remove the user, closes the confirmation popup, /// It calls the user service to remove the user, closes the confirmation popup,
/// and then refreshes the list of users by fetching the updated data. /// and then refreshes the list of users by fetching the updated data.
/// </summary> /// </summary>
private async Task RemoveUser() private async Task RemoveUser()
{ {
// Check if there is a user to delete // Check if there is a user to delete
if (userToDelete != null) if (userToDelete != null)
{ {
// Remove the selected user from the system using the user service // Remove the selected user from the system using the user service
LoggerSaveStub.Log(Logger, LogLevel.Information, $"Delete user {userToDelete.Name}"); LoggerSaveStub.Log(Logger, LogLevel.Information, $"Delete user {userToDelete.Name}");
await userService.removeUser(userToDelete); await userService.removeUser(userToDelete);
// Close the confirmation popup after the deletion // Close the confirmation popup after the deletion
ClosePopup(); ClosePopup();
// Refresh the list of users by fetching the updated data from the user service // Refresh the list of users by fetching the updated data from the user service
var response = await userService.getSomeUser(MaxValue, page); var response = await userService.getSomeUser(MaxValue, page);
// Update the users list with the latest data // Update the users list with the latest data
users = new List<User>(response.ToArray()); users = new List<User>(response.ToArray());
} }
} }
/// <summary> /// <summary>
/// Modifies the selected user's information. /// Modifies the selected user's information.
/// This method is triggered when the user confirms the modification of a user's details. /// This method is triggered when the user confirms the modification of a user's details.
/// It calls the user service to update the user's information and then closes the modification popup. /// It calls the user service to update the user's information and then closes the modification popup.
/// </summary> /// </summary>
private async Task ModifyUser() private async Task ModifyUser()
{ {
// Update the selected user's information using the user service // Update the selected user's information using the user service
LoggerSaveStub.Log(Logger, LogLevel.Information, $"Modification of user {selectedUser.Name}"); LoggerSaveStub.Log(Logger, LogLevel.Information, $"Modification of user {selectedUser.Name}");
await userService.updateUser(selectedUser); await userService.updateUser(selectedUser);
// Close the modification popup after the update is complete // Close the modification popup after the update is complete
ClosePopup(); ClosePopup();
} }
/// <summary> /// <summary>
/// Closes all open popups in the UI by setting their visibility flags to false. /// Closes all open popups in the UI by setting their visibility flags to false.
/// This method is typically called after an action (like deleting or modifying a user) /// This method is typically called after an action (like deleting or modifying a user)
/// to hide any active popups and reset the UI state. /// to hide any active popups and reset the UI state.
/// </summary> /// </summary>
private void ClosePopup() private void ClosePopup()
{ {
// Set all popup visibility flags to false to hide the popups // Set all popup visibility flags to false to hide the popups
showDeletePopup = false; // Close the delete confirmation popup showDeletePopup = false; // Close the delete confirmation popup
showModifyPopup = false; // Close the modify confirmation popup showModifyPopup = false; // Close the modify confirmation popup
showPopupDelete = false; // Close any additional delete popups showPopupDelete = false; // Close any additional delete popups
showPopupAdmin = false; // Close the admin-related popup (if any) showPopupAdmin = false; // Close the admin-related popup (if any)
} }
// ------- Popup admin ------- // ------- Popup admin -------
/// <summary> /// <summary>
/// Displays a confirmation popup to confirm the promotion of a user to admin status. /// Displays a confirmation popup to confirm the promotion of a user to admin status.
/// This method is triggered when the user intends to promote a specific user to admin. /// This method is triggered when the user intends to promote a specific user to admin.
/// It sets the selected user to be promoted and shows the confirmation popup for admin promotion. /// It sets the selected user to be promoted and shows the confirmation popup for admin promotion.
/// </summary> /// </summary>
/// <param name="user">The user to be promoted to admin, passed to the method for confirmation.</param> /// <param name="user">The user to be promoted to admin, passed to the method for confirmation.</param>
private void ShowConfirmationAdmin(User user) private void ShowConfirmationAdmin(User user)
{ {
// Set the user to be promoted to admin and show the confirmation popup. // Set the user to be promoted to admin and show the confirmation popup.
userToAdmin = user; // Store the user to be promoted userToAdmin = user; // Store the user to be promoted
showPopupAdmin = true; // Display the confirmation popup for admin promotion showPopupAdmin = true; // Display the confirmation popup for admin promotion
} }
/// <summary> /// <summary>
/// Toggles the admin status of the selected user. /// Toggles the admin status of the selected user.
/// This method checks the current admin status of the user, and if the user is not an admin, /// This method checks the current admin status of the user, and if the user is not an admin,
/// it promotes them to admin. If the user is already an admin, it demotes them. /// it promotes them to admin. If the user is already an admin, it demotes them.
/// After the change, the user's information is updated, and the confirmation popup is closed. /// After the change, the user's information is updated, and the confirmation popup is closed.
/// </summary> /// </summary>
private async Task setAdmin() private async Task setAdmin()
{ {
// Check if the user is not already an admin // Check if the user is not already an admin
if (!userToAdmin.IsAdmin) if (!userToAdmin.IsAdmin)
{ {
// Promote the user to admin // Promote the user to admin
LoggerSaveStub.Log(Logger, LogLevel.Information, $"User {userToAdmin.Name} is now administrator"); LoggerSaveStub.Log(Logger, LogLevel.Information, $"User {userToAdmin.Name} is now administrator");
userToAdmin.IsAdmin = true; userToAdmin.IsAdmin = true;
await userService.updateUser(userToAdmin); // Update the user status in the service await userService.updateUser(userToAdmin); // Update the user status in the service
ClosePopup(); // Close the confirmation popup ClosePopup(); // Close the confirmation popup
} }
else else
{ {
// Demote the user from admin to normal user // Demote the user from admin to normal user
LoggerSaveStub.Log(Logger, LogLevel.Information, $"User {userToAdmin.Name} is no longer an administator"); LoggerSaveStub.Log(Logger, LogLevel.Information, $"User {userToAdmin.Name} is no longer an administator");
userToAdmin.IsAdmin = false; userToAdmin.IsAdmin = false;
await userService.updateUser(userToAdmin); // Update the user status in the service await userService.updateUser(userToAdmin); // Update the user status in the service
ClosePopup(); // Close the confirmation popup ClosePopup(); // Close the confirmation popup
} }
} }

Loading…
Cancel
Save