Code Smells from Pages/DeleteUser
continuous-integration/drone/push Build is passing Details

master
tomivt 2 months ago
parent cc616955bc
commit ccf7e35f39

@ -5,7 +5,7 @@
<h3>@Localizer["TitleUser"]</h3> <h3>@Localizer["TitleUser"]</h3>
@if(users == null) @if(_users == null)
{ {
<p><strong>@Localizer["UserNobody"]</strong></p> <p><strong>@Localizer["UserNobody"]</strong></p>
} }
@ -13,10 +13,10 @@
else else
{ {
<DataGrid TItem="User" <DataGrid TItem="User"
Data="@users" Data="@_users"
PageSize="@MaxValue" PageSize="@MaxValue"
ReadData="@OnReadData" ReadData="@OnReadData"
TotalItems="@totalItem" TotalItems="@_totalItem"
ShowPager ShowPager
Responsive> Responsive>
@ -29,7 +29,7 @@ else
<p>@Localizer["UserHere"]</p> <p>@Localizer["UserHere"]</p>
@foreach (var user in users) @foreach (var user in _users)
{ {
<div class="userDiv" id="@user.Id"> <div class="userDiv" id="@user.Id">
<img class="imgProfil" src="@user.Image" /> <img class="imgProfil" src="@user.Image" />
@ -74,7 +74,7 @@ else
</div> </div>
<!-- Fenêtre de confirmation de suppression --> <!-- Fenêtre de confirmation de suppression -->
@if (showPopupDelete) @if (_showPopupDelete)
{ {
<div class="divPopup"> <div class="divPopup">
<div class="contentPopup"> <div class="contentPopup">
@ -84,17 +84,17 @@ else
</div> </div>
</div> </div>
} }
@if (showModifyPopup) @if (_showModifyPopup)
{ {
<div class="divPopup"> <div class="divPopup">
<div class="contentPopup"> <div class="contentPopup">
<p>Modifier les informations de l'utilisateur :</p> <p>Modifier les informations de l'utilisateur :</p>
<label>Nom d'utilisateur:</label> <label>Nom d'utilisateur:</label>
<input type="text" @bind="selectedUser.Name"/> <input type="text" @bind="_selectedUser.Name"/>
<label>Email:</label> <label>Email:</label>
<input type="email" @bind="selectedUser.Email" /> <input type="email" @bind="_selectedUser.Email" />
<label>Image:</label> <label>Image:</label>
<input type="text" @bind="selectedUser.Image" /> <input type="text" @bind="_selectedUser.Image" />
<button @onclick="ModifyUser">Sauvegarder</button> <button @onclick="ModifyUser">Sauvegarder</button>
<button @onclick="ClosePopup">Annuler</button> <button @onclick="ClosePopup">Annuler</button>
</div> </div>
@ -103,7 +103,7 @@ else
} }
<!-- Fenêtre de confirmation d'ajout admin--> <!-- Fenêtre de confirmation d'ajout admin-->
@if (showPopupAdmin) @if (_showPopupAdmin)
{ {
<div class="divPopup"> <div class="divPopup">
<div class="contentPopup"> <div class="contentPopup">

@ -14,30 +14,30 @@ namespace WF_WebAdmin.Pages
[Inject] [Inject]
public ILogger<DeleteUser>? Logger { get; set; } public ILogger<DeleteUser>? Logger { get; set; }
private List<User> users; private List<User>? _users;
private bool showModifyPopup = false; private bool _showModifyPopup = false;
private User userToDelete = null; private User? _userToDelete = null;
private User selectedUser; private User? _selectedUser;
private bool showPopupDelete = false; private bool _showPopupDelete = false;
private bool showPopupAdmin = false; private bool _showPopupAdmin = false;
private User userToAdmin = null; private User? _userToAdmin = null;
private int MaxValue = 5; private const int MaxValue = 5;
private int totalItem; private int _totalItem;
private int page = 1; private int _page = 1;
[Inject] [Inject]
public HttpClient Http { get; set; } public HttpClient? Http { get; set; }
[Inject] [Inject]
public NavigationManager NavigationManager { get; set; } public NavigationManager? NavigationManager { get; set; }
[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; }
@ -50,7 +50,7 @@ namespace WF_WebAdmin.Pages
{ {
// 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);
} }
@ -71,14 +71,14 @@ namespace WF_WebAdmin.Pages
} }
// 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
} }
} }
@ -90,11 +90,11 @@ namespace WF_WebAdmin.Pages
/// 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
} }
@ -104,11 +104,11 @@ namespace WF_WebAdmin.Pages
/// 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
} }
@ -121,20 +121,20 @@ namespace WF_WebAdmin.Pages
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());
} }
} }
@ -147,8 +147,8 @@ namespace WF_WebAdmin.Pages
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();
@ -163,9 +163,9 @@ namespace WF_WebAdmin.Pages
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
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)
} }
@ -176,11 +176,11 @@ namespace WF_WebAdmin.Pages
/// 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
} }
@ -194,20 +194,20 @@ namespace WF_WebAdmin.Pages
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
} }
} }

