Skip to content

Commit

Permalink
#343 - Dialog for creating income.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed May 15, 2021
1 parent 26603b0 commit f7cf4e0
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 10 deletions.
42 changes: 42 additions & 0 deletions src/Money.Blazor.Host/Components/IncomeCreate.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@inherits ModalDialog

<Modal @ref="Modal" Title="@Title">
<ChildContent>
<Validation ErrorMessages="@ErrorMessages" />
<div class="form-row">
<div class="col-md-9">
<div class="form-group">
<label for="expense-amount">Amount:</label>
<input type="text" class="form-control" id="expense-amount" @bind="@Amount" autocomplete="off" data-autofocus data-select />
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="expense-currency">Currency:</label>
<select class="form-control" id="expense-currency" @bind="@Currency">
@if (Currencies != null)
{
@foreach (var currency in Currencies)
{
<option value="@currency.UniqueCode">
@currency.Symbol
</option>
}
}
</select>
</div>
</div>
</div>
<div class="form-group">
<label for="expense-description">Description:</label>
<input type="text" class="form-control" id="expense-description" @bind="@Description" />
</div>
<div class="form-group">
<label for="expense-when">When:</label>
<input type="date" class="form-control" id="expense-when" @bind="@When" @bind:format="yyyy-MM-dd" />
</div>
</ChildContent>
<Buttons>
<button class="btn btn-primary" @onclick="@OnSaveClick">@SaveButtonText</button>
</Buttons>
</Modal>
108 changes: 108 additions & 0 deletions src/Money.Blazor.Host/Components/IncomeCreate.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using Microsoft.AspNetCore.Components;
using Money.Commands;
using Money.Models;
using Money.Models.Queries;
using Money.Services;
using Neptuo.Commands;
using Neptuo.Logging;
using Neptuo.Queries;
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 IncomeCreate
{
[Inject]
protected ICommandDispatcher Commands { get; set; }

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

[Inject]
internal ILog<IncomeCreate> Log { get; set; }

[Inject]
internal Navigator Navigator { get; set; }

protected string Title { get; set; }
protected string SaveButtonText { get; set; }
protected List<string> ErrorMessages { get; } = new List<string>();

protected List<CurrencyModel> Currencies { get; private set; }

protected Confirm PrerequisitesConfirm { get; set; }

[Parameter]
public decimal Amount { get; set; }

[Parameter]
public string Currency { get; set; }

[Parameter]
public string Description { get; set; }

[Parameter]
public DateTime When { get; set; } = DateTime.Today;

protected async override Task OnParametersSetAsync()
{
await base.OnParametersSetAsync();

Title = "Create a new Income";
SaveButtonText = "Create";

Currencies = await Queries.QueryAsync(new ListAllCurrency());
Currency = await Queries.QueryAsync(new FindCurrencyDefault());
}

protected void OnSaveClick()
{
if (Validate())
{
Execute();
Modal.Hide();
}
}

private bool Validate()
{
Log.Debug($"Income: Amount: {Amount}, Currency: {Currency}, When: {When}.");

ErrorMessages.Clear();
Validator.AddIncomeAmount(ErrorMessages, Amount);
Validator.AddIncomeDescription(ErrorMessages, Description);
Validator.AddIncomeCurrency(ErrorMessages, Currency);

Log.Debug($"Income: Validation: {string.Join(", ", ErrorMessages)}.");
return ErrorMessages.Count == 0;
}

private async void Execute()
{
await Commands.HandleAsync(new CreateIncome(new Price(Amount, Currency), Description, When));

Amount = 0;
Description = null;
StateHasChanged();
}

public override void Show()
{
if (Currencies == null || Currencies.Count == 0)
PrerequisitesConfirm.Show();
else
base.Show();
}

protected void OnPrerequisitesConfirmed()
{
if (Currencies == null || Currencies.Count == 0)
Navigator.OpenCurrencies();
}
}
}
7 changes: 4 additions & 3 deletions src/Money.Blazor.Host/Pages/Summary.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<Title Icon="signal" Main="Summary" Sub="@SubTitle">
<ButtonContent>
<button class="btn btn-primary ml-2" @onclick="CreateNewExpense">
<button class="btn btn-primary ml-2" @onclick="ExpenseCreate.Show">
<Icon Identifier="minus-circle" />
<span class="d-none d-md-inline">
New Expense
</span>
</button>
<button class="btn btn-primary ml-2">
<button class="btn btn-primary ml-2" @onclick="IncomeCreate.Show">
<Icon Identifier="plus-circle" />
<span class="d-none d-md-inline">
New Income
Expand All @@ -18,7 +18,8 @@
</Title>
<ExceptionPanel />

<OutcomeCreate @ref="CreateModal" />
<OutcomeCreate @ref="ExpenseCreate" />
<IncomeCreate @ref="IncomeCreate" />

<div class="summary">
<ExpenseBagPublishButton Text="You have expenses created when offline. Upload them now..." class="mb-4" />
Expand Down
9 changes: 2 additions & 7 deletions src/Money.Blazor.Host/Pages/Summary.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public partial class Summary<T> :
protected SortDescriptor<SummarySortType> SortDescriptor { get; set; }

protected Modal PeriodSelectorModal { get; set; }
protected ModalDialog CreateModal { get; set; }
protected OutcomeCreate ExpenseCreate { get; set; }
protected IncomeCreate IncomeCreate { get; set; }

protected override async Task OnInitializedAsync()
{
Expand Down Expand Up @@ -262,12 +263,6 @@ private async void OnOutcomeDeletedEvent()
StateHasChanged();
}

protected void CreateNewExpense()
{
CreateModal.Show();
StateHasChanged();
}

protected async Task OpenPeriodSelectorAsync()
{
PeriodSelectorModal.Show();
Expand Down
26 changes: 26 additions & 0 deletions src/Money.Blazor.Host/Services/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@ public static void AddOutcomeCategoryKey(ICollection<string> messages, IKey cate

#endregion

#region Income

public static bool IsIncomeAmount(decimal amount) => amount > 0;
public static bool IsIncomeDescription(string description) => !String.IsNullOrEmpty(description);
public static bool IsIncomeCurrency(string currency) => !String.IsNullOrEmpty(currency);

public static void AddIncomeAmount(ICollection<string> messages, decimal amount)
{
if (!IsIncomeAmount(amount))
messages.Add("Amount must be greater than zero.");
}

public static void AddIncomeDescription(ICollection<string> messages, string description)
{
if (!IsIncomeDescription(description))
messages.Add("Description must be provided.");
}

public static void AddIncomeCurrency(ICollection<string> messages, string currency)
{
if (!IsIncomeCurrency(currency))
messages.Add("Currency must be selected.");
}

#endregion

#region Category

public static bool IsCategoryName(string name) => !String.IsNullOrEmpty(name);
Expand Down

0 comments on commit f7cf4e0

Please sign in to comment.