Skip to content

Commit

Permalink
#343 - Handle income created / delete events on Summary.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed May 16, 2021
1 parent 25b998b commit dd5c505
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/Money.Blazor.Host/Pages/Summary.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public partial class Summary<T> :
IEventHandler<OutcomeDeleted>,
IEventHandler<OutcomeAmountChanged>,
IEventHandler<OutcomeWhenChanged>,
IEventHandler<PulledToRefresh>
IEventHandler<PulledToRefresh>,
IEventHandler<IncomeCreated>,
IEventHandler<IncomeDeleted>
{
private CurrencyFormatter formatter;

Expand Down Expand Up @@ -118,11 +120,7 @@ protected async Task LoadSelectedPeriodAsync()
{
Categories = await Queries.QueryAsync(CreateCategoriesQuery(SelectedPeriod));

var incomeQuery = CreateIncomeTotalQuery(SelectedPeriod);
if (incomeQuery != null)
IncomeTotal = await Queries.QueryAsync(CreateIncomeTotalQuery(SelectedPeriod));
else
IncomeTotal = null;
await LoadIncomeTotalAsync();

ExpenseTotal = await Queries.QueryAsync(CreateExpenseTotalQuery(SelectedPeriod));
}
Expand All @@ -136,6 +134,15 @@ protected async Task LoadSelectedPeriodAsync()
}
}

private async Task LoadIncomeTotalAsync()
{
var incomeQuery = CreateIncomeTotalQuery(SelectedPeriod);
if (incomeQuery != null)
IncomeTotal = await Queries.QueryAsync(CreateIncomeTotalQuery(SelectedPeriod));
else
IncomeTotal = null;
}

protected virtual IQuery<Price> CreateIncomeTotalQuery(T item)
=> null;

Expand Down Expand Up @@ -203,7 +210,9 @@ private void BindEvents()
.Add<OutcomeDeleted>(this)
.Add<OutcomeAmountChanged>(this)
.Add<OutcomeWhenChanged>(this)
.Add<PulledToRefresh>(this);
.Add<PulledToRefresh>(this)
.Add<IncomeCreated>(this)
.Add<IncomeDeleted>(this);
}

private void UnBindEvents()
Expand All @@ -213,7 +222,9 @@ private void UnBindEvents()
.Remove<OutcomeDeleted>(this)
.Remove<OutcomeAmountChanged>(this)
.Remove<OutcomeWhenChanged>(this)
.Remove<PulledToRefresh>(this);
.Remove<PulledToRefresh>(this)
.Remove<IncomeCreated>(this)
.Remove<IncomeDeleted>(this);
}

Task IEventHandler<OutcomeCreated>.HandleAsync(OutcomeCreated payload)
Expand Down Expand Up @@ -247,6 +258,18 @@ Task IEventHandler<PulledToRefresh>.HandleAsync(PulledToRefresh payload)
return Task.CompletedTask;
}

async Task IEventHandler<IncomeCreated>.HandleAsync(IncomeCreated payload)
{
await LoadIncomeTotalAsync();
StateHasChanged();
}

async Task IEventHandler<IncomeDeleted>.HandleAsync(IncomeDeleted payload)
{
await LoadIncomeTotalAsync();
StateHasChanged();
}

private async void OnMonthUpdatedEvent(DateTime changed)
{
if (Periods != null && !IsContained(changed))
Expand Down

0 comments on commit dd5c505

Please sign in to comment.