From 92e2de76ba64e664374fdf0df09116439cecde7a Mon Sep 17 00:00:00 2001 From: Leendert de Borst Date: Fri, 11 Oct 2024 15:18:13 +0200 Subject: [PATCH] Refactor (#175) --- .../View/Components/EmailClaimTable.razor | 17 ++++-------- .../View/Components/RefreshTokenTable.razor | 21 +++++---------- .../Users/View/Components/VaultTable.razor | 27 +++++++------------ .../Tables/SortableTable.razor | 12 ++++++++- 4 files changed, 33 insertions(+), 44 deletions(-) diff --git a/src/AliasVault.Admin/Main/Pages/Users/View/Components/EmailClaimTable.razor b/src/AliasVault.Admin/Main/Pages/Users/View/Components/EmailClaimTable.razor index 06a1e6afd..14b79d16a 100644 --- a/src/AliasVault.Admin/Main/Pages/Users/View/Components/EmailClaimTable.razor +++ b/src/AliasVault.Admin/Main/Pages/Users/View/Components/EmailClaimTable.razor @@ -16,7 +16,7 @@ /// Gets or sets the list of email claims to display. /// [Parameter] - public List EmailClaimList { get; set; } = new(); + public List EmailClaimList { get; set; } = []; private string SortColumn { get; set; } = "CreatedAt"; private SortDirection SortDirection { get; set; } = SortDirection.Descending; @@ -36,21 +36,14 @@ StateHasChanged(); } - private IEnumerable SortList(List emailClaims, string sortColumn, SortDirection sortDirection) + private static IEnumerable SortList(List emailClaims, string sortColumn, SortDirection sortDirection) { return sortColumn switch { - "Id" => SortProperty(emailClaims, e => e.Id, sortDirection), - "CreatedAt" => SortProperty(emailClaims, e => e.CreatedAt, sortDirection), - "Address" => SortProperty(emailClaims, e => e.Address, sortDirection), + "Id" => SortableTable.SortListByProperty(emailClaims, e => e.Id, sortDirection), + "CreatedAt" => SortableTable.SortListByProperty(emailClaims, e => e.CreatedAt, sortDirection), + "Address" => SortableTable.SortListByProperty(emailClaims, e => e.Address, sortDirection), _ => emailClaims }; } - - private IEnumerable SortProperty(IEnumerable source, Func keySelector, SortDirection direction) - { - return direction == SortDirection.Ascending - ? source.OrderBy(keySelector) - : source.OrderByDescending(keySelector); - } } diff --git a/src/AliasVault.Admin/Main/Pages/Users/View/Components/RefreshTokenTable.razor b/src/AliasVault.Admin/Main/Pages/Users/View/Components/RefreshTokenTable.razor index 022785ae0..49796e568 100644 --- a/src/AliasVault.Admin/Main/Pages/Users/View/Components/RefreshTokenTable.razor +++ b/src/AliasVault.Admin/Main/Pages/Users/View/Components/RefreshTokenTable.razor @@ -21,7 +21,7 @@ /// Gets or sets the list of refresh tokens to display. /// [Parameter] - public List RefreshTokenList { get; set; } = new(); + public List RefreshTokenList { get; set; } = []; /// /// Gets or sets the event callback to revoke a refresh token. @@ -50,26 +50,19 @@ StateHasChanged(); } - private IEnumerable SortList(List refreshTokens, string sortColumn, SortDirection sortDirection) + private static IEnumerable SortList(List refreshTokens, string sortColumn, SortDirection sortDirection) { return sortColumn switch { - "Id" => SortProperty(refreshTokens, r => r.Id, sortDirection), - "DeviceIdentifier" => SortProperty(refreshTokens, r => r.DeviceIdentifier, sortDirection), - "IpAddress" => SortProperty(refreshTokens, r => r.IpAddress, sortDirection), - "CreatedAt" => SortProperty(refreshTokens, r => r.CreatedAt, sortDirection), - "ExpireDate" => SortProperty(refreshTokens, r => r.ExpireDate, sortDirection), + "Id" => SortableTable.SortListByProperty(refreshTokens, r => r.Id, sortDirection), + "DeviceIdentifier" => SortableTable.SortListByProperty(refreshTokens, r => r.DeviceIdentifier, sortDirection), + "IpAddress" => SortableTable.SortListByProperty(refreshTokens, r => r.IpAddress, sortDirection), + "CreatedAt" => SortableTable.SortListByProperty(refreshTokens, r => r.CreatedAt, sortDirection), + "ExpireDate" => SortableTable.SortListByProperty(refreshTokens, r => r.ExpireDate, sortDirection), _ => refreshTokens }; } - private IEnumerable SortProperty(IEnumerable source, Func keySelector, SortDirection direction) - { - return direction == SortDirection.Ascending - ? source.OrderBy(keySelector) - : source.OrderByDescending(keySelector); - } - private async Task RevokeRefreshToken(AliasVaultUserRefreshToken entry) { await OnRevokeToken.InvokeAsync(entry); diff --git a/src/AliasVault.Admin/Main/Pages/Users/View/Components/VaultTable.razor b/src/AliasVault.Admin/Main/Pages/Users/View/Components/VaultTable.razor index 3a04328b2..ebccee2d3 100644 --- a/src/AliasVault.Admin/Main/Pages/Users/View/Components/VaultTable.razor +++ b/src/AliasVault.Admin/Main/Pages/Users/View/Components/VaultTable.razor @@ -38,7 +38,7 @@ /// Gets or sets the list of vaults to display. /// [Parameter] - public List VaultList { get; set; } = new(); + public List VaultList { get; set; } = []; /// /// Gets or sets the event callback to make a vault current. @@ -74,29 +74,22 @@ StateHasChanged(); } - private IEnumerable SortList(List vaults, string sortColumn, SortDirection sortDirection) + private static IEnumerable SortList(List vaults, string sortColumn, SortDirection sortDirection) { return sortColumn switch { - "Id" => SortProperty(vaults, v => v.Id, sortDirection), - "CreatedAt" => SortProperty(vaults, v => v.CreatedAt, sortDirection), - "UpdatedAt" => SortProperty(vaults, v => v.UpdatedAt, sortDirection), - "FileSize" => SortProperty(vaults, v => v.FileSize, sortDirection), - "Version" => SortProperty(vaults, v => v.Version, sortDirection), - "RevisionNumber" => SortProperty(vaults, v => v.RevisionNumber, sortDirection), - "CredentialsCount" => SortProperty(vaults, v => v.CredentialsCount, sortDirection), - "EmailClaimsCount" => SortProperty(vaults, v => v.EmailClaimsCount, sortDirection), + "Id" => SortableTable.SortListByProperty(vaults, v => v.Id, sortDirection), + "CreatedAt" => SortableTable.SortListByProperty(vaults, v => v.CreatedAt, sortDirection), + "UpdatedAt" => SortableTable.SortListByProperty(vaults, v => v.UpdatedAt, sortDirection), + "FileSize" => SortableTable.SortListByProperty(vaults, v => v.FileSize, sortDirection), + "Version" => SortableTable.SortListByProperty(vaults, v => v.Version, sortDirection), + "RevisionNumber" => SortableTable.SortListByProperty(vaults, v => v.RevisionNumber, sortDirection), + "CredentialsCount" => SortableTable.SortListByProperty(vaults, v => v.CredentialsCount, sortDirection), + "EmailClaimsCount" => SortableTable.SortListByProperty(vaults, v => v.EmailClaimsCount, sortDirection), _ => vaults }; } - private IEnumerable SortProperty(IEnumerable source, Func keySelector, SortDirection direction) - { - return direction == SortDirection.Ascending - ? source.OrderBy(keySelector) - : source.OrderByDescending(keySelector); - } - private static bool HasPasswordChanged(Vault current, Vault previous) { return current.Salt != previous.Salt || current.Verifier != previous.Verifier; diff --git a/src/Shared/AliasVault.RazorComponents/Tables/SortableTable.razor b/src/Shared/AliasVault.RazorComponents/Tables/SortableTable.razor index da326b62c..c0352bc23 100644 --- a/src/Shared/AliasVault.RazorComponents/Tables/SortableTable.razor +++ b/src/Shared/AliasVault.RazorComponents/Tables/SortableTable.razor @@ -66,6 +66,16 @@ /// [Parameter] public EventCallback<(string, SortDirection)> OnSortChanged { get; set; } + /// + /// Sorts the list of items by the specified column and direction. + /// + public static IEnumerable SortListByProperty(IEnumerable source, Func keySelector, SortDirection direction) + { + return direction == SortDirection.Ascending + ? source.OrderBy(keySelector) + : source.OrderByDescending(keySelector); + } + private async Task OnSort(string columnName) { if (SortColumn == columnName) @@ -82,4 +92,4 @@ await OnSortChanged.InvokeAsync((SortColumn, SortDirection)); } -} \ No newline at end of file +}