Skip to content

Commit

Permalink
Merge pull request #316 from GregFinzer/FixAddLocation
Browse files Browse the repository at this point in the history
Big fix for add location
  • Loading branch information
GregFinzer authored Aug 20, 2024
2 parents 8007afc + 976be4d commit 7ac05fe
Show file tree
Hide file tree
Showing 23 changed files with 853 additions and 163 deletions.
34 changes: 1 addition & 33 deletions BedBrigade.Client/Components/Footer.razor
Original file line number Diff line number Diff line change
@@ -1,33 +1 @@
@using Serilog

@((MarkupString)footerContent)

@code {
// Client
private string footerContent = string.Empty;
[Inject] private IContentDataService _svcContent { get; set; }
[Inject] private ILocationDataService _svcLocation { get; set; }
[Inject] NavigationManager _nm { get; set; }

protected override async Task OnInitializedAsync()
{
try
{

var contentResult = await _svcContent.GetAsync("Footer", (int)LocationNumber.National);
if (contentResult.Success)
{
footerContent = contentResult.Data.ContentHtml;
}
else
{
Log.Logger.Error($"Error getting footer content: {contentResult.Message}");
}
}
catch (Exception ex)
{
Log.Logger.Error(ex, $"Footer.OnInitializedAsync : {ex.Message}");
throw;
}
}
}
@((MarkupString)footerContent)
60 changes: 60 additions & 0 deletions BedBrigade.Client/Components/Footer.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using BedBrigade.Client.Services;
using BedBrigade.Data.Services;
using Microsoft.AspNetCore.Components;
using Serilog;

namespace BedBrigade.Client.Components
{
public partial class Footer : ComponentBase, IDisposable
{
// Client
private string footerContent = string.Empty;
[Inject] private IContentDataService _svcContent { get; set; }
[Inject] private ILocationDataService _svcLocation { get; set; }
[Inject] NavigationManager _nm { get; set; }
[Inject] private IFooterLocationState _locationState { get; set; }
private string PreviousLocation { get; set; }

protected override async Task OnInitializedAsync()
{
await LoadContent();
_locationState.OnChange += OnLocationChanged;
}

public void Dispose()
{
_locationState.OnChange -= OnLocationChanged;
}

private async Task LoadContent()
{
string locationName = _locationState.Location ?? "national";
var locationResult = await _svcLocation.GetLocationByRouteAsync($"/{locationName.ToLower()}");

if (!locationResult.Success)
{
Log.Logger.Error($"Error loading Footer location: {locationResult.Message}");
}
else
{
var contentResult = await _svcContent.GetAsync("Footer", locationResult.Data.LocationId);

if (contentResult.Success)
{
footerContent = contentResult.Data.ContentHtml;
PreviousLocation = locationName;
}
else
{
Log.Logger.Error($"Error loading Footer for LocationId {locationResult.Data.LocationId}: {contentResult.Message}");
}
}
}

private async void OnLocationChanged()
{
await LoadContent();
StateHasChanged();
}
}
}
3 changes: 1 addition & 2 deletions BedBrigade.Client/Components/Header.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
This is the header
@((MarkupString)headerContent)
@((MarkupString)headerContent)


<style>
Expand Down
57 changes: 41 additions & 16 deletions BedBrigade.Client/Components/Header.razor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using BedBrigade.Client.Services;
using BedBrigade.Common.Constants;
using BedBrigade.Common.Enums;
using BedBrigade.Common.Logic;
using BedBrigade.Data.Services;
using Microsoft.AspNetCore.Components;
Expand All @@ -9,14 +9,15 @@

