Skip to content

Commit 14c4533

Browse files
committed
Revert "Remove unwanted sample changes"
This reverts commit 4bc47f9.
1 parent 8deb0a7 commit 14c4533

File tree

6 files changed

+213
-41
lines changed

6 files changed

+213
-41
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace BlazorUnitedApp.Client.Data;
5+
6+
public class ClientWeatherForecast
7+
{
8+
public DateOnly Date { get; set; }
9+
10+
public int TemperatureC { get; set; }
11+
12+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
13+
14+
public string? Summary { get; set; }
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace BlazorUnitedApp.Client.Data;
5+
6+
public class ClientWeatherForecastService
7+
{
8+
private static readonly string[] Summaries = new[]
9+
{
10+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
11+
};
12+
13+
public async Task<ClientWeatherForecast[]> GetForecastAsync(DateOnly startDate)
14+
{
15+
await Task.Yield();
16+
return await Task.FromResult(Enumerable.Range(1, 5).Select(index => new ClientWeatherForecast
17+
{
18+
Date = startDate.AddDays(index),
19+
TemperatureC = Random.Shared.Next(-20, 55),
20+
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
21+
}).ToArray());
22+
}
23+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
@page "/clientfetchdata/"
2+
@using BlazorUnitedApp.Client.Data
3+
@using Microsoft.AspNetCore.Components.Web
4+
@inject ClientWeatherForecastService ForecastService
5+
@inject NavigationManager NavigationManager
6+
@rendermode Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveWebAssembly
7+
8+
<PageTitle>Weather forecast</PageTitle>
9+
10+
<h1>Weather forecast</h1>
11+
12+
<p>This component demonstrates fetching data from a service.</p>
13+
14+
@if (Forecasts == null)
15+
{
16+
<p><em>Loading...</em></p>
17+
}
18+
else
19+
{
20+
<table class="table">
21+
<thead>
22+
<tr>
23+
<th>Date</th>
24+
<th>Temp. (C)</th>
25+
<th>Temp. (F)</th>
26+
<th>Summary</th>
27+
</tr>
28+
</thead>
29+
<tbody>
30+
@foreach (var forecast in Forecasts)
31+
{
32+
<tr>
33+
<td>@forecast.Date.ToShortDateString()</td>
34+
<td>@forecast.TemperatureC</td>
35+
<td>@forecast.TemperatureF</td>
36+
<td>@forecast.Summary</td>
37+
</tr>
38+
}
39+
</tbody>
40+
</table>
41+
}
42+
43+
<p>Current Url = @Page</p>
44+
45+
<ul>
46+
<li>
47+
<a href="clientfetchdata/?page=0">Page 0</a>
48+
</li>
49+
<li>
50+
<a href="clientfetchdata/?page=1">Page 1</a>
51+
</li>
52+
<li>
53+
<a href="clientfetchdata/?page=2">Page 2</a>
54+
</li>
55+
</ul>
56+
57+
@code {
58+
[PersistentState(AllowUpdates = true)]
59+
public ClientWeatherForecast[]? Forecasts { get; set; }
60+
61+
public string Page { get; set; } = "";
62+
63+
protected override async Task OnInitializedAsync()
64+
{
65+
// Extract the page from the query string
66+
var uri = NavigationManager.ToAbsoluteUri(NavigationManager.Uri);
67+
var pageParam = uri.Query.Split('&')
68+
.Select(q => q.Split('='))
69+
.FirstOrDefault(q => q.Length == 2 && q[0] == "page")?[1];
70+
int pageIndex = 1 + (int.TryParse(pageParam, out int parsedPage) ? parsedPage : 0);
71+
pageIndex *= 7;
72+
73+
Forecasts ??= await ForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now).AddDays(pageIndex));
74+
}
75+
76+
protected override void OnParametersSet()
77+
{
78+
Page = NavigationManager.Uri;
79+
}
80+
}

src/Components/Samples/BlazorUnitedApp/Data/WeatherForecastService.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ public class WeatherForecastService
1010
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
1111
};
1212

13-
public Task<WeatherForecast[]> GetForecastAsync(DateOnly startDate)
13+
public async Task<WeatherForecast[]> GetForecastAsync(DateOnly startDate)
1414
{
15-
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
15+
await Task.Delay(1000);
16+
return [.. Enumerable.Range(1, 5).Select(index => new WeatherForecast
1617
{
1718
Date = startDate.AddDays(index),
1819
TemperatureC = Random.Shared.Next(-20, 55),
1920
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
20-
}).ToArray());
21+
})];
2122
}
2223
}

