From cdfacd20efaa5894f49d35eb7fd70b702214613a Mon Sep 17 00:00:00 2001 From: Mukesh Murugan Date: Fri, 19 Mar 2021 20:42:52 +0530 Subject: [PATCH] added interceptor to main component . removed others. #20 --- .../Pages/Catalog/AddEditBrandModal.razor.cs | 7 +- .../Catalog/AddEditProductModal.razor.cs | 4 +- .../Client/Pages/Catalog/Brands.razor.cs | 6 +- .../Client/Pages/Catalog/Products.razor.cs | 10 +-- .../Client/Pages/Content/Dashboard.razor.cs | 5 +- .../Client/Pages/Identity/Profile.razor.cs | 3 - .../Pages/Identity/RegisterUserModal.razor.cs | 30 +------- .../Client/Pages/Identity/RoleModal.razor.cs | 7 +- .../Pages/Identity/RolePermissions.razor.cs | 4 +- .../Client/Pages/Identity/Roles.razor.cs | 5 +- .../Client/Pages/Identity/Security.razor.cs | 15 ++-- .../Pages/Identity/UserProfile.razor.cs | 4 +- .../Client/Pages/Identity/UserRoles.razor.cs | 4 +- .../Client/Pages/Identity/Users.razor.cs | 4 +- .../Shared/Components/UserCard.razor.cs | 4 +- .../Client/Shared/MainLayout.razor | 66 +----------------- .../Client/Shared/MainLayout.razor.cs | 69 +++++++++++++++++++ 17 files changed, 87 insertions(+), 160 deletions(-) create mode 100644 BlazorHero.CleanArchitecture/Client/Shared/MainLayout.razor.cs diff --git a/BlazorHero.CleanArchitecture/Client/Pages/Catalog/AddEditBrandModal.razor.cs b/BlazorHero.CleanArchitecture/Client/Pages/Catalog/AddEditBrandModal.razor.cs index fe0ff9ddb..9ffd02f58 100644 --- a/BlazorHero.CleanArchitecture/Client/Pages/Catalog/AddEditBrandModal.razor.cs +++ b/BlazorHero.CleanArchitecture/Client/Pages/Catalog/AddEditBrandModal.razor.cs @@ -7,7 +7,7 @@ namespace BlazorHero.CleanArchitecture.Client.Pages.Catalog { - public partial class AddEditBrandModal : IDisposable + public partial class AddEditBrandModal { private bool success; private string[] errors = { }; @@ -29,11 +29,6 @@ public partial class AddEditBrandModal : IDisposable public string Description { get; set; } [CascadingParameter] private MudDialogInstance MudDialog { get; set; } - protected override void OnInitialized() - { - _interceptor.RegisterEvent(); - } - public void Dispose() => _interceptor.DisposeEvent(); public void Cancel() { MudDialog.Cancel(); diff --git a/BlazorHero.CleanArchitecture/Client/Pages/Catalog/AddEditProductModal.razor.cs b/BlazorHero.CleanArchitecture/Client/Pages/Catalog/AddEditProductModal.razor.cs index c41d6f040..64c244157 100644 --- a/BlazorHero.CleanArchitecture/Client/Pages/Catalog/AddEditProductModal.razor.cs +++ b/BlazorHero.CleanArchitecture/Client/Pages/Catalog/AddEditProductModal.razor.cs @@ -10,7 +10,7 @@ namespace BlazorHero.CleanArchitecture.Client.Pages.Catalog { - public partial class AddEditProductModal : IDisposable + public partial class AddEditProductModal { private bool success; private string[] errors = { }; @@ -81,10 +81,8 @@ private async Task SaveAsync() protected override async Task OnInitializedAsync() { - _interceptor.RegisterEvent(); await LoadDataAsync(); } - public void Dispose() => _interceptor.DisposeEvent(); private async Task LoadDataAsync() { diff --git a/BlazorHero.CleanArchitecture/Client/Pages/Catalog/Brands.razor.cs b/BlazorHero.CleanArchitecture/Client/Pages/Catalog/Brands.razor.cs index ba8c7445d..33842ca9a 100644 --- a/BlazorHero.CleanArchitecture/Client/Pages/Catalog/Brands.razor.cs +++ b/BlazorHero.CleanArchitecture/Client/Pages/Catalog/Brands.razor.cs @@ -7,19 +7,15 @@ namespace BlazorHero.CleanArchitecture.Client.Pages.Catalog { - public partial class Brands : IDisposable + public partial class Brands { public List BrandList = new List(); private GetAllBrandsResponse brand = new GetAllBrandsResponse(); private string searchString = ""; - protected override async Task OnInitializedAsync() { - _interceptor.RegisterEvent(); await GetBrandsAsync(); } - public void Dispose() => _interceptor.DisposeEvent(); - private async Task GetBrandsAsync() { var response = await _brandManager.GetAllAsync(); diff --git a/BlazorHero.CleanArchitecture/Client/Pages/Catalog/Products.razor.cs b/BlazorHero.CleanArchitecture/Client/Pages/Catalog/Products.razor.cs index 0dbce1962..3c9677f64 100644 --- a/BlazorHero.CleanArchitecture/Client/Pages/Catalog/Products.razor.cs +++ b/BlazorHero.CleanArchitecture/Client/Pages/Catalog/Products.razor.cs @@ -9,7 +9,7 @@ namespace BlazorHero.CleanArchitecture.Client.Pages.Catalog { - public partial class Products : IDisposable + public partial class Products { private IEnumerable pagedData; private MudTable table; @@ -17,14 +17,6 @@ public partial class Products : IDisposable private int totalItems; private int currentPage; private string searchString = null; - - protected override async Task OnInitializedAsync() - { - _interceptor.RegisterEvent(); - await Task.CompletedTask; - } - - public void Dispose() => _interceptor.DisposeEvent(); private async Task> ServerReload(TableState state) { await LoadData(state.Page, state.PageSize); diff --git a/BlazorHero.CleanArchitecture/Client/Pages/Content/Dashboard.razor.cs b/BlazorHero.CleanArchitecture/Client/Pages/Content/Dashboard.razor.cs index 857d4517e..8a78ecfcd 100644 --- a/BlazorHero.CleanArchitecture/Client/Pages/Content/Dashboard.razor.cs +++ b/BlazorHero.CleanArchitecture/Client/Pages/Content/Dashboard.razor.cs @@ -6,7 +6,7 @@ namespace BlazorHero.CleanArchitecture.Client.Pages.Content { - public partial class Dashboard : IDisposable + public partial class Dashboard { [Parameter] public int ProductCount { get; set; } @@ -19,12 +19,9 @@ public partial class Dashboard : IDisposable protected override async Task OnInitializedAsync() { - _interceptor.RegisterEvent(); await LoadDataAsync(); } - public void Dispose() => _interceptor.DisposeEvent(); - private async Task LoadDataAsync() { var data = await _dashboardManager.GetDataAsync(); diff --git a/BlazorHero.CleanArchitecture/Client/Pages/Identity/Profile.razor.cs b/BlazorHero.CleanArchitecture/Client/Pages/Identity/Profile.razor.cs index e8a40ec82..90f887f23 100644 --- a/BlazorHero.CleanArchitecture/Client/Pages/Identity/Profile.razor.cs +++ b/BlazorHero.CleanArchitecture/Client/Pages/Identity/Profile.razor.cs @@ -37,11 +37,8 @@ private async Task UpdateProfileAsync() protected override async Task OnInitializedAsync() { - _interceptor.RegisterEvent(); await LoadDataAsync(); } - public void Dispose() => _interceptor.DisposeEvent(); - private async Task LoadDataAsync() { var state = await _stateProvider.GetAuthenticationStateAsync(); diff --git a/BlazorHero.CleanArchitecture/Client/Pages/Identity/RegisterUserModal.razor.cs b/BlazorHero.CleanArchitecture/Client/Pages/Identity/RegisterUserModal.razor.cs index 847745ecc..2e7331071 100644 --- a/BlazorHero.CleanArchitecture/Client/Pages/Identity/RegisterUserModal.razor.cs +++ b/BlazorHero.CleanArchitecture/Client/Pages/Identity/RegisterUserModal.razor.cs @@ -9,7 +9,7 @@ namespace BlazorHero.CleanArchitecture.Client.Pages.Identity { - public partial class RegisterUserModal : IDisposable + public partial class RegisterUserModal { private bool success; private string[] errors = { }; @@ -52,33 +52,6 @@ public void Cancel() { MudDialog.Cancel(); } - [Parameter] - public int ProductCount { get; set; } - [Parameter] - public int BrandCount { get; set; } - [Parameter] - public int UserCount { get; set; } - [Parameter] - public int RoleCount { get; set; } - - protected override void OnInitialized() - { - _interceptor.RegisterEvent(); - } - - public void Dispose() => _interceptor.DisposeEvent(); - - private async Task LoadDataAsync() - { - var data = await _dashboardManager.GetDataAsync(); - if (data.Succeeded) - { - ProductCount = data.Data.ProductCount; - BrandCount = data.Data.BrandCount; - UserCount = data.Data.UserCount; - RoleCount = data.Data.RoleCount; - } - } private async Task SaveAsync() { form.Validate(); @@ -111,7 +84,6 @@ private async Task SaveAsync() } } } - private IEnumerable PasswordStrength(string pw) { if (string.IsNullOrWhiteSpace(pw)) diff --git a/BlazorHero.CleanArchitecture/Client/Pages/Identity/RoleModal.razor.cs b/BlazorHero.CleanArchitecture/Client/Pages/Identity/RoleModal.razor.cs index 76a6923c6..e9a86e3f5 100644 --- a/BlazorHero.CleanArchitecture/Client/Pages/Identity/RoleModal.razor.cs +++ b/BlazorHero.CleanArchitecture/Client/Pages/Identity/RoleModal.razor.cs @@ -7,7 +7,7 @@ namespace BlazorHero.CleanArchitecture.Client.Pages.Identity { - public partial class RoleModal : IDisposable + public partial class RoleModal { private bool success; private string[] errors = { }; @@ -26,11 +26,6 @@ public void Cancel() { MudDialog.Cancel(); } - protected override void OnInitialized() - { - _interceptor.RegisterEvent(); - } - public void Dispose() => _interceptor.DisposeEvent(); private async Task SaveAsync() { form.Validate(); diff --git a/BlazorHero.CleanArchitecture/Client/Pages/Identity/RolePermissions.razor.cs b/BlazorHero.CleanArchitecture/Client/Pages/Identity/RolePermissions.razor.cs index c7f467561..a3c4a6690 100644 --- a/BlazorHero.CleanArchitecture/Client/Pages/Identity/RolePermissions.razor.cs +++ b/BlazorHero.CleanArchitecture/Client/Pages/Identity/RolePermissions.razor.cs @@ -9,7 +9,7 @@ namespace BlazorHero.CleanArchitecture.Client.Pages.Identity { - public partial class RolePermissions : IDisposable + public partial class RolePermissions { [Parameter] public string Id { get; set; } @@ -25,7 +25,6 @@ public partial class RolePermissions : IDisposable protected override async Task OnInitializedAsync() { - _interceptor.RegisterEvent(); _mapper = new MapperConfiguration(c => { c.AddProfile(); }).CreateMapper(); var roleId = Id; var result = await _roleManager.GetPermissionsAsync(roleId); @@ -38,7 +37,6 @@ protected override async Task OnInitializedAsync() } } } - public void Dispose() => _interceptor.DisposeEvent(); private async Task SaveAsync() { var request = _mapper.Map(model); diff --git a/BlazorHero.CleanArchitecture/Client/Pages/Identity/Roles.razor.cs b/BlazorHero.CleanArchitecture/Client/Pages/Identity/Roles.razor.cs index 482c3aab6..ff53b05d8 100644 --- a/BlazorHero.CleanArchitecture/Client/Pages/Identity/Roles.razor.cs +++ b/BlazorHero.CleanArchitecture/Client/Pages/Identity/Roles.razor.cs @@ -7,7 +7,7 @@ namespace BlazorHero.CleanArchitecture.Client.Pages.Identity { - public partial class Roles : IDisposable + public partial class Roles { public List RoleList = new List(); private RoleResponse role = new RoleResponse(); @@ -15,11 +15,8 @@ public partial class Roles : IDisposable protected override async Task OnInitializedAsync() { - _interceptor.RegisterEvent(); await GetRolesAsync(); } - public void Dispose() => _interceptor.DisposeEvent(); - private async Task GetRolesAsync() { var response = await _roleManager.GetRolesAsync(); diff --git a/BlazorHero.CleanArchitecture/Client/Pages/Identity/Security.razor.cs b/BlazorHero.CleanArchitecture/Client/Pages/Identity/Security.razor.cs index f085116dd..ac1a1d7bd 100644 --- a/BlazorHero.CleanArchitecture/Client/Pages/Identity/Security.razor.cs +++ b/BlazorHero.CleanArchitecture/Client/Pages/Identity/Security.razor.cs @@ -8,31 +8,24 @@ namespace BlazorHero.CleanArchitecture.Client.Pages.Identity { - public partial class Security : IDisposable + public partial class Security { [Inject] private Microsoft.Extensions.Localization.IStringLocalizer localizer { get; set; } private readonly ChangePasswordRequest passwordModel = new ChangePasswordRequest(); - - protected override void OnInitialized() - { - _interceptor.RegisterEvent(); - } - public void Dispose() => _interceptor.DisposeEvent(); - private async Task ChangePasswordAsync() { var response = await _accountManager.ChangePasswordAsync(passwordModel); - if(response.Succeeded) + if (response.Succeeded) { - _snackBar.Add("Password Changed!",Severity.Success); + _snackBar.Add("Password Changed!", Severity.Success); passwordModel.Password = string.Empty; passwordModel.NewPassword = string.Empty; passwordModel.ConfirmNewPassword = string.Empty; } else { - foreach(var error in response.Messages) + foreach (var error in response.Messages) { _snackBar.Add(error, Severity.Error); } diff --git a/BlazorHero.CleanArchitecture/Client/Pages/Identity/UserProfile.razor.cs b/BlazorHero.CleanArchitecture/Client/Pages/Identity/UserProfile.razor.cs index 3cb37efe1..953eb1912 100644 --- a/BlazorHero.CleanArchitecture/Client/Pages/Identity/UserProfile.razor.cs +++ b/BlazorHero.CleanArchitecture/Client/Pages/Identity/UserProfile.razor.cs @@ -7,7 +7,7 @@ namespace BlazorHero.CleanArchitecture.Client.Pages.Identity { - public partial class UserProfile : IDisposable + public partial class UserProfile { [Parameter] public string Id { get; set; } @@ -48,10 +48,8 @@ private async Task ToggleUserStatus() [Parameter] public string ImageDataUrl { get; set; } - public void Dispose() => _interceptor.DisposeEvent(); protected override async Task OnInitializedAsync() { - _interceptor.RegisterEvent(); var userId = Id; var result = await _userManager.GetAsync(userId); if (result.Succeeded) diff --git a/BlazorHero.CleanArchitecture/Client/Pages/Identity/UserRoles.razor.cs b/BlazorHero.CleanArchitecture/Client/Pages/Identity/UserRoles.razor.cs index 3643f5adc..e06af4585 100644 --- a/BlazorHero.CleanArchitecture/Client/Pages/Identity/UserRoles.razor.cs +++ b/BlazorHero.CleanArchitecture/Client/Pages/Identity/UserRoles.razor.cs @@ -9,7 +9,7 @@ namespace BlazorHero.CleanArchitecture.Client.Pages.Identity { - public partial class UserRoles : IDisposable + public partial class UserRoles { [Parameter] public string Id { get; set; } @@ -25,7 +25,6 @@ public partial class UserRoles : IDisposable protected override async Task OnInitializedAsync() { - _interceptor.RegisterEvent(); CurrentUser = await _authenticationManager.CurrentUser(); var userId = Id; @@ -42,7 +41,6 @@ protected override async Task OnInitializedAsync() } } } - public void Dispose() => _interceptor.DisposeEvent(); private async Task SaveAsync() { var request = new UpdateUserRolesRequest() diff --git a/BlazorHero.CleanArchitecture/Client/Pages/Identity/Users.razor.cs b/BlazorHero.CleanArchitecture/Client/Pages/Identity/Users.razor.cs index 1b95638a5..d1b863039 100644 --- a/BlazorHero.CleanArchitecture/Client/Pages/Identity/Users.razor.cs +++ b/BlazorHero.CleanArchitecture/Client/Pages/Identity/Users.razor.cs @@ -7,7 +7,7 @@ namespace BlazorHero.CleanArchitecture.Client.Pages.Identity { - public partial class Users : IDisposable + public partial class Users { public List UserList = new List(); private UserResponse user = new UserResponse(); @@ -15,10 +15,8 @@ public partial class Users : IDisposable protected override async Task OnInitializedAsync() { - _interceptor.RegisterEvent(); await GetUsersAsync(); } - public void Dispose() => _interceptor.DisposeEvent(); private async Task GetUsersAsync() { var response = await _userManager.GetAllAsync(); diff --git a/BlazorHero.CleanArchitecture/Client/Shared/Components/UserCard.razor.cs b/BlazorHero.CleanArchitecture/Client/Shared/Components/UserCard.razor.cs index 3ca49595a..06d44b4b9 100644 --- a/BlazorHero.CleanArchitecture/Client/Shared/Components/UserCard.razor.cs +++ b/BlazorHero.CleanArchitecture/Client/Shared/Components/UserCard.razor.cs @@ -7,7 +7,7 @@ namespace BlazorHero.CleanArchitecture.Client.Shared.Components { - public partial class UserCard : IDisposable + public partial class UserCard { [Parameter] public string Class { get; set; } private string FirstName { get; set; } @@ -19,10 +19,8 @@ public partial class UserCard : IDisposable public string ImageDataUrl { get; set; } protected override async Task OnInitializedAsync() { - _interceptor.RegisterEvent(); await LoadDataAsync(); } - public void Dispose() => _interceptor.DisposeEvent(); private async Task LoadDataAsync() { var state = await _stateProvider.GetAuthenticationStateAsync(); diff --git a/BlazorHero.CleanArchitecture/Client/Shared/MainLayout.razor b/BlazorHero.CleanArchitecture/Client/Shared/MainLayout.razor index f51bf4a30..948e5b712 100644 --- a/BlazorHero.CleanArchitecture/Client/Shared/MainLayout.razor +++ b/BlazorHero.CleanArchitecture/Client/Shared/MainLayout.razor @@ -81,68 +81,4 @@ - -@code { - private string FirstName { get; set; } - private string SecondName { get; set; } - private string Email { get; set; } - private char FirstLetterOfName { get; set; } - private async Task LoadDataAsync() - { - var state = await _stateProvider.GetAuthenticationStateAsync(); - var user = state.User; - if (user == null) return; - if (user.Identity.IsAuthenticated) - { - this.FirstName = user.GetFirstName(); - if (this.FirstName.Length > 0) - { - FirstLetterOfName = FirstName[0]; - } - - } - - } - MudTheme currentTheme; - private bool _drawerOpen = true; - protected override async Task OnInitializedAsync() - { - currentTheme = await _preferenceManager.GetCurrentThemeAsync(); - } - void Logout() - { - string logoutConfirmationText = localizer["Logout Confirmation"]; - string logoutText = localizer["Logout"]; - var parameters = new DialogParameters(); - parameters.Add("ContentText", logoutConfirmationText); - parameters.Add("ButtonText", logoutText); - parameters.Add("Color", Color.Error); - - var options = new DialogOptions() { CloseButton = true, MaxWidth = MaxWidth.Small, FullWidth = true }; - - _dialogService.Show("Logout", parameters, options); - } - void DrawerToggle() - { - _drawerOpen = !_drawerOpen; - } - public List _items = new List -{ - new BreadcrumbItem("Home", href: "/"), - new BreadcrumbItem("Resource", href: "/resources"), - }; - - async Task DarkMode() - { - bool isDarkMode = await _preferenceManager.ToggleDarkModeAsync(); - if (isDarkMode) - { - currentTheme = BlazorHeroTheme.DefaultTheme; - } - else - { - currentTheme = BlazorHeroTheme.DarkTheme; - } - } - -} \ No newline at end of file + \ No newline at end of file diff --git a/BlazorHero.CleanArchitecture/Client/Shared/MainLayout.razor.cs b/BlazorHero.CleanArchitecture/Client/Shared/MainLayout.razor.cs new file mode 100644 index 000000000..26b08f01b --- /dev/null +++ b/BlazorHero.CleanArchitecture/Client/Shared/MainLayout.razor.cs @@ -0,0 +1,69 @@ +using BlazorHero.CleanArchitecture.Client.Extensions; +using BlazorHero.CleanArchitecture.Client.Infrastructure.Settings; +using MudBlazor; +using System; +using System.Threading.Tasks; + +namespace BlazorHero.CleanArchitecture.Client.Shared +{ + public partial class MainLayout : IDisposable + { + private string FirstName { get; set; } + private string SecondName { get; set; } + private string Email { get; set; } + private char FirstLetterOfName { get; set; } + private async Task LoadDataAsync() + { + var state = await _stateProvider.GetAuthenticationStateAsync(); + var user = state.User; + if (user == null) return; + if (user.Identity.IsAuthenticated) + { + this.FirstName = user.GetFirstName(); + if (this.FirstName.Length > 0) + { + FirstLetterOfName = FirstName[0]; + } + + } + + } + MudTheme currentTheme; + private bool _drawerOpen = true; + protected override async Task OnInitializedAsync() + { + _interceptor.RegisterEvent(); + currentTheme = await _preferenceManager.GetCurrentThemeAsync(); + } + void Logout() + { + string logoutConfirmationText = localizer["Logout Confirmation"]; + string logoutText = localizer["Logout"]; + var parameters = new DialogParameters(); + parameters.Add("ContentText", logoutConfirmationText); + parameters.Add("ButtonText", logoutText); + parameters.Add("Color", Color.Error); + + var options = new DialogOptions() { CloseButton = true, MaxWidth = MaxWidth.Small, FullWidth = true }; + + _dialogService.Show("Logout", parameters, options); + } + void DrawerToggle() + { + _drawerOpen = !_drawerOpen; + } + async Task DarkMode() + { + bool isDarkMode = await _preferenceManager.ToggleDarkModeAsync(); + if (isDarkMode) + { + currentTheme = BlazorHeroTheme.DefaultTheme; + } + else + { + currentTheme = BlazorHeroTheme.DarkTheme; + } + } + public void Dispose() => _interceptor.DisposeEvent(); + } +}