namespace BedBrigade.Client.Components
{
public partial class Header
public partial class Header : ComponentBase, IDisposable
{
// Client
[Inject] private IJSRuntime _js { get; set; }
[Inject] private IContentDataService _svcContent { get; set; }
[Inject] private ILocationDataService _svcLocation { get; set; }
[Inject] private AuthenticationStateProvider _authenticationStateProvider { get; set; }
[Inject] private NavigationManager _nm { get; set; }
[Inject] private IHeaderLocationState _locationState { get; set; }

const string LoginElement = "loginElement";
const string AdminElement = "adminElement";
Expand All @@ -27,39 +28,63 @@ public partial class Header
private bool IsAuthenicated { get; set; } = false;
private string Menu { get; set; }
private AuthenticationState _authState { get; set; }
private string PreviousLocation { get; set; }

protected override async Task OnInitializedAsync()
{
Log.Debug("Header.OnInitializedAsync");
await LoadContent();
_authenticationStateProvider.AuthenticationStateChanged += OnAuthenticationStateChanged;
try
_locationState.OnChange += OnLocationChanged;
}


private async void OnAuthenticationStateChanged(Task<AuthenticationState> task)
{
_authState = await task;
StateHasChanged();
}

private async void OnLocationChanged()
{
if (_locationState.Location == PreviousLocation)
{
var contentResult = await _svcContent.GetAsync("Header", (int)LocationNumber.National);
return;
}

await LoadContent();
StateHasChanged();
}

private async Task LoadContent()
{
string locationName = _locationState.Location ?? "national";
var locationResult = await _svcLocation.GetLocationByRouteAsync($"/{locationName.ToLower()}");

if (!locationResult.Success)
{
Log.Logger.Error($"Error loading location: {locationResult.Message}");
}
else
{
var contentResult = await _svcContent.GetAsync("Header", locationResult.Data.LocationId);

if (contentResult.Success)
{
await Console.Out.WriteLineAsync($"Loaded Header");
headerContent = contentResult.Data.ContentHtml;
PreviousLocation = locationName;
}
else
{
Log.Logger.Error($"Error loading Header: {contentResult.Message}");
Log.Logger.Error($"Error loading Header for LocationId {locationResult.Data.LocationId}: {contentResult.Message}");
}
}
catch (Exception ex)
{
Log.Logger.Error(ex, $"Error loading header: {ex.Message}");
}
}

private async void OnAuthenticationStateChanged(Task<AuthenticationState> task)
{
_authState = await task;
StateHasChanged();
}

public void Dispose()
{
_authenticationStateProvider.AuthenticationStateChanged -= OnAuthenticationStateChanged;
_locationState.OnChange -= OnLocationChanged; // Unsubscribe from the event
}

protected override async Task OnAfterRenderAsync(bool firstRender)
Expand Down
68 changes: 5 additions & 63 deletions BedBrigade.Client/Components/LocationGrid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,43 +245,19 @@ private async Task AddNewLocationAsync(Location Location)

// new Location
var result = await _svcLocation.CreateAsync(Location);
if (result.Success)
{
Location location = result.Data;
if (!FileUtil.MediaSubDirectoryExists(Location.Route))
{
FileUtil.CreateMediaSubDirectory(Location.Route);
var locationRoute = FileUtil.GetMediaDirectory(location.Route);
if (!Directory.Exists(locationRoute + "/pages"))
{
locationRoute = locationRoute + "/pages";
FileUtil.CreateDirectory(locationRoute);
string seedingDirectory = Common.Logic.FileUtil.GetSeedingDirectory();
FileUtil.CopyDirectory($"{seedingDirectory}/SeedImages/pages/", locationRoute);
}

await CreateContentAsync(location.LocationId, location.Name, PageNames, BedBrigade.Common.Enums.ContentType.Body);
PageNames.Clear();
PageNames.Add("Header0");
await CreateContentAsync(location.LocationId, location.Name, PageNames, ContentType.Header);
PageNames.Clear();
PageNames.Add("Footer0");
await CreateContentAsync(location.LocationId, location.Name, PageNames, ContentType.Footer);
PageNames.Clear();
PageNames.Add($"Home0");
await CreateContentAsync(location.LocationId, location.Name, PageNames, ContentType.Home);
}
}
ToastTitle = "Create Location";
if (Location.LocationId != 0)

if (result.Success)
{
ToastContent = "Location Created Successfully!";
await ToastObj.ShowAsync(new ToastModel { Title = ToastTitle, Content = ToastContent, Timeout = ToastTimeout });
}
else
{
ToastContent = "Unable to save Location!";
ToastContent = result.Message ;
await ToastObj.ShowAsync(new ToastModel { Title = ToastTitle, Content = ToastContent, Timeout = ToastTimeout });
}
await ToastObj.ShowAsync(new ToastModel { Title = ToastTitle, Content = ToastContent, Timeout = ToastTimeout });
}

private async Task UpdateLocationAsync(Location Location)
Expand All @@ -299,41 +275,7 @@ private async Task UpdateLocationAsync(Location Location)
await ToastObj.ShowAsync(new ToastModel { Title = ToastTitle, Content = ToastContent, Timeout = ToastTimeout });
}

