-
Notifications
You must be signed in to change notification settings - Fork 0
/
FiveDayForecast.razor
73 lines (60 loc) · 1.8 KB
/
FiveDayForecast.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@page "/"
@using ExampleBlazorApp.Client.HttpClients
@using ExampleBlazorApp.Shared
@using RandomSkunk.Results
<PageTitle>Five Day forecast</PageTitle>
<h1>Five Day Forecast</h1>
@if (_forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<div class="form-group">
<label>City:
<input type="text" @bind-value="@_city"/>
<button @onclick="FetchForecast">Get Five-day Forecast</button>
</label>
</div>
<hr/>
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>High</th>
<th>Low</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in _forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.HighF.ToString("0")F (@forecast.HighC.ToString("0")C)</td>
<td>@forecast.LowF.ToString("0")F (@forecast.LowC.ToString("0")C)</td>
</tr>
}
</tbody>
</table>
}
@code {
private IReadOnlyList<WeatherForecast> _forecasts = null!;
private string _city = "Chicago";
[Inject]
public WeatherForecastClient HttpClient { get; set; } = null!;
[Inject]
public IJSRuntime JS { get; set; } = null!;
protected override async Task OnInitializedAsync()
{
await FetchForecast();
}
private async Task FetchForecast()
{
// Make a call to the server using our WeatherForcastClient.
await HttpClient.GetFiveDayForecast(_city)
// If it was successful, set the _forcasts field.
.OnSuccess(forcasts => _forecasts = forcasts)
// If it was not successful, log to the browser console and alert the user.
.OnNonSuccessLogAndAlert(JS);
}
}