@page "/"
Coffee order
Coffee order
Order
@cups × @size@(oatMilk ? " with oat milk" : "") for @(string.IsNullOrWhiteSpace(name) ? "a guest" : name)
Pickup on @pickup.ToString("dddd, d MMMM yyyy", CultureInfo.InvariantCulture)
Total: @Total.ToString("0.00", CultureInfo.InvariantCulture)
Current culture: @(CultureInfo.CurrentCulture.Name == "" ? "invariant" : CultureInfo.CurrentCulture.Name)
@code {
private string name = "";
private string note = "";
private CupSize size = CupSize.Medium;
private bool oatMilk;
private int cups = 2;
private DateOnly pickup = DateOnly.FromDateTime(DateTime.Today.AddDays(1));
private string voucher = "";
private string stockMessage = "Change the size to run @bind:after.";
private bool VoucherApplied => voucher == "COFFEE10";
private decimal PricePerCup => size switch
{
CupSize.Small => 2.80m,
CupSize.Medium => 3.20m,
_ => 3.60m,
} + (oatMilk ? 0.40m : 0m);
private decimal Total => Math.Round(cups * PricePerCup * (VoucherApplied ? 0.9m : 1m), 2);
// Runs after @bind has stored the new size. It may be async.
private async Task CheckStockAsync()
{
stockMessage = $"Checking {size} cups…";
await Task.Delay(600);
stockMessage = $"{size} cups are in stock. {PricePerCup.ToString("0.00", CultureInfo.InvariantCulture)} each.";
}
// Called with the raw text on every keystroke. Keep letters and digits, upper-case, at most 8.
private void SetVoucher(string? value)
{
var kept = new string((value ?? "").Where(char.IsLetterOrDigit).ToArray()).ToUpperInvariant();
voucher = kept.Length > 8 ? kept[..8] : kept;
}
public enum CupSize { Small, Medium, Large }
}