diff --git a/src/BlazingPizza.Client/Shared/ConfigurePizzaDialog.razor b/src/BlazingPizza.Client/Shared/ConfigurePizzaDialog.razor index 47b708c6..7d800591 100644 --- a/src/BlazingPizza.Client/Shared/ConfigurePizzaDialog.razor +++ b/src/BlazingPizza.Client/Shared/ConfigurePizzaDialog.razor @@ -1,68 +1,70 @@ @inject HttpClient HttpClient +@inject IJSRuntime JSRuntime -
-

@Pizza.Special?.Name

- @Pizza.Special?.Description -
-
-
- - - - @(Pizza.Size)" (£@(Pizza.GetFormattedTotalPrice())) - -
-
- - @if (toppings is null) - { - - } - else if (Pizza.Toppings.Count >= 6) - { -
(maximum reached)
- } - else +
+

@Pizza.Special?.Name

+ @Pizza.Special?.Description +
+ +
+ + + + @(Pizza.Size)" (£@(Pizza.GetFormattedTotalPrice())) + +
+
+ + @if (toppings is null) + { + + } + else if (Pizza.Toppings.Count >= 6) + { +
(maximum reached)
+ } + else + { + - - @for (var i = 0; i < toppings.Count; i++) - { - - } - + } -
+ + } +
-
- @foreach (var topping in Pizza.Toppings) - { - if (topping?.Topping is not null) - { -
- @topping.Topping.Name - @topping.Topping.GetFormattedPrice() - -
- } - } -
-
+
+ @foreach (var topping in Pizza.Toppings) + { + if (topping?.Topping is not null) + { +
+ @topping.Topping.Name + @topping.Topping.GetFormattedPrice() + +
+ } + } +
+ -
- - - Price: @(Pizza.GetFormattedTotalPrice()) - - -
+
+ + + Price: @(Pizza.GetFormattedTotalPrice()) + + +
@code { List? toppings; + ElementReference? toppingsSelect { get; set; } [Parameter, EditorRequired] public Pizza Pizza { get; set; } = new(); [Parameter, EditorRequired] public EventCallback OnCancel { get; set; } @@ -73,25 +75,34 @@ toppings = await HttpClient.GetFromJsonAsync>("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); } -} +} \ No newline at end of file diff --git a/src/BlazingPizza.Client/wwwroot/index.html b/src/BlazingPizza.Client/wwwroot/index.html index 0aeae293..a7425c58 100644 --- a/src/BlazingPizza.Client/wwwroot/index.html +++ b/src/BlazingPizza.Client/wwwroot/index.html @@ -14,7 +14,7 @@
-
+
An unhandled error has occurred. @@ -29,5 +29,6 @@ + diff --git a/src/BlazingPizza.Client/wwwroot/js/functions.js b/src/BlazingPizza.Client/wwwroot/js/functions.js new file mode 100644 index 00000000..486fcc7b --- /dev/null +++ b/src/BlazingPizza.Client/wwwroot/js/functions.js @@ -0,0 +1,6 @@ +window.JsFunctions = +{ + setSelectedIndex: function (element, index) { + element.selectedIndex = index; + } +}; \ No newline at end of file