Skip to content

Commit

Permalink
#289 - Displaying user selected bottom menu items.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed May 28, 2021
1 parent dd6b023 commit 35dd2b6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
48 changes: 46 additions & 2 deletions src/Money.Blazor.Host/Layouts/BottomMenu.razor.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,21 +17,61 @@

namespace Money.Layouts
{
public partial class BottomMenu
public partial class BottomMenu : IDisposable,
IEventHandler<UserPropertyChanged>
{
[Inject]
protected Navigator Navigator { get; set; }

[Inject]
protected IQueryDispatcher Queries { get; set; }

[Inject]
protected IEventHandlerCollection EventHandlers { get; set; }

[Inject]
protected AuthenticationStateProvider AuthenticationStateProvider { get; set; }

protected List<IActionMenuItemModel> Items { get; set; }

protected override void OnInitialized()
{
base.OnInitialized();

EventHandlers.Add<UserPropertyChanged>(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<UserPropertyChanged>(this);
}

private async void OnAuthenticationStateChanged(Task<AuthenticationState> 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<UserPropertyChanged>.HandleAsync(UserPropertyChanged payload)
{
await LoadAsync();
StateHasChanged();
}
}
}
5 changes: 4 additions & 1 deletion src/Money.Blazor.Host/MenuItems/MenuItemService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ async Task<object> HttpQueryDispatcher.IMiddleware.ExecuteAsync(object query, Ht
{
if (query is ListBottomMenuItem)
{
return storage.ToList<IActionMenuItemModel>();
var value = await dispatcher.QueryAsync(new FindUserProperty("MobileMenu"));
var selectedItems = value.Split(',');

return storage.Where(m => selectedItems.Contains(m.Identifier)).ToList<IActionMenuItemModel>();
}
else if (query is ListAvailableMenuItem)
{
Expand Down

0 comments on commit 35dd2b6

Please sign in to comment.