-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeatherProfiles.razor
202 lines (177 loc) · 7.22 KB
/
WeatherProfiles.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
@page "/WeatherProfiles/View"
@using ExampleBlazorApp.Client.HttpClients
@using ExampleBlazorApp.Shared
@using RandomSkunk.Results
<PageTitle>Weather Profiles</PageTitle>
<h1>Weather Profiles</h1>
@if (_profiles is null)
{
<p><em>Loading...</em></p>
}
else
{
<div class="form-group">
<label>
Profiles:
<select @onchange="@OnCityChanged">
@foreach (var profile in _profiles)
{
<option value="@profile.City">@profile.City</option>
}
</select>
<button @onclick="@EditSelectedCity">Edit selected city</button>
<button @onclick="@AddNewCity">Add new city</button>
</label>
</div>
}
@if (_selectedProfile != null)
{
<hr />
<div class="form-group">
<table>
<caption>Weather profile for @_selectedProfile.City</caption>
<thead>
<tr>
<th>Month</th>
<th>Average High (F)</th>
<th>Average Low (F)</th>
<th>Standard Deviation</th>
</tr>
</thead>
<tbody>
@foreach (var monthlyTemperature in _selectedProfile.MonthlyTemperatures)
{
<tr>
<td>@monthlyTemperature.MonthName</td>
<td>@monthlyTemperature.AverageHigh</td>
<td>@monthlyTemperature.AverageLow</td>
<td>@monthlyTemperature.StandardDeviation</td>
</tr>
}
</tbody>
</table>
</div>
}
@if (_dialogProfile != null)
{
<div class="modal fade show d-block" id="exampleModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="titleLabel">@_dialogTitle</h5>
<button type="button" class="close" @onclick="@CloseDialog" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<EditForm Model="@_dialogProfile" OnValidSubmit="@SaveProfile">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="form-group">
<label for="lastName">City: </label>
<InputText id="city" class="form-control" @bind-Value="@_dialogProfile.City" placeholder="Enter city"></InputText>
<ValidationMessage For="@(() => _dialogProfile.City)" />
</div>
<table>
<caption>Weather profile for @_dialogProfile.City</caption>
<thead>
<tr>
<th>Month</th>
<th>Average High (F)</th>
<th>Average Low (F)</th>
<th>Standard Deviation</th>
</tr>
</thead>
<tbody>
@foreach (var monthlyTemperature in _dialogProfile.MonthlyTemperatures)
{
<tr>
<td>@monthlyTemperature.MonthName</td>
<td><InputNumber class="form-control" @bind-Value="@monthlyTemperature.AverageHigh" placeholder="Enter average high temperature"></InputNumber></td>
<td><InputNumber class="form-control" @bind-Value="@monthlyTemperature.AverageLow" placeholder="Enter average low temperature"></InputNumber></td>
<td><InputNumber class="form-control" @bind-Value="@monthlyTemperature.StandardDeviation" placeholder="Enter standard deviation"></InputNumber></td>
</tr>
}
</tbody>
</table>
<button type="submit" class="btn btn-primary">Save weather profile</button>
<a class="btn btn-outline-primary" @onclick="@CloseDialog">Close</a>
</EditForm>
</div>
</div>
</div>
</div>
}
@code {
private IReadOnlyList<WeatherProfile> _profiles = null!;
private WeatherProfile? _selectedProfile;
private bool _isNew;
private WeatherProfile? _dialogProfile;
private string? _dialogTitle;
[Inject]
public WeatherProfileClient HttpClient { get; set; } = null!;
[Inject]
public IJSRuntime JS { get; set; } = null!;
protected override Task OnInitializedAsync()
{
return FetchWeatherProfiles();
}
private void OnCityChanged(ChangeEventArgs args)
{
_selectedProfile = _profiles.FirstOrDefault(p => p.City == (string?)args.Value);
}
private void EditSelectedCity()
{
_dialogTitle = $"Edit weather profile for {_selectedProfile!.City}";
_isNew = false;
_dialogProfile = _selectedProfile!.Clone();
}
private void AddNewCity()
{
_dialogTitle = "Add new weather profile";
_isNew = true;
_dialogProfile = new WeatherProfile { MonthlyTemperatures = MonthlyTemperature.CreateListForCalendarYear() };
}
private void CloseDialog()
{
_dialogProfile = null;
_dialogTitle = null;
}
private async Task FetchWeatherProfiles()
{
// Make a call to the server to get a list of weather profiles.
var result = await HttpClient.GetWeatherProfiles();
// Handle the result:
await result
// If the call to the server was successful, set the _profiles and _selectedProfile fields.
.OnSuccess(profiles =>
{
_profiles = profiles;
_selectedProfile = _profiles.FirstOrDefault();
})
// If the call to the server was not successful, log to the browser console and alert the user.
.OnNonSuccessLogAndAlert(JS);
}
private async Task SaveProfile()
{
foreach (var monthlyTemperature in _dialogProfile!.MonthlyTemperatures)
monthlyTemperature.City = _dialogProfile.City;
Result result;
if (_isNew)
{
// Make a call to the server to add the new weather profile.
result = await HttpClient.AddWeatherProfile(_dialogProfile);
}
else
{
// Make a call to the server to edit the existing weather profile.
result = await HttpClient.EditWeatherProfile(_dialogProfile);
}
// Handle the result:
await result
// If the call to the server was successful, close the dialog.
.OnSuccess(() => CloseDialog())
// If the call to the server was not successful, log to the browser console and alert the user.
.OnNonSuccessLogAndAlert(JS);
}
}