Exercise - Improve app interactivity with lifecycle events
The pizza company has decided they'd like to sell a special family-sized pizza that's only available in one 24-inch size. The current pizza app has a size slider that doesn't support a single-sized pizza. You're asked to add the new family-size pizza and disable the size option for that pizza.
In this exercise, you change the pizza database to add the family-size pizza and change the pizza model to support the new pizza. To handle Blazor component lifecycle events, you make changes to the configure pizza dialog to handle the fixed-size case.
Create the new family-size pizza
First, you add the new FixedSize
capability to the pizza model, and create the new family-size pizza in the pizza database.
In Visual Studio Code Explorer, expand Models and select PizzaSpecial.cs.
In the PizzaSpecial.cs file, after the
ImageUrl
property, add the following new property:public int? FixedSize { get; set; }
Open the Pizza.cs file, and replace the
GetBasePrice
method with the following code:public decimal GetBasePrice() => Special is { FixedSize: not null } ? Special.BasePrice : (decimal)Size / DefaultSize * Special?.BasePrice ?? 1;
The code now accounts for a special that has a
FixedSize
when it returns theBasePrice
.Expand Data, and select SeedData.cs.
In the SeedData.cs file, add the following code for the new family size pizza to the end of the
specials
array declaration in theInitializeAsync
method.new() { Id = 9, Name = "Margherita Family Size", Description = "24\" of pure tomatoes and basil", BasePrice = 14.99m, ImageUrl = "img/pizzas/margherita.jpg", FixedSize = 24 }
The
SeedData
class prepopulates the pizza database with special pizzas. For the newPizzaSpecial
to be created, you need to delete the existing database. In Explorer, select and delete the pizza.db, pizza.db-shm, and pizza.db-wal files.In Visual Studio Code, press F5or select Run > Start Debugging.
In the app, select the new Margherita Family Size pizza.
In the order form, notice that you can still change the pizza size.
Press Shift+F5 or select Run > Stop Debugging to stop the app.
Remove the size slider
The ConfigurePizzaDialog
component uses an HTML range
element to allow the customer to select the pizza size. One way to disable user input is to conditionally omit the rendering of the size user control altogether.
In Visual Studio Code Explorer, expand Shared and then select ConfigurePizzaDialog.razor.
In the
@code
directive, after the existing properties, add the following members:bool supportSizing = true; protected override void OnInitialized() { if (Pizza is { Special.FixedSize: not null }) { Pizza.Size = Pizza.Special.FixedSize.Value; supportSizing = false; } }
The
supportSizing
field defaults totrue
, but if the pizza has a fixed size, the field is set tofalse
. TheOnInitialized
lifecycle method override sets the pizza size to the fixed size and disables sizing support.Note
If your code relied on JavaScript interop, using the
OnInitialized
method wouldn't work. Instead, you'd need to use theOnAfterRenderAsync
method to ensure the JavaScript interop was available.Near the top of the file in the
<form class="dialog-body">
, replace the existinglabel
andinput
lines with the following code:@if (supportSizing) { <label>Size:</label> <input type="range" min="@Pizza.MinimumSize" max="@Pizza.MaximumSize" step="1" @bind="Pizza.Size" @bind:event="oninput" /> }
Press F5 or select Run > Start Debugging.
Add the family-size pizza, and verify that the size slider is disabled because it's omitted from rendering.
Order a different pizza, and verify that you can still use the size slider for that pizza.
Press Shift+F5 or select Run > Stop Debugging to stop the app.