Exercise - Improve app interactivity with lifecycle events
The pizza company decided that 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 dialog for configuring the pizza so that it handles 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
ImageUrlproperty, add the following new property:public int? FixedSize { get; set; }Open the Pizza.cs file, and replace the
GetBasePricemethod 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
FixedSizewhen 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
specialsarray declaration in theInitializeAsyncmethod.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
SeedDataclass prepopulates the pizza database with special pizzas. For the newPizzaSpecialto 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
@codedirective, 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
supportSizingfield defaults totrue, but if the pizza has a fixed size, the field is set tofalse. TheOnInitializedlifecycle method override sets the pizza size to the fixed size and disables sizing support.Note
If your code relied on JavaScript interop, using the
OnInitializedmethod wouldn't work. Instead, you'd need to use theOnAfterRenderAsyncmethod to ensure the JavaScript interop was available.Near the top of the file in the
<form class="dialog-body">, replace the existinglabelandinputlines 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 doesn't render.
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.