-
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.
#382 - Cascading component implementing OutcomeCard.IContext.
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 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
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
82
src/Money.Blazor.Host/Components/ExpenseCardContext.razor.cs
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,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(); | ||
} | ||
} | ||
} |