Skip to content

Commit af0b688

Browse files
authored
Merge pull request #107 from HTBox/103_hazard-info-ui
Admin UI to create and edit Hazard Info
2 parents dbb4cec + 66c9ea5 commit af0b688

12 files changed

Lines changed: 145 additions & 36 deletions

File tree

.github/workflows/admin.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
paths:
66
- 'admin/**'
7+
- 'TwoWeeksReady.Common/**'
78
- '.github/workflows/admin.yaml'
89
branches: [ master ]
910
pull_request:

.github/workflows/api.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
paths:
66
- 'api/**'
7+
- 'TwoWeeksReady.Common/**'
78
- '.github/workflows/api.yaml'
89
branches: [ master ]
910
pull_request:

admin/TwoWeeksReady.Admin/Data/FunctionsRepository.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,7 @@ public Task<HazardHunt> GetHazardHuntById(string id)
4747

4848
public async Task<HazardInfo> GetHazardInfoById(string id)
4949
{
50-
try
51-
{
52-
return await _httpClient.GetFromJsonAsync<HazardInfo>($"hazardinfo-by-id/{id}");
53-
} catch (Exception ex)
54-
{
55-
return new HazardInfo();
56-
}
57-
50+
return await _httpClient.GetFromJsonAsync<HazardInfo>($"hazardinfo-by-id/{id}");
5851
}
5952

6053
public Task<BaseKitItem> SaveBaseKitItem(BaseKitItem kit)
@@ -67,9 +60,30 @@ public Task<HazardHunt> SaveHazardHunt(HazardHunt hazardHunt)
6760
throw new NotImplementedException();
6861
}
6962

70-
public Task<HazardInfo> SaveHazardInfo(HazardInfo hazardInfo)
63+
public async Task<HazardInfo> SaveHazardInfo(HazardInfo hazardInfo)
7164
{
72-
throw new NotImplementedException();
65+
var response = await _httpClient.PutAsJsonAsync("hazardinfo-update", hazardInfo);
66+
if (response.IsSuccessStatusCode)
67+
{
68+
return await response.Content.ReadFromJsonAsync<HazardInfo>();
69+
}
70+
else
71+
{
72+
throw new Exception("Error saving hazard info");
73+
}
74+
}
75+
76+
public async Task<HazardInfo> CreateHazardInfo(HazardInfo hazardInfo)
77+
{
78+
var response = await _httpClient.PostAsJsonAsync("hazardinfo-create", hazardInfo);
79+
if (response.IsSuccessStatusCode)
80+
{
81+
return await response.Content.ReadFromJsonAsync<HazardInfo>();
82+
}
83+
else
84+
{
85+
throw new Exception("Error saving hazard info");
86+
}
7387
}
7488
}
7589
}

admin/TwoWeeksReady.Admin/Data/IRepository.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public interface IRepository
2323

2424
Task<HazardInfo> GetHazardInfoById(string id);
2525

26-
Task<HazardInfo> SaveHazardInfo(HazardInfo hazardInfo);
26+
Task<HazardInfo> SaveHazardInfo(HazardInfo hazardInfo);
27+
28+
Task<HazardInfo> CreateHazardInfo(HazardInfo hazardInfo);
2729
}
2830

2931
}

admin/TwoWeeksReady.Admin/Data/StubRepository.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ public Task<HazardInfo> SaveHazardInfo(HazardInfo hazardInfo)
103103
{
104104
return Task.FromResult(hazardInfo);
105105
}
106+
107+
public Task<HazardInfo> CreateHazardInfo(HazardInfo hazardInfo)
108+
{
109+
return Task.FromResult(hazardInfo);
110+
}
111+
112+
106113
}
107114

108115
}

admin/TwoWeeksReady.Admin/Pages/HazardInfos/Details.razor

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,81 @@
11
@page "/HazardInfos/{id}"
2+
@page "/HazardInfos/new"
23
@attribute [Authorize(Roles = "admin")]
34

5+
@using TinyMCE.Blazor
46
@inject IRepository repository
57
@inject IJSRuntime JS
8+
@inject Microsoft.Extensions.Configuration.IConfiguration configuration
9+
10+
@{
11+
var tinyMCEApiKey = configuration["TinyMCEApiKey"];
12+
}
613

714
@if (Hazard == null)
815
{
916
<p><em>Loading...</em></p>
1017
}
1118
else
1219
{
20+
1321
<h3>Details</h3>
14-
<label for="name">Hazard Name:</label>
15-
<input type="text" @bind="@Hazard.Name" />
16-
<br />
17-
<label for="description">Short Description:</label>
18-
<input type="text" @bind="@Hazard.Description" />
19-
<span class="btn btn-secondary float-right" style="cursor: pointer" @onclick="@Save">Save</span>
22+
<form>
23+
<div class="form-group">
24+
<label for="name">Hazard Name</label>
25+
<input type="text" class="form-control" name="name" @bind="@Hazard.Name">
26+
</div>
27+
<div class="form-group">
28+
<label for="description">Short Description:</label>
29+
<textarea class="form-control" name="description" @bind="@Hazard.Description" rows="5"></textarea>
30+
</div>
31+
<div class="form-group">
32+
<label for="iconUrl">Icon Url</label>
33+
<input type="text" class="form-control" name="iconUrl" @bind="@Hazard.IconUrl">
34+
</div>
35+
<div class="form-group">
36+
<label for="mediaUrl">Media Url</label>
37+
<input type="text" class="form-control" name="mediaUrl" @bind="@Hazard.MediaUrl">
38+
</div>
39+
40+
<div class="form-group">
41+
<label for="beforeSafetyDetails">Before Safety Details</label>
42+
<Editor Id="beforeSafetyDetails" Conf="@EditorConfig" ApiKey="@tinyMCEApiKey" @bind-Value="@Hazard.BeforeSafetyDetails" />
43+
</div>
44+
45+
<div class="form-group">
46+
<label for="duringSafetyDetails">DuringSafety Details</label>
47+
<Editor Id="duringSafetyDetails" Conf="@EditorConfig" ApiKey="@tinyMCEApiKey" @bind-Value="@Hazard.DuringSafetyDetails" />
48+
</div>
2049

50+
<div class="form-group">
51+
<label for="afterSafetyDetails">Before Safety Details</label>
52+
<Editor Id="afterSafetyDetails" Conf="@EditorConfig" ApiKey="@tinyMCEApiKey" @bind-Value="@Hazard.AfterSafetyDetails" />
53+
</div>
54+
55+
56+
<div class="form-group">
57+
<label for="externalLinks">External Links (One link per line)</label>
58+
<textarea rows="6" class="form-control" name="externalLinks" @bind="@ExternalLinks"></textarea>
59+
</div>
60+
61+
<button type="button" class="btn btn-primary" @onclick="@Save">Submit</button>
62+
</form>
2163
}
2264

