-
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.
- Loading branch information
Showing
5 changed files
with
182 additions
and
10 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,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> |
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,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(); | ||
} | ||
} | ||
} |
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
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
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