Skip to content

Commit

Permalink
#404 - Process expense change events on search page.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Aug 2, 2021
1 parent 2437811 commit 93c86ec
Showing 1 changed file with 101 additions and 1 deletion.
102 changes: 101 additions & 1 deletion src/Money.Blazor.Host/Pages/Search.razor.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Microsoft.AspNetCore.Components;
using Money.Components;
using Money.Events;
using Money.Models;
using Money.Models.Loading;
using Money.Models.Queries;
using Money.Models.Sorting;
using Money.Services;
using Neptuo.Events;
using Neptuo.Events.Handlers;
using Neptuo.Queries;
using System;
using System.Collections.Generic;
Expand All @@ -14,7 +17,13 @@

namespace Money.Pages
{
public partial class Search : System.IDisposable
public partial class Search : System.IDisposable,
IEventHandler<OutcomeCreated>,
IEventHandler<OutcomeDeleted>,
IEventHandler<OutcomeAmountChanged>,
IEventHandler<OutcomeDescriptionChanged>,
IEventHandler<OutcomeWhenChanged>,
IEventHandler<PulledToRefresh>
{
public static readonly SortDescriptor<OutcomeOverviewSortType> DefaultSort = new SortDescriptor<OutcomeOverviewSortType>(OutcomeOverviewSortType.ByWhen, SortDirection.Descending);

Expand All @@ -23,6 +32,9 @@ public partial class Search : System.IDisposable
[Inject]
protected Navigator Navigator { get; set; }

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

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

Expand All @@ -46,6 +58,7 @@ protected async override Task OnInitializedAsync()
PagingContext = new PagingContext(LoadPageAsync, Loading);
CurrencyFormatter = await CurrencyFormatterFactory.CreateAsync();
Navigator.LocationChanged += OnLocationChanged;
BindEvents();
}

protected override async Task OnParametersSetAsync()
Expand All @@ -57,6 +70,7 @@ protected override async Task OnParametersSetAsync()
public void Dispose()
{
Navigator.LocationChanged -= OnLocationChanged;
UnBindEvents();
}

private async void OnLocationChanged(string url)
Expand Down Expand Up @@ -101,5 +115,91 @@ protected async Task<PagingLoadStatus> LoadPageAsync()
return PagingLoadStatus.LastPage;
}
}

protected OutcomeOverviewModel FindModel(IEvent payload)
=> Models.FirstOrDefault(o => o.Key.Equals(payload.AggregateKey));

#region Events

private void BindEvents()
{
EventHandlers
.Add<OutcomeCreated>(this)
.Add<OutcomeDeleted>(this)
.Add<OutcomeAmountChanged>(this)
.Add<OutcomeDescriptionChanged>(this)
.Add<OutcomeWhenChanged>(this)
.Add<PulledToRefresh>(this);
}

private void UnBindEvents()
{
EventHandlers
.Remove<OutcomeCreated>(this)
.Remove<OutcomeDeleted>(this)
.Remove<OutcomeAmountChanged>(this)
.Remove<OutcomeDescriptionChanged>(this)
.Remove<OutcomeWhenChanged>(this)
.Remove<PulledToRefresh>(this);
}

private Task UpdateModel(IEvent payload, Action<OutcomeOverviewModel> handler)
{
OutcomeOverviewModel model = FindModel(payload);
if (model != null)
{
handler(model);
StateHasChanged();
}

return Task.CompletedTask;
}

Task IEventHandler<OutcomeCreated>.HandleAsync(OutcomeCreated payload)
{
_ = LoadPageAsync();
return Task.CompletedTask;
}

Task IEventHandler<OutcomeDeleted>.HandleAsync(OutcomeDeleted payload)
{
_ = LoadPageAsync();
return Task.CompletedTask;
}

Task IEventHandler<OutcomeAmountChanged>.HandleAsync(OutcomeAmountChanged payload)
{
if (Sort.Type == OutcomeOverviewSortType.ByAmount)
_ = LoadPageAsync();
else
UpdateModel(payload, model => model.Amount = payload.NewValue);

return Task.CompletedTask;
}

Task IEventHandler<OutcomeDescriptionChanged>.HandleAsync(OutcomeDescriptionChanged payload)
{
_ = LoadPageAsync();
return Task.CompletedTask;
}

Task IEventHandler<OutcomeWhenChanged>.HandleAsync(OutcomeWhenChanged payload)
{
if (Sort.Type == OutcomeOverviewSortType.ByWhen)
_ = LoadPageAsync();
else
UpdateModel(payload, model => model.When = payload.When);

return Task.CompletedTask;
}

async Task IEventHandler<PulledToRefresh>.HandleAsync(PulledToRefresh payload)
{
payload.IsHandled = true;
await LoadPageAsync();
StateHasChanged();
}

#endregion
}
}

0 comments on commit 93c86ec

Please sign in to comment.