|
1 | 1 | @page "/HazardInfos/{id}" |
| 2 | +@page "/HazardInfos/new" |
2 | 3 | @attribute [Authorize(Roles = "admin")] |
3 | 4 |
|
| 5 | +@using TinyMCE.Blazor |
4 | 6 | @inject IRepository repository |
5 | 7 | @inject IJSRuntime JS |
| 8 | +@inject Microsoft.Extensions.Configuration.IConfiguration configuration |
| 9 | + |
| 10 | +@{ |
| 11 | + var tinyMCEApiKey = configuration["TinyMCEApiKey"]; |
| 12 | +} |
6 | 13 |
|
7 | 14 | @if (Hazard == null) |
8 | 15 | { |
9 | 16 | <p><em>Loading...</em></p> |
10 | 17 | } |
11 | 18 | else |
12 | 19 | { |
| 20 | + |
13 | 21 | <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> |
20 | 49 |
|
| 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> |
21 | 63 | } |
22 | 64 |
|
23 | 65 | @code { |
24 | 66 |
|
| 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 | + |
25 | 79 | [Parameter] |
26 | 80 | public string Id { get; set; } |
27 | 81 |
|
|
30 | 84 |
|
31 | 85 | private HazardInfo Hazard { get; set; } |
32 | 86 |
|
| 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 | + |
33 | 100 |
|
34 | 101 | protected override async Task OnInitializedAsync() |
35 | 102 | { |
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 | + |
37 | 113 | } |
38 | 114 |
|
39 | 115 |
|
40 | 116 | public async Task Save() |
41 | 117 | { |
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); |
44 | 128 |
|
45 | 129 | await JS.InvokeVoidAsync("alert", new object[] { "Hazard Info Saved" }); |
46 | 130 |
|
|
0 commit comments