Skip to content

Commit 3e4309e

Browse files
dpaquetteDavid Paquette
andauthored
Ability to configure client images in admin app (#123)
* Ability to configure client images in admin app * An Msbuild native way of generated client images file * Ensure client images file is copied to output Co-authored-by: David Paquette <dave.paquette@outlook.com>
1 parent 4003142 commit 3e4309e

File tree

8 files changed

+119
-25
lines changed

8 files changed

+119
-25
lines changed

.github/workflows/admin.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ on:
44
push:
55
paths:
66
- 'admin/**'
7+
- '2wr-app/public/images/**'
78
- 'TwoWeeksReady.Common/**'
89
- '.github/workflows/admin.yaml'
910
branches: [ master ]
1011
pull_request:
1112
paths:
1213
- 'admin/**'
14+
- '2wr-app/public/images/**'
1315
- '.github/workflows/admin.yaml'
1416
branches: [ master ]
1517
workflow_dispatch:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,4 @@ MigrationBackup/
353353
# Ionide (cross platform F# VS Code tools) working folder
354354
.ionide/
355355
/lambda/*.zip
356+
admin/TwoWeeksReady.Admin/client-images.txt
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace TwoWeeksReady.Admin.Data
2+
{
3+
public class ClientImage
4+
{
5+
public string RelativePath { get; set; }
6+
public string AbsolutePath { get; set; }
7+
}
8+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Microsoft.Extensions.Configuration;
2+
using System.IO;
3+
using System.Linq;
4+
5+
namespace TwoWeeksReady.Admin.Data
6+
{
7+
/// <summary>
8+
/// Provides a list of image that are available within the 2wr client app.
9+
/// These are use in Hazard Info and in Base Kits
10+
/// </summary>
11+
public class ClientImageService
12+
{
13+
public readonly IConfiguration _configuration;
14+
public readonly ClientImage[] clientImages;
15+
16+
public ClientImageService(IConfiguration configuration)
17+
{
18+
_configuration = configuration;
19+
20+
var imagePaths = File.ReadAllLines("./client-images.txt");
21+
var appUrl = _configuration["2wrAppUrl"].TrimEnd('/');
22+
23+
clientImages = imagePaths
24+
.Select(i => i.Replace("\\", "/"))
25+
.Select(i => new ClientImage
26+
{
27+
AbsolutePath = ToAbsolutePath(i),
28+
RelativePath = i
29+
}).ToArray();
30+
}
31+
32+
33+
public ClientImage[] Images => clientImages;
34+
35+
public string ToAbsolutePath(string relativeImagePath)
36+
{
37+
var appUrl = _configuration["2wrAppUrl"].TrimEnd('/');
38+
if (string.IsNullOrEmpty(relativeImagePath))
39+
{
40+
return appUrl;
41+
}
42+
43+
return $"{appUrl}/{relativeImagePath.TrimStart('/')}";
44+
}
45+
}
46+
}

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
@attribute [Authorize(Roles = "admin")]
44

55
@using TinyMCE.Blazor
6+
@using System.IO;
7+
@using System.Linq;
8+
69
@inject IRepository repository
710
@inject IJSRuntime JS
811
@inject Microsoft.Extensions.Configuration.IConfiguration configuration
12+
@inject ClientImageService clientImages
913

1014
@{
1115
var tinyMCEApiKey = configuration["TinyMCEApiKey"];
@@ -19,7 +23,7 @@ else
1923
{
2024

2125
<h3>Details</h3>
22-
<form>
26+
<EditForm Model="@Hazard">
2327
<div class="form-group">
2428
<label for="name">Hazard Name</label>
2529
<input type="text" class="form-control" name="name" @bind="@Hazard.Name">
@@ -30,11 +34,25 @@ else
3034
</div>
3135
<div class="form-group">
3236
<label for="iconUrl">Icon Url</label>
33-
<input type="text" class="form-control" name="iconUrl" @bind="@Hazard.IconUrl">
37+
38+
<InputSelect id="iconUrl" class="form-control" @bind-Value="Hazard.IconUrl">
39+
@foreach(var image in clientImages.Images)
40+
{
41+
<option value="@image">@image</option>
42+
}
43+
</InputSelect>
44+
<img src="@clientImages.ToAbsolutePath(Hazard.IconUrl)"/>
45+
3446
</div>
3547
<div class="form-group">
3648
<label for="mediaUrl">Media Url</label>
37-
<input type="text" class="form-control" name="mediaUrl" @bind="@Hazard.MediaUrl">
49+
<InputSelect id="mediaUrl" class="form-control" @bind-Value="Hazard.MediaUrl">
50+
@foreach(var image in clientImages.Images)
51+
{
52+
<option value="@image">@image</option>
53+
}
54+
</InputSelect>
55+
<img src="@clientImages.ToAbsolutePath(Hazard.MediaUrl)"/>
3856
</div>
3957

4058
<div class="form-group">
@@ -59,21 +77,15 @@ else
5977
</div>
6078

6179
<button type="button" class="btn btn-primary" @onclick="@Save">Submit</button>
62-
</form>
80+
</EditForm>
6381
}
6482

6583
@code {
6684

6785
public Dictionary<string, object> EditorConfig = new Dictionary<string, object>
68-
{
86+
{
6987
{ "plugins", "image" },
7088
{ "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-
7789
};
7890

7991
[Parameter]
@@ -103,13 +115,12 @@ else
103115
if (string.IsNullOrEmpty(Id))
104116
{
105117
Hazard = new HazardInfo();
106-
107118
}
108119
else
109120
{
110121
Hazard = await repository.GetHazardInfoById(Id);
111122
}
112-
123+
EditorConfig["image_list"] = clientImages.Images.Select(i => new { title = i.RelativePath, value = i.AbsolutePath }).ToArray();
113124
}
114125

115126

admin/TwoWeeksReady.Admin/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public void ConfigureServices(IServiceCollection services)
115115
services.AddScoped<TokenProvider>();
116116
//services.AddScoped<IRepository, StubRepository>();
117117
services.AddScoped<IRepository, FunctionsRepository>();
118+
services.AddSingleton<ClientImageService>();
118119
}
119120

120121
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

3-
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
5-
<UserSecretsId>4c82d094-3afe-4274-922b-9c0d8bdda7c5</UserSecretsId>
6-
</PropertyGroup>
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<UserSecretsId>4c82d094-3afe-4274-922b-9c0d8bdda7c5</UserSecretsId>
6+
</PropertyGroup>
77

8-
<ItemGroup>
9-
<ProjectReference Include="..\..\TwoWeeksReady.Common\TwoWeeksReady.Common.csproj" />
10-
</ItemGroup>
8+
<ItemGroup>
9+
<Content Include="client-images.txt">
10+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
11+
</Content>
12+
</ItemGroup>
1113

12-
<ItemGroup>
13-
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.6" />
14-
<PackageReference Include="TinyMCE.Blazor" Version="0.0.8" />
15-
</ItemGroup>
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\TwoWeeksReady.Common\TwoWeeksReady.Common.csproj" />
16+
</ItemGroup>
1617

17-
</Project>
18+
<ItemGroup>
19+
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.6" />
20+
<PackageReference Include="TinyMCE.Blazor" Version="0.0.8" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ClientImages Include="..\..\2wr-app\public\images\**\*.*" />
25+
</ItemGroup>
26+
27+
<Target Name="ClearClientImagesFile">
28+
<Delete Files=".\client-images.txt" />
29+
<Touch AlwaysCreate="true" Files=".\client-images.txt" />
30+
</Target>
31+
32+
<Target Name="PreBuild" BeforeTargets="PreBuildEvent" DependsOnTargets="ClearClientImagesFile">
33+
<!-- Generates an up-to-date file of all the images available in the client app -->
34+
<PropertyGroup>
35+
<ClientImage>%(ClientImages.Identity)</ClientImage>
36+
<ClientImageIndexOffset>$([MSBuild]::Add($(ClientImage.IndexOf(`2wr-app`)), 14))</ClientImageIndexOffset>
37+
</PropertyGroup>
38+
<Message Text="Client Image: %(ClientImages.Identity)" Importance="high" />
39+
<WriteLinesToFile Lines="$([System.String]::Copy(&quot;%(ClientImages.Identity)&quot;).Substring($(ClientImageIndexOffset)))" File="./client-images.txt" Overwrite="false" />
40+
</Target>
41+
</Project>

admin/TwoWeeksReady.Admin/appsettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"AllowedHosts": "*",
1010
"TinyMCEApiKey": "your-api-key",
1111
"ApiUrl": "http://localhost:7071/api/",
12+
"2wrAppUrl": "http://localhost:8080/",
1213
"Auth0": {
1314
"Domain": "YOUR_AUTH0_DOMAIN",
1415
"ClientId": "YOUR_CLIENT_ID",

0 commit comments

Comments
 (0)