src/Components/Samples/BlazorUnitedApp/Pages/FetchData.razor

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,14 @@
22
@using BlazorUnitedApp.Data
33
@inject WeatherForecastService ForecastService
44

5-
<PageTitle>Weather forecast</PageTitle>
6-
7-
<h1>Weather forecast</h1>
8-
9-
<p>This component demonstrates fetching data from a service.</p>
10-
11-
@if (forecasts == null)
12-
{
13-
<p><em>Loading...</em></p>
14-
}
15-
else
16-
{
17-
<table class="table">
18-
<thead>
19-
<tr>
20-
<th>Date</th>
21-
<th>Temp. (C)</th>
22-
<th>Temp. (F)</th>
23-
<th>Summary</th>
24-
</tr>
25-
</thead>
26-
<tbody>
27-
@foreach (var forecast in forecasts)
28-
{
29-
<tr>
30-
<td>@forecast.Date.ToShortDateString()</td>
31-
<td>@forecast.TemperatureC</td>
32-
<td>@forecast.TemperatureF</td>
33-
<td>@forecast.Summary</td>
34-
</tr>
35-
}
36-
</tbody>
37-
</table>
38-
}
39-
5+
<FetchDataChildren @rendermode="InteractiveServer" YayOrNay="@YayOrNay"></FetchDataChildren>
406
@code {
41-
private WeatherForecast[]? forecasts;
7+
[CascadingParameter] public HttpContext Context { get; set; } = null!;
428

43-
protected override async Task OnInitializedAsync()
9+
protected override void OnInitialized()
4410
{
45-
forecasts = await ForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now));
11+
YayOrNay = Context.Request.Query["YayOrNay"] == "true" ? true : false;
4612
}
13+
14+
public bool YayOrNay { get; set; } = false;
4715
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
@using BlazorUnitedApp.Data
2+
@inject WeatherForecastService ForecastService
3+
@inject NavigationManager NavigationManager
4+
5+
<h1>Weather forecast</h1>
6+
7+
<p>This component demonstrates fetching data from a service.</p>
8+
9+
@if (Forecasts == null)
10+
{
11+
<p><em>Loading...</em></p>
12+
}
13+
else
14+
{
15+
<table class="table">
16+
<thead>
17+
<tr>
18+
<th>Date</th>
19+
<th>Temp. (C)</th>
20+
<th>Temp. (F)</th>
21+
<th>Summary</th>
22+
</tr>
23+
</thead>
24+
<tbody>
25+
@foreach (var forecast in Forecasts)
26+
{
27+
<tr>
28+
<td>@forecast.Date.ToShortDateString()</td>
29+
<td>@forecast.TemperatureC</td>
30+
<td>@forecast.TemperatureF</td>
31+
<td>@forecast.Summary</td>
32+
</tr>
33+
}
34+
</tbody>
35+
</table>
36+
}
37+
38+
<button @onclick="ClearData">Clear data</button>
39+
40+
<p>Current Url = @Page</p>
41+
42+
<p>Yay or Nay: @YayOrNay</p>
43+
44+
<ul>
45+
<li>
46+
<a href="fetchdata/?page=0">Page 0</a>
47+
</li>
48+
<li>
49+
<a href="fetchdata/?page=1&YayOrNay=true">Page 1</a>
50+
</li>
51+
<li>
52+
<a href="fetchdata/?page=2">Page 2</a>
53+
</li>
54+
</ul>
55+
56+
@code {
57+
[PersistentState(AllowUpdates = true)]
58+
public WeatherForecast[]? Forecasts { get; set; }
59+
60+
[Parameter] public bool YayOrNay { get; set; } = false;
61+
62+
public string Page { get; set; } = "";
63+
64+
protected override async Task OnInitializedAsync()
65+
{
66+
// Extract the page from the query string
67+
var uri = NavigationManager.ToAbsoluteUri(NavigationManager.Uri);
68+
var query = System.Web.HttpUtility.ParseQueryString(uri.Query);
69+
var pageParam = query["page"];
70+
int pageIndex = 1 + (int.TryParse(pageParam, out int parsedPage) ? parsedPage : 0);
71+
pageIndex *= 7;
72+
73+
Forecasts ??= await ForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now).AddDays(pageIndex));
74+
}
75+
76+
protected override void OnParametersSet()
77+
{
78+
Page = NavigationManager.Uri;
79+
}
80+
81+
private void ClearData()
82+
{
83+
Forecasts = [];
84+
}
85+
}

0 commit comments

Comments
 (0)