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 Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Configuration.UserSecrets;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Localization;
using System.Collections.Generic;
using WF_WebAdmin.Model;
@ -11,12 +11,12 @@ namespace WF_WebAdmin.Pages
{
public partial class DeleteUser
{
[Inject]
public ILogger<DeleteUser> Logger { get; set; }
[Inject]
public ILogger<DeleteUser> Logger { get; set; }
private List<User> users;
private List<User> users;
private bool showDeletePopup = false;
private bool showModifyPopup = false;
private User userToDelete = null;
private User selectedUser;
@ -37,180 +37,180 @@ namespace WF_WebAdmin.Pages
[Inject]
private IUserService userService { get; set; }
[Inject]
[Inject]
public IStringLocalizer<DeleteUser> Localizer { get; set; }
/// <summary>
/// This method is called when the component is initialized.
/// 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).
/// </summary>
/// <summary>
/// This method is called when the component is initialized.
/// 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).
/// </summary>
protected override async Task OnInitializedAsync()
{
// 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.
// 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.
users = await userService.getSomeUser(MaxValue, 1);
}
/// <summary>
/// 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.
/// 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.
/// </summary>
/// <param name="e">The event arguments containing pagination details (page size and page number) and a cancellation token.</param>
/// <summary>
/// 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.
/// 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.
/// </summary>
/// <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)
{
// 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)
{
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);
// 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)
{
totalItem = await userService.getNbUser(); // Get the total number of users
users = new List<User>(response.ToArray()); // Store the retrieved users in the users list
page = e.Page; // Update the current page number
totalItem = await userService.getNbUser(); // Get the total number of users
users = new List<User>(response.ToArray()); // Store the retrieved users in the users list
page = e.Page; // Update the current page number
}
}
// ------- Popup remove user -------
/// <summary>
/// Displays a confirmation popup to confirm the deletion of 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.
/// </summary>
/// <param name="user">The user to be deleted, which is passed to the method for confirmation.</param>
/// <summary>
/// Displays a confirmation popup to confirm the deletion of 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.
/// </summary>
/// <param name="user">The user to be deleted, which is passed to the method for confirmation.</param>
private void ShowConfirmation(User user)
{
// Set the user to be deleted and show the confirmation popup.
userToDelete = user; // Store the user to be deleted in a variable
showPopupDelete = true; // Display 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
showPopupDelete = true; // Display the confirmation popup
}
/// <summary>
/// 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,
/// and it sets the selected user and shows the modification confirmation popup.
/// </summary>
/// <param name="user">The user whose information is to be modified, passed to the method for confirmation.</param>
/// <summary>
/// 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,
/// and it sets the selected user and shows the modification confirmation popup.
/// </summary>
/// <param name="user">The user whose information is to be modified, passed to the method for confirmation.</param>
private void ShowModifyConfirmation(User user)
{
// Set the selected user and show the modification confirmation popup.
selectedUser = user; // Store the user to be modified
showModifyPopup = true; // Display the confirmation popup for modification
// Set the selected user and show the modification confirmation popup.
selectedUser = user; // Store the user to be modified
showModifyPopup = true; // Display the confirmation popup for modification
}
/// <summary>
/// Removes the specified user from the system.
/// 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,
/// and then refreshes the list of users by fetching the updated data.
/// </summary>
/// <summary>
/// Removes the specified user from the system.
/// 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,
/// and then refreshes the list of users by fetching the updated data.
/// </summary>
private async Task RemoveUser()
{
// Check if there is a user to delete
// Check if there is a user to delete
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}");
await userService.removeUser(userToDelete);
// Close the confirmation popup after the deletion
// Close the confirmation popup after the deletion
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);
// Update the users list with the latest data
// Update the users list with the latest data
users = new List<User>(response.ToArray());
}
}
/// <summary>
/// Modifies the selected user's information.
/// 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.
/// </summary>
/// <summary>
/// Modifies the selected user's information.
/// 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.
/// </summary>
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}");
await userService.updateUser(selectedUser);
// Close the modification popup after the update is complete
// Close the modification popup after the update is complete
ClosePopup();
}
/// <summary>
/// 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)
/// to hide any active popups and reset the UI state.
/// </summary>
/// <summary>
/// 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)
/// to hide any active popups and reset the UI state.
/// </summary>
private void ClosePopup()
{
// Set all popup visibility flags to false to hide the popups
showDeletePopup = false; // Close the delete confirmation popup
showModifyPopup = false; // Close the modify confirmation popup
showPopupDelete = false; // Close any additional delete popups
showPopupAdmin = false; // Close the admin-related popup (if any)
// Set all popup visibility flags to false to hide the popups
showDeletePopup = false; // Close the delete confirmation popup
showModifyPopup = false; // Close the modify confirmation popup
showPopupDelete = false; // Close any additional delete popups
showPopupAdmin = false; // Close the admin-related popup (if any)
}
// ------- Popup admin -------
/// <summary>
/// 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.
/// It sets the selected user to be promoted and shows the confirmation popup for admin promotion.
/// </summary>
/// <param name="user">The user to be promoted to admin, passed to the method for confirmation.</param>
/// <summary>
/// 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.
/// It sets the selected user to be promoted and shows the confirmation popup for admin promotion.
/// </summary>
/// <param name="user">The user to be promoted to admin, passed to the method for confirmation.</param>
private void ShowConfirmationAdmin(User user)
{
// Set the user to be promoted to admin and show the confirmation popup.
userToAdmin = user; // Store the user to be promoted
showPopupAdmin = true; // Display the confirmation popup for admin promotion
// Set the user to be promoted to admin and show the confirmation popup.
userToAdmin = user; // Store the user to be promoted
showPopupAdmin = true; // Display the confirmation popup for admin promotion
}
/// <summary>
/// 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,
/// 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.
/// </summary>
/// <summary>
/// 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,
/// 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.
/// </summary>
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)
{
// Promote the user to admin
LoggerSaveStub.Log(Logger, LogLevel.Information, $"User {userToAdmin.Name} is now administrator");
// Promote the user to admin
LoggerSaveStub.Log(Logger, LogLevel.Information, $"User {userToAdmin.Name} is now administrator");
userToAdmin.IsAdmin = true;
await userService.updateUser(userToAdmin); // Update the user status in the service
ClosePopup(); // Close the confirmation popup
await userService.updateUser(userToAdmin); // Update the user status in the service
ClosePopup(); // Close the confirmation popup
}
else
{
// Demote the user from admin to normal user
LoggerSaveStub.Log(Logger, LogLevel.Information, $"User {userToAdmin.Name} is no longer an administator");
// Demote the user from admin to normal user
LoggerSaveStub.Log(Logger, LogLevel.Information, $"User {userToAdmin.Name} is no longer an administator");
userToAdmin.IsAdmin = false;
await userService.updateUser(userToAdmin); // Update the user status in the service
ClosePopup(); // Close the confirmation popup
await userService.updateUser(userToAdmin); // Update the user status in the service
ClosePopup(); // Close the confirmation popup
}
}

Loading…
Cancel
Save