Skip to content

Commit

Permalink
#382 - Cascading component implementing OutcomeCard.IContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Jun 9, 2021
1 parent 3fdc849 commit 94121df
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Money.Blazor.Host/Components/ExpenseCardContext.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<CascadingValue Value="@this">
@ChildContent
</CascadingValue>

<Confirm @ref="DeleteConfirm" Message="@DeleteMessage" OnConfirmed="@OnDeleteConfirmed" />
<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)" />
82 changes: 82 additions & 0 deletions src/Money.Blazor.Host/Components/ExpenseCardContext.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using Microsoft.AspNetCore.Components;
using Money.Commands;
using Money.Models;
using Money.Services;
using Neptuo.Commands;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Money.Components
{
public partial class ExpenseCardContext : OutcomeCard.IContext
{
[Inject]
public ICommandDispatcher Commands { get; set; }

[Inject]
protected CurrencyFormatterFactory CurrencyFormatterFactory { get; set; }
private CurrencyFormatter currencyFormatter;

[Parameter]
public RenderFragment ChildContent { get; set; }

#region OutcomeCard.IContext

CurrencyFormatter OutcomeCard.IContext.CurrencyFormatter => currencyFormatter;

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

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

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

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
currencyFormatter = await CurrencyFormatterFactory.CreateAsync();
}

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

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

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

0 comments on commit 94121df

Please sign in to comment.