diff --git a/src/Money.Blazor.Host/Layouts/BottomMenu.razor.cs b/src/Money.Blazor.Host/Layouts/BottomMenu.razor.cs index 080f3e26..7dad9c67 100644 --- a/src/Money.Blazor.Host/Layouts/BottomMenu.razor.cs +++ b/src/Money.Blazor.Host/Layouts/BottomMenu.razor.cs @@ -1,8 +1,12 @@ using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Authorization; +using Money.Events; using Money.Models; using Money.Models.Queries; using Money.Pages; using Money.Services; +using Neptuo.Events; +using Neptuo.Events.Handlers; using Neptuo.Queries; using System; using System.Collections.Generic; @@ -13,7 +17,8 @@ namespace Money.Layouts { - public partial class BottomMenu + public partial class BottomMenu : IDisposable, + IEventHandler { [Inject] protected Navigator Navigator { get; set; } @@ -21,13 +26,52 @@ public partial class BottomMenu [Inject] protected IQueryDispatcher Queries { get; set; } + [Inject] + protected IEventHandlerCollection EventHandlers { get; set; } + + [Inject] + protected AuthenticationStateProvider AuthenticationStateProvider { get; set; } + protected List Items { get; set; } + protected override void OnInitialized() + { + base.OnInitialized(); + + EventHandlers.Add(this); + AuthenticationStateProvider.AuthenticationStateChanged += OnAuthenticationStateChanged; + } + protected async override Task OnInitializedAsync() { await base.OnInitializedAsync(); + await LoadAsync(); + } - Items = await Queries.QueryAsync(new ListBottomMenuItem()); + public void Dispose() + { + EventHandlers.Remove(this); + } + + private async void OnAuthenticationStateChanged(Task task) + { + await LoadAsync(); + StateHasChanged(); + } + + private async Task LoadAsync() + { + var state = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + if (state.User.Identity.IsAuthenticated) + Items = await Queries.QueryAsync(new ListBottomMenuItem()); + else + Items = null; + } + + async Task IEventHandler.HandleAsync(UserPropertyChanged payload) + { + await LoadAsync(); + StateHasChanged(); } } } diff --git a/src/Money.Blazor.Host/MenuItems/MenuItemService.cs b/src/Money.Blazor.Host/MenuItems/MenuItemService.cs index f66364bc..b38dc7ce 100644 --- a/src/Money.Blazor.Host/MenuItems/MenuItemService.cs +++ b/src/Money.Blazor.Host/MenuItems/MenuItemService.cs @@ -64,7 +64,10 @@ async Task HttpQueryDispatcher.IMiddleware.ExecuteAsync(object query, Ht { if (query is ListBottomMenuItem) { - return storage.ToList(); + var value = await dispatcher.QueryAsync(new FindUserProperty("MobileMenu")); + var selectedItems = value.Split(','); + + return storage.Where(m => selectedItems.Contains(m.Identifier)).ToList(); } else if (query is ListAvailableMenuItem) {