groceries/Groceries/Transactions/NewTransactionItemsPage.razor

82 lines
3.2 KiB
Plaintext

@using Groceries.Data
@using Microsoft.EntityFrameworkCore
@layout Layout
@inject AppDbContext DbContext
<PageTitle>Groceries &ndash; New Transaction</PageTitle>
<h1>New Transaction</h1>
<div class="form-field">
@Transaction.CreatedAt.ToShortDateString() @Transaction.CreatedAt.ToLongTimeString() &ndash; @store
</div>
<form method="post">
<section class="card form-field">
<div class="card__header row">
<h2 class="row__fill">Items</h2>
<a class="button button--primary" href="/transactions/new/items/new" autofocus data-turbo-frame="modal">
New item
</a>
</div>
<div class="card__content card__content--table">
<Table Items="Transaction.Items.AsQueryable()" CellClass="table__cell--compact">
<ChildContent>
<TemplateTableColumn Title="Name" Fill="true" Context="item">
@itemNames.GetValueOrDefault(item.ItemId)
</TemplateTableColumn>
<PropertyTableColumn Property="i => i.Price" Format="c" />
<PropertyTableColumn Property="i => i.Quantity">
<HeaderContent>
<abbr title="Quantity">Qty</abbr>
</HeaderContent>
</PropertyTableColumn>
<PropertyTableColumn Property="i => i.Amount" Format="c" />
<TemplateTableColumn Context="item">
<a class="link" href="/transactions/new/items/edit/@item.ItemId" data-turbo-frame="modal">
Edit
</a>
</TemplateTableColumn>
</ChildContent>
<FooterContent>
<tr>
<td class="table__cell table__cell--compact table__cell--total" colspan="3">Subtotal</td>
<td class="table__cell table__cell--compact table__cell--total table__cell--align-end">
@Transaction.Items.Sum(item => item.Amount).ToString("c")
</td>
<td class="table__cell table__cell--compact"></td>
</tr>
</FooterContent>
</Table>
</div>
</section>
<div class="row">
<button class="button button--primary" type="submit" disabled="@(Transaction.Items.Count == 0)">Next</button>
<a class="button" href="/transactions/new">Back</a>
</div>
</form>
@code {
private string store = string.Empty;
private Dictionary<Guid, string> itemNames = new();
[Parameter]
public required Transaction Transaction { get; set; }
protected override async Task OnParametersSetAsync()
{
store = await DbContext.Stores
.Where(store => store.Id == Transaction.StoreId)
.Select(store => string.Concat(store.Retailer!.Name, " ", store.Name))
.SingleAsync();
var itemIds = Transaction.Items.Select(item => item.ItemId);
itemNames = await DbContext.Items
.Where(item => itemIds.Contains(item.Id))
.ToDictionaryAsync(item => item.Id, item => string.Concat(item.Brand, " ", item.Name));
}
}