From e643c0d9523a885a44d51813f3cc9309cf78f96b Mon Sep 17 00:00:00 2001 From: Francesco Colaianni Date: Sat, 2 Dec 2023 19:50:36 +0100 Subject: [PATCH 1/2] fix: set topping dropdown to default when no item has been selected --- src/BlazingPizza.Client/Shared/ConfigurePizzaDialog.razor | 8 ++++++-- src/BlazingPizza.Client/wwwroot/index.html | 3 ++- src/BlazingPizza.Client/wwwroot/js/functions.js | 6 ++++++ 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 src/BlazingPizza.Client/wwwroot/js/functions.js diff --git a/src/BlazingPizza.Client/Shared/ConfigurePizzaDialog.razor b/src/BlazingPizza.Client/Shared/ConfigurePizzaDialog.razor index 47b708c6..ec26a473 100644 --- a/src/BlazingPizza.Client/Shared/ConfigurePizzaDialog.razor +++ b/src/BlazingPizza.Client/Shared/ConfigurePizzaDialog.razor @@ -1,4 +1,5 @@ @inject HttpClient HttpClient +@inject IJSRuntime JSRuntime
@@ -27,7 +28,7 @@ } else { - @for (var i = 0; i < toppings.Count; i++) { @@ -63,6 +64,7 @@ @code { List? toppings; + ElementReference? toppingsSelect { get; set; } [Parameter, EditorRequired] public Pizza Pizza { get; set; } = new(); [Parameter, EditorRequired] public EventCallback OnCancel { get; set; } @@ -90,8 +92,10 @@ } } - void RemoveTopping(Topping topping) + async Task RemoveTopping(Topping topping) { Pizza.Toppings.RemoveAll(pt => pt.Topping == topping); + if(!Pizza.Toppings.Any() && toppingsSelect != null) + await JSRuntime.InvokeVoidAsync("JsFunctions.setSelectedIndex", toppingsSelect, 0); } } 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 From 8d155659dd5666faec058ddaf70db52f1c9e0911 Mon Sep 17 00:00:00 2001 From: Francesco Colaianni Date: Sat, 2 Dec 2023 20:14:46 +0100 Subject: [PATCH 2/2] chore: set default even when adding topping for better user experience and refactor in standalone method --- .../Shared/ConfigurePizzaDialog.razor | 125 +++++++++--------- 1 file changed, 66 insertions(+), 59 deletions(-) diff --git a/src/BlazingPizza.Client/Shared/ConfigurePizzaDialog.razor b/src/BlazingPizza.Client/Shared/ConfigurePizzaDialog.razor index ec26a473..7d800591 100644 --- a/src/BlazingPizza.Client/Shared/ConfigurePizzaDialog.razor +++ b/src/BlazingPizza.Client/Shared/ConfigurePizzaDialog.razor @@ -2,64 +2,64 @@ @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 + { - } -
- -
- @foreach (var topping in Pizza.Toppings) + + @for (var i = 0; i < toppings.Count; i++) { - if (topping?.Topping is not null) - { -
- @topping.Topping.Name - @topping.Topping.GetFormattedPrice() - -
- } + } -
- + + } +
-
- - - Price: @(Pizza.GetFormattedTotalPrice()) - - -
+
+ @foreach (var topping in Pizza.Toppings) + { + if (topping?.Topping is not null) + { +
+ @topping.Topping.Name + @topping.Topping.GetFormattedPrice() + +
+ } + } +
+ + +
+ + + Price: @(Pizza.GetFormattedTotalPrice()) + + +
@code { @@ -75,27 +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(); } async Task RemoveTopping(Topping topping) { Pizza.Toppings.RemoveAll(pt => pt.Topping == topping); - if(!Pizza.Toppings.Any() && toppingsSelect != null) + 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