@ -6,15 +6,15 @@ namespace WF_WebAdmin.Service
{ {
public Task removeUser(User user); public Task removeUser(User user);
public Task updateRole(User user); public Task updateRole(User? user);
public Task downgradeRole(User user); public Task downgradeRole(User? user);
public Task updateUser(User user); public Task updateUser(User? user);
public Task<List<User>> getAllUser(); public Task<List<User>> getAllUser();
public Task<List<User>> getSomeUser(int nb, int page); public Task<List<User>?> getSomeUser(int nb, int page);
public Task<User> getOneUser(int id); public Task<User> getOneUser(int id);

@ -55,7 +55,7 @@ public class UserServiceStub : IUserService
/// This method updates the `IsAdmin` property of the specified user to `true`, indicating that the user is an administrator. /// This method updates the `IsAdmin` property of the specified user to `true`, indicating that the user is an administrator.
/// It then calls the <see cref="updateUser"/> method to persist the updated user information. /// It then calls the <see cref="updateUser"/> method to persist the updated user information.
/// </remarks> /// </remarks>
public Task updateRole(User user) public Task updateRole(User? user)
{ {
user.IsAdmin = true; user.IsAdmin = true;
return updateUser(user); return updateUser(user);
@ -71,7 +71,7 @@ public class UserServiceStub : IUserService
/// This method updates the `IsAdmin` property of the specified user to `false`, removing their administrator status. /// This method updates the `IsAdmin` property of the specified user to `false`, removing their administrator status.
/// It then calls the <see cref="updateUser"/> method to persist the updated user information. /// It then calls the <see cref="updateUser"/> method to persist the updated user information.
/// </remarks> /// </remarks>
public Task downgradeRole(User user) public Task downgradeRole(User? user)
{ {
user.IsAdmin = false; user.IsAdmin = false;
return updateUser(user); return updateUser(user);
@ -113,7 +113,7 @@ public class UserServiceStub : IUserService
/// It returns the corresponding subset of users for the given page. If the page exceeds the available number of users, /// It returns the corresponding subset of users for the given page. If the page exceeds the available number of users,
/// it returns the last `nb` users available. /// it returns the last `nb` users available.
/// </remarks> /// </remarks>
public async Task<List<User>> getSomeUser(int nb, int page) public async Task<List<User>?> getSomeUser(int nb, int page)
{ {
var users = await getAllUser(); var users = await getAllUser();
if ((page - 1) * nb + nb > users.Count) if ((page - 1) * nb + nb > users.Count)
@ -177,7 +177,7 @@ public class UserServiceStub : IUserService
/// If a user with the given ID is found, it updates their details (Name, Email, Image, IsAdmin) based on the provided `user` object. /// If a user with the given ID is found, it updates their details (Name, Email, Image, IsAdmin) based on the provided `user` object.
/// After updating the user, the modified list of users is saved back to the JSON file using the <see cref="saveUsersJson"/> method. /// After updating the user, the modified list of users is saved back to the JSON file using the <see cref="saveUsersJson"/> method.
/// </remarks> /// </remarks>
public async Task updateUser(User user) public async Task updateUser(User? user)
{ {
var data = await getAllUser(); var data = await getAllUser();
var person = data.FirstOrDefault(p => p.Id == user.Id); var person = data.FirstOrDefault(p => p.Id == user.Id);

Loading…
Cancel
Save