@using Microsoft.AspNetCore.Authorization;
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
@using MyRecipes2.Shared;
@using System;
@using System.IO;
@using System.Text;
<!-- Ingredients -->
<div>
@foreach (var ingredient in Ingredients)
{
<div class="form-row">
<div class="form-group col-3">
<input @bind="ingredient.Name" type="text" class="form-control" placeholder="Ingredient">
</div>
<div class="form-group col-2">
<input @bind="ingredient.Quantity" type="number" class="form-control" placeholder="Quantity">
</div>
<div class="form-group col-2">
<select class="form-control" @bind="ingredient.Unit">
@foreach (var unit in Enum.GetValues(typeof(IngredientUnit)))
{
<option value="@unit">@unit</option>
}
</select>
</div>
<div class="form-group col-3">
<input class="form-control" type="number" placeholder="Calories" @bind="ingredient.Calories" min="0" />
</div>
<div class="col-1">calories</div>
<div class="col-1">
<button @onclick="() => ClickRemoveIngredient(ingredient)" class="btn btn-danger">Remove</button>
</div>
</div>
}
<div class="form-row">
<div class="form-group col-3">
<input @bind="CurrentIngredient.Name" type="text" class="form-control" placeholder="Ingredient">
<span class="text-danger">
@IngredientNameErrorText
</span>
</div>
<div class="form-group col-2">
<input @bind="CurrentIngredient.Quantity" type="number" class="form-control" placeholder="Quantity">
<span class="text-danger">
@QuantityErrorText
</span>
</div>
<div class="form-group col-2">
<select class="form-control" @bind="CurrentIngredient.Unit">
@foreach (var unit in Enum.GetValues(typeof(IngredientUnit)))
{
<option value="@unit">@unit</option>
}
</select>
</div>
<div class="form-group col-3">
<input class="form-control" type="number" placeholder="Calories" @bind="CurrentIngredient.Calories" min="0"/>
</div>
<div class="col-1">calories</div>
<div class="col-1">
<button @onclick="ClickAddIngredient" class="btn btn-primary">Add</button>
</div>
</div>
</div>
@code {
public List<IngredientDto> Ingredients { get; set; }
public void SetIngredients(List<IngredientDto> ingredients)
{
Ingredients = ingredients;
StateHasChanged();
}
IngredientDto CurrentIngredient { get; set; }
private string IngredientNameErrorText { get; set; }
private string QuantityErrorText { get; set; }
protected override void OnInitialized()
{
Ingredients = new List<IngredientDto>();
CurrentIngredient = new();
}
protected bool IsValid()
{
bool result = true;
IngredientNameErrorText = "";
QuantityErrorText = "";
if (string.IsNullOrWhiteSpace(CurrentIngredient.Name))
{
IngredientNameErrorText = "Please enter an ingredient name";
result = false;
}
if (CurrentIngredient.Quantity == 0)
{
QuantityErrorText = "Please enter a quanitity other than zero";
result = false;
}
return result;
}
protected void ClickAddIngredient()
{
if (!IsValid()) return;
Ingredients.Add(CurrentIngredient);
CurrentIngredient = new();
}
protected void ClickRemoveIngredient(IngredientDto ingredient)
{
Ingredients.Remove(ingredient);
}
}