2365
@code {
2466

67+
public Dictionary<string, object> EditorConfig = new Dictionary<string, object>
68+
{
69+
{ "plugins", "image" },
70+
{ "toolbar", "image" },
71+
{"image_list", new []
72+
{
73+
// TODO: Figure out a strategy for loading a list of images from assets availabe within the app
74+
new { title = "Image 1", value = "http://localhost:8080/images/hazards/earthquake.png"}
75+
} }
76+
77+
};
78+
2579
[Parameter]
2680
public string Id { get; set; }
2781

@@ -30,17 +84,47 @@ else
3084

3185
private HazardInfo Hazard { get; set; }
3286

87+
private string ExternalLinks
88+
{
89+
get
90+
{
91+
return string.Join(Environment.NewLine, Hazard?.ExternalLinks ?? new string[0]);
92+
}
93+
set
94+
{
95+
var links = value.Split(new string[] { "\n", Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s));
96+
Hazard.ExternalLinks = links.ToArray();
97+
}
98+
}
99+
33100

34101
protected override async Task OnInitializedAsync()
35102
{
36-
Hazard = await repository.GetHazardInfoById(Id);
103+
if (string.IsNullOrEmpty(Id))
104+
{
105+
Hazard = new HazardInfo();
106+
107+
}
108+
else
109+
{
110+
Hazard = await repository.GetHazardInfoById(Id);
111+
}
112+
37113
}
38114

39115

40116
public async Task Save()
41117
{
42-
await repository.SaveHazardInfo(Hazard);
43-
await OnSave.InvokeAsync(Hazard);
118+
if (string.IsNullOrEmpty(Hazard.Id))
119+
{
120+
Hazard = await repository.CreateHazardInfo(Hazard);
121+
}
122+
else
123+
{
124+
Hazard = await repository.SaveHazardInfo(Hazard);
125+
}
126+
127+
//await OnSave.InvokeAsync(Hazard);
44128
45129
await JS.InvokeVoidAsync("alert", new object[] { "Hazard Info Saved" });
46130

admin/TwoWeeksReady.Admin/Pages/HazardInfos/List.razor

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
@page "/HazardInfos/"
22
@attribute [Authorize(Roles = "admin")]
33
@inject IRepository Repository
4-
<h1>Administer Hazard Info for Two Weeks Ready</h1>
5-
6-
<h3>Current Hazards Defined:</h3>
4+
<h1>Administer Hazard Info</h1>
75

86
@if (_HazardInfos != null && _HazardInfos.Any())
97
{
108

11-
<table>
9+
<table class="table table-bordered">
1210
<thead>
1311
<tr>
14-
<th>Name</th>
12+
<th>Name</th>
1513
</tr>
1614
</thead>
1715
<tbody>
@@ -21,19 +19,24 @@
2119
<tr>
2220
<td>
2321
<a href="/HazardInfos/@hazard.Id">
24-
@hazard.Name
22+
@hazard.Name
2523
</a>
2624
</td>
2725
</tr>
2826
}
2927
</tbody>
3028

3129
</table>
30+
<a class="btn btn-primary" href="/HazardInfos/new">Add New Hazard</a>
3231

3332
}
33+
else if (_HazardInfos == null)
34+
{
35+
<p>Loading....</p>
36+
}
3437
else
3538
{
36-
<p>No Hazards defined.</p>
39+
<p>No hazard infos defined</p>
3740
}
3841

3942
@code {

admin/TwoWeeksReady.Admin/Pages/_Host.cshtml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</head>
2222

2323
<body>
24-
@{
24+
@{
2525
var token = new TokenProvider
2626
{
2727
AccessToken = await HttpContext.GetTokenAsync("access_token")
@@ -43,5 +43,6 @@
4343
</div>
4444

4545
<script src="_framework/blazor.server.js"></script>
46+
<script src="_content/TinyMCE.Blazor/tinymce-blazor.js"></script>
4647
</body>
4748
</html>

admin/TwoWeeksReady.Admin/Startup.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public void ConfigureServices(IServiceCollection services)
6060
options.Scope.Clear();
6161
options.Scope.Add("openid");
6262
options.Scope.Add("profile");
63-
options.Scope.Add("roles");
6463

6564
options.CallbackPath = new PathString("/callback");
6665
options.ClaimsIssuer = "Auth0";

admin/TwoWeeksReady.Admin/TwoWeeksReady.Admin.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
@@ -11,6 +11,7 @@
1111

1212
<ItemGroup>
1313
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.6" />
14+
<PackageReference Include="TinyMCE.Blazor" Version="0.0.8" />
1415
</ItemGroup>
1516

1617
</Project>

0 commit comments

Comments
 (0)