Skip to content

Commit

Permalink
#382 - Use ExpenseCardContext where appropriate.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Jun 9, 2021
1 parent 94121df commit 4c7528a
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 94 deletions.
10 changes: 2 additions & 8 deletions src/Money.Blazor.Host/Pages/Overview.razor
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
</Title>
<ExceptionPanel />

<Confirm @ref="DeleteConfirm" Message="@DeleteMessage" OnConfirmed="@OnDeleteConfirmed" />

<div class="overview">
<Loading Context="@Loading" IsOverlay="true">
@{
Expand Down Expand Up @@ -55,12 +53,12 @@
<div class="clear"></div>

<div class="cards">
<CascadingValue Value="@this">
<ExpenseCardContext>
@foreach (var item in Items)
{
<OutcomeCard Model="@item" />
}
</CascadingValue>
</ExpenseCardContext>
</div>

<Paging Context="@PagingContext" />
Expand All @@ -72,7 +70,3 @@
}
</Loading>
</div>

<OutcomeAmount @ref="AmountEditModal" OutcomeKey="@SelectedItem?.Key" Amount="@(SelectedItem?.Amount?.Value ?? 0)" Currency="@SelectedItem?.Amount?.Currency" />
<OutcomeDescription @ref="DescriptionEditModal" OutcomeKey="@SelectedItem?.Key" Description="@SelectedItem?.Description" />
<OutcomeWhen @ref="WhenEditModal" OutcomeKey="@SelectedItem?.Key" When="@(SelectedItem?.When ?? DateTime.MinValue)" />
57 changes: 1 addition & 56 deletions src/Money.Blazor.Host/Pages/Overview.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ namespace Money.Pages
{
public partial class Overview<T> :
System.IDisposable,
OutcomeCard.IContext,
IEventHandler<OutcomeCreated>,
IEventHandler<OutcomeDeleted>,
IEventHandler<OutcomeAmountChanged>,
IEventHandler<OutcomeDescriptionChanged>,
IEventHandler<OutcomeWhenChanged>,
IEventHandler<PulledToRefresh>
{
public CurrencyFormatter CurrencyFormatter { get; private set; }

[Inject]
public ICommandDispatcher Commands { get; set; }

Expand All @@ -48,30 +45,18 @@ public partial class Overview<T> :
[Inject]
public Navigator Navigator { get; set; }

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

protected string Title { get; set; }
protected string SubTitle { get; set; }

protected T SelectedPeriod { get; set; }
protected IKey CategoryKey { get; set; }
protected string CategoryName { get; set; }
protected List<OutcomeOverviewModel> Items { get; set; }
protected OutcomeOverviewModel SelectedItem { get; set; }

protected ModalDialog AmountEditModal { get; set; }
protected ModalDialog DescriptionEditModal { get; set; }
protected ModalDialog WhenEditModal { get; set; }

protected LoadingContext Loading { get; } = new LoadingContext();
protected SortDescriptor<OutcomeOverviewSortType> SortDescriptor { get; set; } = new SortDescriptor<OutcomeOverviewSortType>(OutcomeOverviewSortType.ByWhen, SortDirection.Descending);
protected PagingContext PagingContext { get; set; }

protected OutcomeOverviewModel Selected { get; set; }
protected string DeleteMessage { get; set; }
protected Confirm DeleteConfirm { get; set; }

protected override async Task OnInitializedAsync()
{
PagingContext = new PagingContext(LoadDataAsync, Loading);
Expand All @@ -90,8 +75,7 @@ protected override async Task OnInitializedAsync()
{
Title = $"Expenses in {SelectedPeriod}";
}

CurrencyFormatter = await CurrencyFormatterFactory.CreateAsync();

Reload();
}

Expand Down Expand Up @@ -137,27 +121,6 @@ protected async void OnSortChanged()
StateHasChanged();
}

protected void OnActionClick(OutcomeOverviewModel model, ModalDialog modal)
{
SelectedItem = model;
modal.Show();
StateHasChanged();
}

protected void OnDeleteClick(OutcomeOverviewModel model)
{
Selected = model;
DeleteMessage = $"Do you really want to delete expense '{model.Description}'?";
DeleteConfirm.Show();
StateHasChanged();
}

protected async void OnDeleteConfirmed()
{
await Commands.HandleAsync(new DeleteOutcome(Selected.Key));
StateHasChanged();
}

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

Expand Down Expand Up @@ -262,23 +225,5 @@ async Task IEventHandler<PulledToRefresh>.HandleAsync(PulledToRefresh payload)
}

#endregion

#region OutcomeCard.IContext

bool OutcomeCard.IContext.HasEdit => true;

void OutcomeCard.IContext.EditAmount(OutcomeOverviewModel model)
=> OnActionClick(model, AmountEditModal);

void OutcomeCard.IContext.EditDescription(OutcomeOverviewModel model)
=> OnActionClick(model, DescriptionEditModal);

void OutcomeCard.IContext.EditWhen(OutcomeOverviewModel model)
=> OnActionClick(model, WhenEditModal);

void OutcomeCard.IContext.Delete(OutcomeOverviewModel model)
=> OnDeleteClick(model);

#endregion
}
}
4 changes: 2 additions & 2 deletions src/Money.Blazor.Host/Pages/Search.razor
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
else
{
<div class="cards">
<CascadingValue Value="@this">
<ExpenseCardContext>
@foreach (var item in Models)
{
<OutcomeCard Model="@item" />
}
</CascadingValue>
</ExpenseCardContext>
</div>

<Paging Context="@PagingContext" />
Expand Down
21 changes: 1 addition & 20 deletions src/Money.Blazor.Host/Pages/Search.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Money.Models.Queries;
using Money.Models.Sorting;
using Money.Services;
using Neptuo;
using Neptuo.Queries;
using System;
using System.Collections.Generic;
Expand All @@ -15,7 +14,7 @@

namespace Money.Pages
{
public partial class Search : OutcomeCard.IContext, System.IDisposable
public partial class Search : System.IDisposable
{
public static readonly SortDescriptor<OutcomeOverviewSortType> DefaultSort = new SortDescriptor<OutcomeOverviewSortType>(OutcomeOverviewSortType.ByWhen, SortDirection.Descending);

Expand Down Expand Up @@ -102,23 +101,5 @@ protected async Task<PagingLoadStatus> LoadPageAsync()
return PagingLoadStatus.LastPage;
}
}

#region OutcomeCard.IContext

bool OutcomeCard.IContext.HasEdit => false;

void OutcomeCard.IContext.EditAmount(OutcomeOverviewModel model)
=> throw Ensure.Exception.NotSupported();

void OutcomeCard.IContext.EditDescription(OutcomeOverviewModel model)
=> throw Ensure.Exception.NotSupported();

void OutcomeCard.IContext.EditWhen(OutcomeOverviewModel model)
=> throw Ensure.Exception.NotSupported();

void OutcomeCard.IContext.Delete(OutcomeOverviewModel model)
=> throw Ensure.Exception.NotSupported();

#endregion
}
}
3 changes: 0 additions & 3 deletions src/Money.Blazor.Host/wwwroot/css/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,6 @@ h4 {
max-width: 900px;
margin: 0 auto; }

.search .controls {
display: none; }

.cards {
margin: 20px 0; }
.cards .row:first-child {
Expand Down
Loading

0 comments on commit 4c7528a

Please sign in to comment.