|
1 | 1 | @using Humanizer |
2 | 2 | @using Microsoft.AspNetCore.Identity |
3 | 3 | @using System.Security.Claims |
| 4 | +@using System.Threading |
4 | 5 | @using Blazorise.Extensions |
5 | 6 | @using Blazorise |
6 | 7 | @using Blazorise.DataGrid |
|
11 | 12 | <Row> |
12 | 13 | <Column ColumnSize="ColumnSize.Is12"> |
13 | 14 | <DataGrid TItem="ApplicationUser" |
| 15 | + @ref="_usersDataGrid" |
14 | 16 | Data="@_users" |
| 17 | + ReadData="@OnReadData" |
| 18 | + TotalItems="@_totalItems" |
15 | 19 | Editable="true" |
16 | 20 | EditMode="DataGridEditMode.Popup" |
17 | 21 | Responsive="true" |
|
24 | 28 | ShowPager="true" |
25 | 29 | ShowPageSizes="true" |
26 | 30 | PageSize="25" |
27 | | - Filterable="true" |
| 31 | + Filterable="false" |
28 | 32 | ShowValidationFeedback="true" |
29 | 33 | ShowValidationsSummary="false" |
30 | 34 | UseValidation="true"> |
|
56 | 60 | </DataGridCommandColumn> |
57 | 61 | <DataGridColumn TItem="ApplicationUser" Editable="true" Field="@nameof(ApplicationUser.UserName)" Caption="Username" Sortable="false" Displayable="@IsColumnVisible(UsersColumnName.Username)" Filterable="false"> |
58 | 62 | <EditTemplate> |
59 | | - <Validation Validator="@((ValidatorEventArgs obj) => @ValidationHelper.ValidateUsername(obj, _users, context.Item.Id))"> |
| 63 | + <Validation AsyncValidator="(args, token) => ValidateUsernameAsync(args, token, context.Item.Id)"> |
60 | 64 | <TextEdit Text="@((string)context.CellValue)" TextChanged="(text) => { context.CellValue = text; }"> |
61 | 65 | <Feedback> |
62 | 66 | <ValidationError/> |
|
121 | 125 | @attribute [Authorize(Roles = "Superadmin")] |
122 | 126 | @code { |
123 | 127 | private List<ApplicationUser> _users = new(); |
| 128 | + private DataGrid<ApplicationUser>? _usersDataGrid; |
| 129 | + private int _totalItems; |
124 | 130 |
|
125 | 131 | [CascadingParameter] |
126 | 132 | private ApplicationUser? LoggedUser { get; set; } |
|
151 | 157 | { |
152 | 158 | if (LoggedUser != null) |
153 | 159 | { |
154 | | - await GetData(); |
| 160 | + await LoadNodesList(); |
155 | 161 | } |
156 | 162 | } |
157 | 163 |
|
158 | | - private async Task GetData() |
| 164 | + private async Task LoadNodesList() |
159 | 165 | { |
| 166 | + _nodesList = await NodeRepository.GetAllManagedByNodeGuard(); |
| 167 | + } |
160 | 168 |
|
161 | | - _users = await ApplicationUserRepository.GetAll(true); |
| 169 | + private async Task OnReadData(DataGridReadDataEventArgs<ApplicationUser> e) |
| 170 | + { |
| 171 | + if (LoggedUser == null) return; |
162 | 172 |
|
163 | | - _nodesList = await NodeRepository.GetAllManagedByNodeGuard(); |
| 173 | + var (users, totalCount) = await ApplicationUserRepository.GetPaginatedAsync( |
| 174 | + e.Page, |
| 175 | + e.PageSize, |
| 176 | + includeBanned: true); |
164 | 177 |
|
| 178 | + _users = users; |
| 179 | + _totalItems = totalCount; |
165 | 180 | } |
166 | 181 |
|
167 | 182 | protected override async Task OnAfterRenderAsync(bool firstRender) |
|
218 | 233 | AuditObjectType.User, |
219 | 234 | arg.Item.Id, |
220 | 235 | new { Username = arg.Item.UserName, Roles = string.Join(", ", _selectedRoles) }); |
221 | | - await GetData(); |
| 236 | + if (_usersDataGrid != null) |
| 237 | + { |
| 238 | + await _usersDataGrid.Reload(); |
| 239 | + } |
222 | 240 | } |
223 | 241 | else |
224 | 242 | { |
|
258 | 276 | else |
259 | 277 | { |
260 | 278 | ToastService.ShowSuccess("Success"); |
261 | | - await GetData(); |
| 279 | + if (_usersDataGrid != null) |
| 280 | + { |
| 281 | + await _usersDataGrid.Reload(); |
| 282 | + } |
262 | 283 | } |
263 | 284 | } |
264 | 285 | } |
|
306 | 327 | new { Username = arg.Item.UserName }); |
307 | 328 | } |
308 | 329 |
|
309 | | - await GetData(); |
| 330 | + if (_usersDataGrid != null) |
| 331 | + { |
| 332 | + await _usersDataGrid.Reload(); |
| 333 | + } |
310 | 334 | } |
311 | 335 |
|
312 | 336 | private void NewItemDefaultSetter(ApplicationUser obj) |
|
390 | 414 | new { Username = contextItem.UserName }); |
391 | 415 | } |
392 | 416 |
|
393 | | - await GetData(); |
| 417 | + if (_usersDataGrid != null) |
| 418 | + { |
| 419 | + await _usersDataGrid.Reload(); |
| 420 | + } |
394 | 421 | } |
395 | 422 | } |
396 | 423 |
|
|
422 | 449 | new { Username = contextItem.UserName }); |
423 | 450 | } |
424 | 451 |
|
425 | | - await GetData(); |
426 | | - } } |
| 452 | + if (_usersDataGrid != null) |
| 453 | + { |
| 454 | + await _usersDataGrid.Reload(); |
| 455 | + } |
| 456 | + } |
| 457 | + } |
| 458 | + |
| 459 | + private async Task ValidateUsernameAsync(ValidatorEventArgs obj, CancellationToken cancellationToken, string currentUserId) |
| 460 | + { |
| 461 | + obj.Status = ValidationStatus.Success; |
| 462 | + if (string.IsNullOrWhiteSpace((string)obj.Value)) |
| 463 | + { |
| 464 | + obj.ErrorText = "The Username cannot be empty"; |
| 465 | + obj.Status = ValidationStatus.Error; |
| 466 | + return; |
| 467 | + } |
| 468 | + |
| 469 | + var existingUser = await ApplicationUserRepository.GetByUsername(obj.Value.ToString()); |
| 470 | + if (existingUser != null && existingUser.Id != currentUserId) |
| 471 | + { |
| 472 | + obj.ErrorText = "A user with the same username already exists"; |
| 473 | + obj.Status = ValidationStatus.Error; |
| 474 | + } |
| 475 | + } |
427 | 476 |
|
428 | 477 | private void ValidateRole(ValidatorEventArgs obj) |
429 | 478 | { |
|
0 commit comments