102 lines
2.7 KiB
C#
102 lines
2.7 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using AntDesign;
|
|
using Microsoft.AspNetCore.Components;
|
|
using UserPointManagement.Application.Services;
|
|
using UserPointManagement.Model.Dtos.User;
|
|
using UserPointManagement.Model.Entities;
|
|
|
|
namespace UserPointManagement.Web.Pages.UserManagement;
|
|
|
|
public class UserManagementBase : ComponentBase
|
|
{
|
|
[Inject] private IUserService _userService { get; set; }
|
|
|
|
protected IDictionary<int, (bool edit, User data)> editCache =
|
|
new Dictionary<int, (bool edit, User data)>();
|
|
|
|
protected List<User> _users;
|
|
protected int _pageIndex = 1;
|
|
protected int _pageSize = 20;
|
|
protected int _total = 0;
|
|
protected bool _loading;
|
|
private string _searchValue;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
await RefreshTable();
|
|
}
|
|
|
|
protected async Task OnSearch(string arg)
|
|
{
|
|
_searchValue = arg;
|
|
_pageIndex = 1;
|
|
await RefreshTable();
|
|
}
|
|
|
|
protected async Task RefreshTable()
|
|
{
|
|
_loading = true;
|
|
var res = await _userService.GetUsers(new GetUserDto()
|
|
{
|
|
Keyword = _searchValue,
|
|
PageIndex = _pageIndex,
|
|
PageSize = _pageSize
|
|
});
|
|
|
|
_users = res.Items;
|
|
_total = res.TotalCount;
|
|
|
|
_users.ForEach(item =>
|
|
{
|
|
editCache[item.Id] = (false, item);
|
|
});
|
|
|
|
_loading = false;
|
|
}
|
|
|
|
protected async Task OnPageIndexChanged(PaginationEventArgs args)
|
|
{
|
|
_pageIndex = args.Page;
|
|
_pageSize = args.PageSize;
|
|
await RefreshTable();
|
|
}
|
|
|
|
protected async Task OnPageSizeChange(PaginationEventArgs args)
|
|
{
|
|
_pageIndex = args.Page;
|
|
_pageSize = args.PageSize;
|
|
await RefreshTable();
|
|
}
|
|
|
|
protected async Task Delete(int userId)
|
|
{
|
|
await _userService.DeleteUser(userId).ConfigureAwait(false);
|
|
await RefreshTable();
|
|
}
|
|
|
|
protected void startEdit(int id)
|
|
{
|
|
var data = editCache[id];
|
|
editCache[id] = (true, data.data ); // add a copy in cache
|
|
}
|
|
|
|
protected void cancelEdit(int id)
|
|
{
|
|
var data = _users.FirstOrDefault(item => item.Id == id);
|
|
editCache[id] = (false, data); // recovery
|
|
}
|
|
|
|
protected async Task saveEdit(int id)
|
|
{
|
|
var index = _users.FindIndex(item => item.Id == id);
|
|
_users[index] = editCache[id].data; // apply the copy to data source
|
|
await _userService.ModifyUser(editCache[id].data.Id, new ModifyUserDto()
|
|
{
|
|
Mobile = editCache[id].data.Mobile,
|
|
Name = editCache[id].data.Name,
|
|
}).ConfigureAwait(false);
|
|
editCache[id] = (false, _users[index]); // don't affect rows in editing
|
|
}
|
|
} |