Skip to content

Commit

Permalink
#417 - Pick template in expense create dialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Jun 20, 2022
1 parent 49ee097 commit 570bb8a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/Money.Blazor.Host/Components/OutcomeCreate.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,49 @@

<Modal @ref="Modal" Title="@Title">
<ChildContent>
@if (Templates != null)
{
<style>
.card.card-hover {
height: calc(100% - 15px);
}
</style>
<div class="form-row">
@foreach (var model in Templates)
{
<div class="col-4">
<div class="card card-hover cursor-pointer" @onclick="(() => ApplyTemplate(model))">
<div class="card-body">
@if (model.Amount != null)
{
<div>
<strong>
@CurrencyFormatter.Format(model.Amount)
</strong>
</div>
}

@if (!model.CategoryKey.IsEmpty)
{
<p class="m-0 @FindCategoryColor(model.CategoryKey)?.SelectAccent("back-dark", "back-light")">
<span class="badge" style="background-color: @FindCategoryColor(model.CategoryKey)?.ToHashCode()">@FindCategoryName(model.CategoryKey)</span>
</p>
}

@if (!String.IsNullOrEmpty(model.Description))
{
<p>
<small>
@model.Description
</small>
</p>
}
</div>
</div>
</div>
}
</div>
}
<Validation ErrorMessages="@ErrorMessages" />
<div class="form-row">
<div class="col-md-9">
Expand Down
38 changes: 37 additions & 1 deletion src/Money.Blazor.Host/Components/OutcomeCreate.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@ public partial class OutcomeCreate
[Inject]
protected Navigator.ModalContainer ModalContainer { get; set; }

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

protected CurrencyFormatter CurrencyFormatter { get; private set; }

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

protected List<ExpenseTemplateModel> Templates { get; private set; }
protected List<CategoryModel> Categories { get; private set; }
protected List<CurrencyModel> Currencies { get; private set; }

Expand Down Expand Up @@ -114,8 +120,11 @@ public async void Show(IKey categoryKey)
Ensure.NotNull(categoryKey, "categoryKey");
CategoryKey = categoryKey;

CurrencyFormatter = await CurrencyFormatterFactory.CreateAsync();

Categories = await Queries.QueryAsync(new ListAllCategory());
Currencies = await Queries.QueryAsync(new ListAllCurrency());
Templates = await Queries.QueryAsync(new ListAllExpenseTemplate());
Currency = await Queries.QueryAsync(new FindCurrencyDefault());

if (Currencies == null || Currencies.Count == 0 || Categories == null || Categories.Count == 0)
Expand All @@ -140,7 +149,7 @@ public async void Show(decimal? amount, string currency, string description, IKe
Show(categoryKey);
}

public override void Show()
public override void Show()
=> Show(KeyFactory.Empty(typeof(Category)));

protected void OnPrerequisitesConfirmed()
Expand All @@ -150,5 +159,32 @@ protected void OnPrerequisitesConfirmed()
else if (Categories == null || Categories.Count == 0)
Navigator.OpenCategories();
}

protected void ApplyTemplate(ExpenseTemplateModel model)
{
if (model.Amount != null)
{
Amount = model.Amount.Value;
Currency = model.Amount.Currency;
}

if (!String.IsNullOrEmpty(model.Description))
Description = model.Description;

if (!model.CategoryKey.IsEmpty)
CategoryKey = model.CategoryKey;
}

protected string FindCategoryName(IKey categoryKey)
{
var category = Categories?.FirstOrDefault(c => c.Key.Equals(categoryKey));
return category?.Name;
}

protected Color? FindCategoryColor(IKey categoryKey)
{
var category = Categories?.FirstOrDefault(c => c.Key.Equals(categoryKey));
return category?.Color;
}
}
}

0 comments on commit 570bb8a

Please sign in to comment.