From 5d52940716eb58b2931ccbb222ea93b67111c93d Mon Sep 17 00:00:00 2001 From: Greg Finzer Date: Tue, 27 Aug 2024 11:58:39 -0400 Subject: [PATCH] Remove MyBody and improve Index page performance. Use constants. --- .../Components/AngleImageView.razor.cs | 3 +- .../Components/FileManager.razor.cs | 3 +- BedBrigade.Client/Components/Footer.razor.cs | 3 +- BedBrigade.Client/Components/Header.razor.cs | 16 +- BedBrigade.Client/Components/MediaHelper.cs | 5 +- BedBrigade.Client/Components/MyBody.razor | 146 ------------------ .../Components/Pages/Index.razor | 41 +---- .../Components/Pages/Index.razor.cs | 90 +++++++++++ BedBrigade.Client/Services/LocationState.cs | 6 +- 9 files changed, 118 insertions(+), 195 deletions(-) delete mode 100644 BedBrigade.Client/Components/MyBody.razor create mode 100644 BedBrigade.Client/Components/Pages/Index.razor.cs diff --git a/BedBrigade.Client/Components/AngleImageView.razor.cs b/BedBrigade.Client/Components/AngleImageView.razor.cs index 2f9cc257..47c77d75 100644 --- a/BedBrigade.Client/Components/AngleImageView.razor.cs +++ b/BedBrigade.Client/Components/AngleImageView.razor.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Components; using System.Collections.Generic; using System.IO; +using BedBrigade.Data.Data.Seeding; namespace BedBrigade.Client.Components { @@ -9,7 +10,7 @@ public partial class AngleImageView { [Parameter] public string Caption { get; set; } = "Bed Brigade"; - [Parameter] public string Path { get; set; } = "National"; + [Parameter] public string Path { get; set; } = SeedConstants.SeedNationalName; string FileName { get; set; } = "NoImageFound.jpg"; string LeftFileName { get; set; } diff --git a/BedBrigade.Client/Components/FileManager.razor.cs b/BedBrigade.Client/Components/FileManager.razor.cs index 64aea18b..361e2d85 100644 --- a/BedBrigade.Client/Components/FileManager.razor.cs +++ b/BedBrigade.Client/Components/FileManager.razor.cs @@ -9,6 +9,7 @@ using BedBrigade.Common.Constants; using BedBrigade.Common.Enums; using BedBrigade.Common.Models; +using BedBrigade.Data.Data.Seeding; namespace BedBrigade.Client.Components { @@ -302,7 +303,7 @@ public bool isLocationFolder() if (slashCount == 2) { // take text between /xxxxx/ var arLocation = folderPath.Split(PathDivider); - if (arLocation[1].ToLower() != "national") // special validation, because route "/" is national + if (arLocation[1].ToLower() != SeedConstants.SeedNationalName.ToLower()) // special validation, because route "/" is national { selectedLocation += arLocation[1]; } diff --git a/BedBrigade.Client/Components/Footer.razor.cs b/BedBrigade.Client/Components/Footer.razor.cs index d09a551d..6b7960f9 100644 --- a/BedBrigade.Client/Components/Footer.razor.cs +++ b/BedBrigade.Client/Components/Footer.razor.cs @@ -1,4 +1,5 @@ using BedBrigade.Client.Services; +using BedBrigade.Data.Data.Seeding; using BedBrigade.Data.Services; using Microsoft.AspNetCore.Components; using Serilog; @@ -28,7 +29,7 @@ public void Dispose() private async Task LoadContent() { - string locationName = _locationState.Location ?? "National"; + string locationName = _locationState.Location ?? SeedConstants.SeedNationalName; var locationResult = await _svcLocation.GetLocationByRouteAsync($"/{locationName.ToLower()}"); if (!locationResult.Success) diff --git a/BedBrigade.Client/Components/Header.razor.cs b/BedBrigade.Client/Components/Header.razor.cs index 89c2db9f..9beecf4f 100644 --- a/BedBrigade.Client/Components/Header.razor.cs +++ b/BedBrigade.Client/Components/Header.razor.cs @@ -1,6 +1,8 @@ +using System.Security.Claims; using BedBrigade.Client.Services; using BedBrigade.Common.Constants; using BedBrigade.Common.Logic; +using BedBrigade.Data.Data.Seeding; using BedBrigade.Data.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; @@ -29,6 +31,7 @@ public partial class Header : ComponentBase, IDisposable private string Menu { get; set; } private AuthenticationState _authState { get; set; } private string PreviousLocation { get; set; } + private ClaimsPrincipal User { get; set; } protected override async Task OnInitializedAsync() { @@ -42,7 +45,16 @@ protected override async Task OnInitializedAsync() private async void OnAuthenticationStateChanged(Task task) { _authState = await task; - StateHasChanged(); + + //We don't need to refresh if this is the first time to load the component + if (User == null) + { + User = _authState.User; + } + else + { + StateHasChanged(); + } } private async Task OnLocationChanged() @@ -53,7 +65,7 @@ private async Task OnLocationChanged() private async Task LoadContent() { - string locationName = _locationState.Location ?? "National"; + string locationName = _locationState.Location ?? SeedConstants.SeedNationalName; var locationResult = await _svcLocation.GetLocationByRouteAsync($"/{locationName.ToLower()}"); if (!locationResult.Success) diff --git a/BedBrigade.Client/Components/MediaHelper.cs b/BedBrigade.Client/Components/MediaHelper.cs index 2eb42a87..9e61eaf2 100644 --- a/BedBrigade.Client/Components/MediaHelper.cs +++ b/BedBrigade.Client/Components/MediaHelper.cs @@ -4,6 +4,7 @@ using BedBrigade.Common.Enums; using BedBrigade.Common.Constants; using BedBrigade.Common.Models; +using BedBrigade.Data.Data.Seeding; namespace BedBrigade.Client.Components { @@ -178,10 +179,10 @@ public static void UpdateUserFolderList(ref MediaUser MediaUser) if (MediaUser.FolderList.Count > 1) { - if (MediaUser.Role.Contains("National")) + if (MediaUser.Role.Contains(SeedConstants.SeedNationalName)) { // set National pre-selecterd - var nationalFolder = MediaUser.FolderList.Where(f => f.ToLower().Contains("national")).ToList(); + var nationalFolder = MediaUser.FolderList.Where(f => f.ToLower().Contains(SeedConstants.SeedNationalName.ToLower())).ToList(); MediaUser.DropFileFolder = nationalFolder[0]; } else diff --git a/BedBrigade.Client/Components/MyBody.razor b/BedBrigade.Client/Components/MyBody.razor deleted file mode 100644 index 602dc1ff..00000000 --- a/BedBrigade.Client/Components/MyBody.razor +++ /dev/null @@ -1,146 +0,0 @@ -@using BedBrigade.Client.Services; -@using BedBrigade.Common.Models -@using Serilog -@inject IJSRuntime JS - -@((MarkupString)BodyContent) - -@code { - // Client - - [Inject] private IContentDataService _svcContent { get; set; } - [Inject] private ILocationDataService _svcLocation { get; set; } - [Inject] private NavigationManager _navigationManager { get; set; } - [Inject] private ILoadImagesService _loadImagesService { get; set; } - - string FileName { get; set; } = "NoImageFound.jpg"; - - - private Content content { get; set; } - private string BodyContent = string.Empty; - private string[] routePath; - private string Menu { get; set; } - private string Path { get; set; } - - protected override async Task OnInitializedAsync() - { - await LoadBody(); - } - - public void RefreshState() - { - StateHasChanged(); - } - - public async Task LoadBody() - { - try - { - Log.Logger.Debug("MyBody.LoadBody"); - var url = _navigationManager.Uri; - if (String.IsNullOrEmpty(url)) - { - return; - } - - var PageName = string.Empty; - var Location = string.Empty; - var routePath = url.Split('/'); - if (string.IsNullOrEmpty(routePath[3])) - { - Location = "National"; - PageName = "Home"; - } - else if (routePath.Length == 4) - { - Location = routePath[3]; - PageName = "Home"; - } - else if (routePath.Length == 5) - { - Location = routePath[3]; - PageName = routePath[4]; - } - else - { - _navigationManager.NavigateTo("/Sorry", true); - return; - } - - Path = $"/{Location}/pages/{PageName}"; - await LoadLocationPage(Location, PageName); - } - catch (Exception ex) - { - Log.Logger.Error(ex, $"LoadBody: {ex.Message}"); - throw; - } - } - - - - private async Task LoadLocationPage(string location, string pageName) - { - Log.Logger.Debug("MyBody.LoadLocationPage"); - - try - { - - - var locationResponse = await _svcLocation.GetLocationByRouteAsync($"/{location}"); - - if (locationResponse.Success && locationResponse.Data != null) - { - Console.WriteLine($"Location passed {location} Location {locationResponse.Data.LocationId} "); - var contentResult = await _svcContent.GetAsync(pageName, locationResponse.Data.LocationId); - Console.WriteLine($"Page: {pageName} Location: {locationResponse.Data.LocationId}"); - if (contentResult.Success) - { - content = contentResult.Data; - string html = _loadImagesService.SetImagesForHtml(Path, contentResult.Data.ContentHtml); - BodyContent = html; - - Menu = FindMenu(); - if (string.IsNullOrEmpty(Menu)) - { - Menu = "home"; - } - } - else - { - _navigationManager.NavigateTo("/Sorry", true); - } - } - } - catch (Exception ex) - { - Log.Logger.Error(ex, $"LoadLocationPage: {ex.Message}"); - throw; - } - } - - - protected string FindMenu() - { - Log.Logger.Debug("MyBody.FindMenu"); - - try - { - - - var location = _navigationManager.Uri.Split('/'); - if (location.Length <= 4) - { - return "home"; - } - - return location[location.Length - 1].ToLower(); - } - catch (Exception ex) - { - Log.Logger.Error(ex, $"FindMenu: {ex.Message}"); - throw; - } - } - -} \ No newline at end of file diff --git a/BedBrigade.Client/Components/Pages/Index.razor b/BedBrigade.Client/Components/Pages/Index.razor index f09708a5..5a009b08 100644 --- a/BedBrigade.Client/Components/Pages/Index.razor +++ b/BedBrigade.Client/Components/Pages/Index.razor @@ -1,46 +1,7 @@ @page "/" @page "/{mylocation:nonfile}" @page "/{mylocation}/{mypageName:nonfile}" -@using BedBrigade.Client.Services -@using Serilog - +@((MarkupString)BodyContent) -@code { - MyBody myBody; - [Inject] private IJSRuntime _js { get; set; } - [Inject] - private ILocationState _locationState { get; set; } - - [Parameter] public string? mylocation { get; set; } - [Parameter] public string? mypageName { get; set; } - public string location { get; set; } - public string pageName { get; set; } - const string defaultLocation = "National"; - const string defaultPageName = "Home"; - - protected override async Task OnParametersSetAsync() - { - location = string.IsNullOrEmpty(mylocation) ? defaultLocation : mylocation; - pageName = string.IsNullOrEmpty(mypageName) ? defaultPageName : mypageName; - _locationState.Location = location; - mylocation = null; - mypageName = null; - } - - protected override async Task OnAfterRenderAsync(bool firstRender) - { - - string message = $"Calling {mypageName ?? defaultPageName} for location {mylocation ?? defaultLocation}"; - Log.Logger.Debug(message); - - if (!firstRender) - { - Log.Logger.Debug("LoadBody"); - await myBody.LoadBody(); - Log.Logger.Debug("RefreshState"); - myBody.RefreshState(); - } - } -} diff --git a/BedBrigade.Client/Components/Pages/Index.razor.cs b/BedBrigade.Client/Components/Pages/Index.razor.cs new file mode 100644 index 00000000..8508af88 --- /dev/null +++ b/BedBrigade.Client/Components/Pages/Index.razor.cs @@ -0,0 +1,90 @@ +using BedBrigade.Client.Services; +using BedBrigade.Data.Data.Seeding; +using BedBrigade.Data.Services; +using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; +using Serilog; + +namespace BedBrigade.Client.Components.Pages +{ + + public partial class Index : ComponentBase + { + [Inject] private ILocationDataService _svcLocation { get; set; } + [Inject] private IContentDataService _svcContent { get; set; } + [Inject] private NavigationManager _navigationManager { get; set; } + [Inject] private ILoadImagesService _loadImagesService { get; set; } + [Inject] private IJSRuntime _js { get; set; } + + [Inject] + private ILocationState _locationState { get; set; } + + [Parameter] public string? mylocation { get; set; } + [Parameter] public string? mypageName { get; set; } + + const string defaultLocation = SeedConstants.SeedNationalName; + const string defaultPageName = "Home"; + + private string previousLocation = SeedConstants.SeedNationalName; + private string previousPageName = defaultPageName; + private string BodyContent = string.Empty; + + protected override async Task OnInitializedAsync() + { + string location = string.IsNullOrEmpty(mylocation) ? defaultLocation : mylocation; + string pageName = string.IsNullOrEmpty(mypageName) ? defaultPageName : mypageName; + await LoadLocationPage(location, pageName); + } + + protected override async Task OnParametersSetAsync() + { + string location = string.IsNullOrEmpty(mylocation) ? defaultLocation : mylocation; + string pageName = string.IsNullOrEmpty(mypageName) ? defaultPageName : mypageName; + _locationState.Location = location; + + if (previousLocation.ToLower() != location.ToLower() || previousPageName.ToLower() != pageName.ToLower()) + { + previousLocation = location; + previousPageName = pageName; + await LoadLocationPage(location, pageName); + } + } + + private async Task LoadLocationPage(string location, string pageName) + { + Log.Logger.Debug("Index.LoadLocationPage"); + + try + { + var locationResponse = await _svcLocation.GetLocationByRouteAsync($"/{location}"); + + if (locationResponse.Success && locationResponse.Data != null) + { + Console.WriteLine($"Location passed {location} Location {locationResponse.Data.LocationId} "); + var contentResult = await _svcContent.GetAsync(pageName, locationResponse.Data.LocationId); + Console.WriteLine($"Page: {pageName} Location: {locationResponse.Data.LocationId}"); + if (contentResult.Success) + { + //string content = contentResult.Data; + var path = $"/{location}/pages/{pageName}"; + string html = _loadImagesService.SetImagesForHtml(path, contentResult.Data.ContentHtml); + BodyContent = html; + } + else + { + _navigationManager.NavigateTo("/Sorry", true); + } + } + else + { + _navigationManager.NavigateTo("/Sorry", true); + } + } + catch (Exception ex) + { + Log.Logger.Error(ex, $"LoadLocationPage: {ex.Message}"); + throw; + } + } + } +} diff --git a/BedBrigade.Client/Services/LocationState.cs b/BedBrigade.Client/Services/LocationState.cs index 2b53211d..54797294 100644 --- a/BedBrigade.Client/Services/LocationState.cs +++ b/BedBrigade.Client/Services/LocationState.cs @@ -1,15 +1,17 @@  +using BedBrigade.Data.Data.Seeding; + namespace BedBrigade.Client.Services { public class LocationState : ILocationState { - private string _location; + private string _location = SeedConstants.SeedNationalName; public string Location { get => _location; set { - if (_location != value) + if (_location.ToLower() != value.ToLower()) { _location = value; NotifyStateChangedAsync();