private async Task CreateContentAsync(int locationId, string LocationName, List<string> names, ContentType type)
{
foreach (var pageName in names)
{
var seedHtml = WebHelper.GetHtml($"{pageName}.html");
var name = string.Empty;
switch (type)
{
case ContentType.Home:
name = "Home";
break;
case ContentType.Header:
name = "Header";
seedHtml = seedHtml.Replace("Template", LocationName);
break;
case ContentType.Footer:
name = "Footer";
seedHtml = seedHtml.Replace("Template", LocationName);
break;
default:
name = pageName;
break;

}
Content content = new Content
{
LocationId = locationId,
ContentType = type,
Name = name,
ContentHtml = seedHtml,
};

var result = await _svcContent.CreateAsync(content);
}
}

private void BeginEdit()
{
Expand Down
11 changes: 10 additions & 1 deletion BedBrigade.Client/Components/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
@page "/"
@page "/{mylocation:nonfile}"
@page "/{mylocation}/{mypageName:nonfile}"
@using BedBrigade.Client.Services
@using Serilog

<MyBody @ref="myBody"/>

@code {
MyBody myBody;
[Inject] private IJSRuntime _js { get; set; }

[Inject]
private IHeaderLocationState _headerLocationState { get; set; }

[Inject]
private IFooterLocationState _footerLocationState { get; set; }

[Parameter] public string? mylocation { get; set; }
[Parameter] public string? mypageName { get; set; }
public string location { get; set; }
Expand All @@ -19,7 +27,8 @@
{
location = string.IsNullOrEmpty(mylocation) ? defaultLocation : mylocation;
pageName = string.IsNullOrEmpty(mypageName) ? defaultPageName : mypageName;

_headerLocationState.Location = location; // Update the state
_footerLocationState.Location = location + string.Empty; // Update the state
mylocation = null;
mypageName = null;
}
Expand Down
23 changes: 23 additions & 0 deletions BedBrigade.Client/Services/FooterLocationState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace BedBrigade.Client.Services
{
public class FooterLocationState : IFooterLocationState
{
private string _location;
public string Location
{
get => _location;
set
{
if (_location != value)
{
_location = value;
NotifyStateChanged();
}
}
}

public event Action OnChange;

private void NotifyStateChanged() => OnChange?.Invoke();
}
}
23 changes: 23 additions & 0 deletions BedBrigade.Client/Services/HeaderLocationState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace BedBrigade.Client.Services
{
public class HeaderLocationState : IHeaderLocationState
{
private string _location;
public string Location
{
get => _location;
set
{
if (_location != value)
{
_location = value;
NotifyStateChanged();
}
}
}

public event Action OnChange;

private void NotifyStateChanged() => OnChange?.Invoke();
}
}
8 changes: 8 additions & 0 deletions BedBrigade.Client/Services/IFooterLocationState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace BedBrigade.Client.Services
{
public interface IFooterLocationState
{
string Location { get; set; }
event Action OnChange;
}
}
8 changes: 8 additions & 0 deletions BedBrigade.Client/Services/IHeaderLocationState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace BedBrigade.Client.Services
{
public interface IHeaderLocationState
{
string Location { get; set; }
event Action OnChange;
}
}
Loading

0 comments on commit 7ac05fe

Please sign in to comment.