11@page " /HazardHunts/{id}"
2+ @page " /HazardHunts/new"
23@attribute [Authorize(Roles = "admin")]
34
5+ @using TinyMCE .Blazor
6+ @using System .IO ;
7+ @using System .Linq ;
8+
49@inject IRepository repository
510@inject IJSRuntime JS
11+ @inject Microsoft .Extensions .Configuration .IConfiguration configuration
12+ @inject ClientImageService clientImages
13+
14+ @{
15+ var tinyMCEApiKey = configuration [" TinyMCEApiKey" ];
16+ }
17+
18+ @if (Hazard == null )
19+ {
20+ <p ><em >Loading .. .</em ></p >
21+ }
22+ else
23+ {
24+ <h3 >Details </h3 >
25+ <EditForm Model =" @Hazard" >
26+ <div class =" form-group" >
27+ <label for =" name" >Hazard Name </label >
28+ <input type =" text" class =" form-control" name =" name" @bind =" @Hazard.Name" >
29+ </div >
30+ <div class =" form-group" >
31+ <label for =" description" >Short Description : </label >
32+ <textarea class =" form-control" name =" description" @bind =" @Hazard.Description" rows =" 5" ></textarea >
33+ </div >
34+ <div class =" form-group" >
35+ <label for =" iconUrl" >Icon Url </label >
36+
37+ <InputSelect id =" iconUrl" class =" form-control" @bind-Value =" Hazard.IconUrl" >
38+ @foreach( var image in clientImages .Images )
39+ {
40+ <option value =" @image.RelativePath" >@image.RelativePath </option >
41+ }
42+ </InputSelect >
43+ <img src =" @clientImages.ToAbsolutePath(Hazard.IconUrl)" />
44+
45+ </div >
46+ <div class =" form-group" >
47+ <label for =" mediaUrl" >Media Url </label >
48+ <InputSelect id =" mediaUrl" class =" form-control" @bind-Value =" Hazard.MediaUrl" >
49+ @foreach( var image in clientImages .Images )
50+ {
51+ <option value =" @image.RelativePath" >@image.RelativePath </option >
52+ }
53+ </InputSelect >
54+ <img src =" @clientImages.ToAbsolutePath(Hazard.MediaUrl)" />
55+ </div >
56+
57+ <div class =" form-group" >
58+ <label for =" beforeSafetyDetails" >Before Safety Details </label >
59+ <Editor Id =" beforeSafetyDetails" Conf =" @EditorConfig" ApiKey =" @tinyMCEApiKey" @bind-Value =" @Hazard.BeforeSafetyDetails" />
60+ </div >
61+
62+ <div class =" form-group" >
63+ <label for =" duringSafetyDetails" >During Safety Details </label >
64+ <Editor Id =" duringSafetyDetails" Conf =" @EditorConfig" ApiKey =" @tinyMCEApiKey" @bind-Value =" @Hazard.DuringSafetyDetails" />
65+ </div >
66+
67+ <div class =" form-group" >
68+ <label for =" afterSafetyDetails" >After Safety Details </label >
69+ <Editor Id =" afterSafetyDetails" Conf =" @EditorConfig" ApiKey =" @tinyMCEApiKey" @bind-Value =" @Hazard.AfterSafetyDetails" />
70+ </div >
671
7- <h3 >Details</h3 >
8- Hazard Name:
9- <input type =" text" @bind =" @Hazard.Name" />
10- <br />
11- Short Description:
12- <input type =" text" @bind =" @Hazard.Description" />
13- <span class =" btn btn-secondary float-right" style =" cursor : pointer " @onclick =" @Save" >Save</span >
72+
73+ <div class =" form-group" >
74+ <label for =" externalLinks" >External Links (One link per line )</label >
75+ <textarea rows =" 6" class =" form-control" name =" externalLinks" @bind =" @ExternalLinks" ></textarea >
76+ </div >
77+
78+ <button type =" button" class =" btn btn-primary" @onclick =" @Save" >Submit </button >
79+ </EditForm >
80+ }
1481
1582@code {
83+ public Dictionary <string , object > EditorConfig = new Dictionary <string , object >
84+ {
85+ { " plugins" , " image" },
86+ { " toolbar" , " image" },
87+ };
1688
1789 [Parameter ]
1890 public string Id { get ; set ; }
@@ -22,19 +94,47 @@ Short Description:
2294
2395 private HazardHunt Hazard { get ; set ; }
2496
97+ private string ExternalLinks
98+ {
99+ get
100+ {
101+ return string .Join (Environment .NewLine , Hazard ? .ExternalLinks ?? new string [0 ]);
102+ }
103+ set
104+ {
105+ var links = value .Split (new string [] { " \n " , Environment .NewLine }, StringSplitOptions .RemoveEmptyEntries ).Select (s => s .Trim ()).Where (s => ! string .IsNullOrEmpty (s ));
106+ Hazard .ExternalLinks = links .ToArray ();
107+ }
108+ }
109+
25110
26111 protected override async Task OnInitializedAsync ()
27112 {
28- Hazard = await repository .GetHazardHuntById (Id );
113+ if (string .IsNullOrEmpty (Id ))
114+ {
115+ Hazard = new HazardHunt ();
116+ }
117+ else
118+ {
119+ Hazard = await repository .GetHazardHuntById (Id );
120+ }
121+ EditorConfig [" image_list" ] = clientImages .Images .Select (i => new { title = i .RelativePath , value = i .AbsolutePath }).ToArray ();
29122 }
30-
31-
123+
32124 public async Task Save ()
33125 {
34- await repository .SaveHazardHunt (Hazard );
35- await OnSave .InvokeAsync (Hazard );
126+ if (string .IsNullOrEmpty (Hazard .Id ))
127+ {
128+ Hazard = await repository .CreateHazardHunt (Hazard );
129+ }
130+ else
131+ {
132+ Hazard = await repository .SaveHazardHunt (Hazard );
133+ }
134+
135+ // await OnSave.InvokeAsync(Hazard);
36136
37- await JS .InvokeVoidAsync (" alert" , new object [] { " Hazard Hunt Saved" });
137+ await JS .InvokeVoidAsync (" alert" , new object [] { " Hazard Info Saved" });
38138
39139 }
40140}
0 commit comments