-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#218 - Events for managing sign-in and out notifications on client.
- Loading branch information
Showing
10 changed files
with
168 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Money.Events | ||
{ | ||
/// <summary> | ||
/// An event raised when a user has signed in. | ||
/// </summary> | ||
public class UserSignedIn : UserEvent | ||
{ } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Money.Events | ||
{ | ||
/// <summary> | ||
/// An event raised when a user has signed in. | ||
/// </summary> | ||
public class UserSignedOut : UserEvent | ||
{ } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,17 @@ | ||
@inject IQueryDispatcher Queries | ||
@inject Navigator Navigator | ||
@inject ApiClient ApiClient | ||
@using Money.Layouts | ||
@inherits LoginInfoBase | ||
|
||
<form class="navbar-right" onsubmit="@OnLogoutAsync"> | ||
<ul class="nav navbar-nav navbar-right"> | ||
<li> | ||
<a href="@Navigator.UrlUserManage()" onclick="@(() => Navigator.OpenUserManage())"> | ||
<a href="@Navigator.UrlUserManage()"> | ||
<Loading Context="@Loading"> | ||
User: @Profile.UserName | ||
User: @(Profile?.UserName ?? "---") | ||
</Loading> | ||
</a> | ||
</li> | ||
<li> | ||
<button type="submit" class="btn btn-link navbar-btn navbar-link">Log out</button> | ||
<button type="button" class="btn btn-link navbar-btn navbar-link" onclick="@OnLogoutAsync">Log out</button> | ||
</li> | ||
</ul> | ||
</form> | ||
|
||
@functions | ||
{ | ||
public ProfileModel Profile { get; private set; } | ||
|
||
protected LoadingContext Loading { get; } = new LoadingContext(); | ||
|
||
protected override async Task OnInitAsync() | ||
{ | ||
using (Loading.Start()) | ||
Profile = await Queries.QueryAsync(new GetProfile()); | ||
} | ||
|
||
protected async Task OnLogoutAsync() | ||
{ | ||
await ApiClient.LogoutAsync(); | ||
Navigator.OpenLogin(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using Microsoft.AspNetCore.Blazor.Components; | ||
using Money.Events; | ||
using Money.Models; | ||
using Money.Models.Loading; | ||
using Money.Queries; | ||
using Money.Services; | ||
using Neptuo.Events; | ||
using Neptuo.Events.Handlers; | ||
using Neptuo.Queries; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Money.Layouts | ||
{ | ||
public class LoginInfoBase : BlazorComponent, IDisposable, IEventHandler<UserSignedIn>, IEventHandler<UserSignedOut> | ||
{ | ||
[Inject] | ||
public IQueryDispatcher Queries { get; set; } | ||
|
||
[Inject] | ||
public IEventHandlerCollection EventHandlers { get; set; } | ||
|
||
[Inject] | ||
internal Navigator Navigator { get; set; } | ||
|
||
[Inject] | ||
public ApiClient ApiClient { get; set; } | ||
|
||
public ProfileModel Profile { get; private set; } | ||
protected LoadingContext Loading { get; } = new LoadingContext(); | ||
|
||
protected async override Task OnInitAsync() | ||
{ | ||
BindEvents(); | ||
|
||
await LoadProfileAsync(); | ||
} | ||
|
||
private async Task LoadProfileAsync() | ||
{ | ||
using (Loading.Start()) | ||
Profile = await Queries.QueryAsync(new GetProfile()); | ||
} | ||
|
||
protected async Task OnLogoutAsync() | ||
{ | ||
await ApiClient.LogoutAsync(); | ||
Navigator.OpenLogin(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
UnBindEvents(); | ||
} | ||
|
||
#region Events | ||
|
||
private void BindEvents() | ||
{ | ||
EventHandlers | ||
.Add<UserSignedIn>(this) | ||
.Add<UserSignedOut>(this); | ||
} | ||
|
||
private void UnBindEvents() | ||
{ | ||
EventHandlers | ||
.Remove<UserSignedIn>(this) | ||
.Remove<UserSignedOut>(this); | ||
} | ||
|
||
async Task IEventHandler<UserSignedIn>.HandleAsync(UserSignedIn payload) | ||
{ | ||
await LoadProfileAsync(); | ||
StateHasChanged(); | ||
} | ||
|
||
Task IEventHandler<UserSignedOut>.HandleAsync(UserSignedOut payload) | ||
{ | ||
Profile = null; | ||
StateHasChanged(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters