Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default item in topping dropdown when adding/removing #367

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 70 additions & 59 deletions src/BlazingPizza.Client/Shared/ConfigurePizzaDialog.razor
Original file line number Diff line number Diff line change
@@ -1,68 +1,70 @@
@inject HttpClient HttpClient
@inject IJSRuntime JSRuntime


<div class="dialog-title">
<h2>@Pizza.Special?.Name</h2>
@Pizza.Special?.Description
</div>
<form class="dialog-body">
<div>
<label>Size:</label>
<input type="range" min="@Pizza.MinimumSize" max="@Pizza.MaximumSize" step="1" @bind="Pizza.Size" @bind:event="oninput" />
<span class="size-label">
@(Pizza.Size)" (£@(Pizza.GetFormattedTotalPrice()))
</span>
</div>
<div>
<label>Extra Toppings:</label>
@if (toppings is null)
{
<select class="custom-select" disabled>
<option>(loading...)</option>
</select>
}
else if (Pizza.Toppings.Count >= 6)
{
<div>(maximum reached)</div>
}
else
<div class="dialog-title">
<h2>@Pizza.Special?.Name</h2>
@Pizza.Special?.Description
</div>
<form class="dialog-body">
<div>
<label>Size:</label>
<input type="range" min="@Pizza.MinimumSize" max="@Pizza.MaximumSize" step="1" @bind="Pizza.Size" @bind:event="oninput" />
<span class="size-label">
@(Pizza.Size)" (£@(Pizza.GetFormattedTotalPrice()))
</span>
</div>
<div>
<label>Extra Toppings:</label>
@if (toppings is null)
{
<select class="custom-select" disabled>
<option>(loading...)</option>
</select>
}
else if (Pizza.Toppings.Count >= 6)
{
<div>(maximum reached)</div>
}
else
{
<select class="custom-select" @onchange="ToppingSelected" @ref="toppingsSelect">
<option value="-1" disabled selected>(select)</option>
@for (var i = 0; i < toppings.Count; i++)
{
<select class="custom-select" @onchange="ToppingSelected">
<option value="-1" disabled selected>(select)</option>
@for (var i = 0; i < toppings.Count; i++)
{
<option value="@i">@toppings[i].Name - (£@(toppings[i].GetFormattedPrice()))</option>
}
</select>
<option value="@i">@toppings[i].Name - (£@(toppings[i].GetFormattedPrice()))</option>
}
</div>
</select>
}
</div>

<div class="toppings">
@foreach (var topping in Pizza.Toppings)
{
if (topping?.Topping is not null)
{
<div class="topping">
@topping.Topping.Name
<span class="topping-price">@topping.Topping.GetFormattedPrice()</span>
<button type="button" class="delete-topping" @onclick="@(() => RemoveTopping(topping.Topping))">x</button>
</div>
}
}
</div>
</form>
<div class="toppings">
@foreach (var topping in Pizza.Toppings)
{
if (topping?.Topping is not null)
{
<div class="topping">
@topping.Topping.Name
<span class="topping-price">@topping.Topping.GetFormattedPrice()</span>
<button type="button" class="delete-topping" @onclick="@(() => RemoveTopping(topping.Topping))">x</button>
</div>
}
}
</div>
</form>

<div class="dialog-buttons">
<button class="btn btn-secondary mr-auto" @onclick="OnCancel">Cancel</button>
<span class="mr-center">
Price: <span class="price">@(Pizza.GetFormattedTotalPrice())</span>
</span>
<button class="btn btn-success ml-auto" @onclick="OnConfirm">Order ></button>
</div>
<div class="dialog-buttons">
<button class="btn btn-secondary mr-auto" @onclick="OnCancel">Cancel</button>
<span class="mr-center">
Price: <span class="price">@(Pizza.GetFormattedTotalPrice())</span>
</span>
<button class="btn btn-success ml-auto" @onclick="OnConfirm">Order ></button>
</div>


@code {
List<Topping>? toppings;
ElementReference? toppingsSelect { get; set; }

[Parameter, EditorRequired] public Pizza Pizza { get; set; } = new();
[Parameter, EditorRequired] public EventCallback OnCancel { get; set; }
Expand All @@ -73,25 +75,34 @@
toppings = await HttpClient.GetFromJsonAsync<List<Topping>>("toppings") ?? new();
}

void ToppingSelected(ChangeEventArgs e)
async Task ToppingSelected(ChangeEventArgs e)
{
if (toppings is null) return;
if (int.TryParse((string?)e.Value, out var index) && index >= 0)
{
AddTopping(toppings[index]);
await AddTopping(toppings[index]);
}
}

void AddTopping(Topping topping)
async Task AddTopping(Topping topping)
{
if (Pizza.Toppings.Find(pt => pt.Topping == topping) is null)
{
Pizza.Toppings.Add(new PizzaTopping() { Topping = topping });
}
await SetDefaultTopping();
}

void RemoveTopping(Topping topping)
async Task RemoveTopping(Topping topping)
{
Pizza.Toppings.RemoveAll(pt => pt.Topping == topping);
if (!Pizza.Toppings.Any())
await SetDefaultTopping();
}

private async Task SetDefaultTopping()
{
if (toppingsSelect != null)
await JSRuntime.InvokeVoidAsync("JsFunctions.setSelectedIndex", toppingsSelect, 0);
}
}
}
3 changes: 2 additions & 1 deletion src/BlazingPizza.Client/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<body>
<div id="app">
<div class="loading-bar"></div>
</div>
</div>

<div id="blazor-error-ui">
An unhandled error has occurred.
Expand All @@ -29,5 +29,6 @@
<script src="_content/BlazingPizza.ComponentsLibrary/deliveryMap.js"></script>
<script src="_content/BlazingPizza.ComponentsLibrary/leaflet/leaflet.js"></script>
<script>navigator.serviceWorker.register('service-worker.js');</script>
<script src="js/functions.js"></script>
</body>
</html>
6 changes: 6 additions & 0 deletions src/BlazingPizza.Client/wwwroot/js/functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
window.JsFunctions =
{
setSelectedIndex: function (element, index) {
element.selectedIndex = index;
}
};