Skip to content

Commit 3697f70

Browse files
committed
Giscus in separate component instead of service
1 parent 2d5fe36 commit 3697f70

File tree

15 files changed

+98
-109
lines changed

15 files changed

+98
-109
lines changed

LinkDotNet.Blog.IntegrationTests/Web/Pages/BlogPostPageTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using LinkDotNet.Blog.Domain;
77
using LinkDotNet.Blog.Infrastructure.Persistence;
88
using LinkDotNet.Blog.TestUtilities;
9+
using LinkDotNet.Blog.Web;
910
using LinkDotNet.Blog.Web.Pages;
1011
using LinkDotNet.Blog.Web.Shared;
1112
using LinkDotNet.Blog.Web.Shared.Services;
@@ -87,7 +88,7 @@ private void RegisterComponents(TestContextBase ctx, ILocalStorageService localS
8788
ctx.Services.AddScoped(_ => new Mock<IToastService>().Object);
8889
ctx.Services.AddScoped(_ => new Mock<IHeadElementHelper>().Object);
8990
ctx.Services.AddScoped(_ => new Mock<IUserRecordService>().Object);
90-
ctx.Services.AddScoped(_ => new Mock<IGiscusService>().Object);
91+
ctx.Services.AddScoped(_ => new AppConfiguration());
9192
}
9293
}
9394
}

LinkDotNet.Blog.UnitTests/Web/AppConfigurationFactoryTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ public void ShouldMapFromAppConfiguration()
5151
appConfiguration.ProfileInformation.Name.Should().Be("Steven");
5252
appConfiguration.ProfileInformation.Heading.Should().Be("Dev");
5353
appConfiguration.ProfileInformation.ProfilePictureUrl.Should().Be("Url");
54-
appConfiguration.Giscus.Repository.Should().Be("repo");
55-
appConfiguration.Giscus.RepositoryId.Should().Be("repoid");
56-
appConfiguration.Giscus.Category.Should().Be("general");
57-
appConfiguration.Giscus.CategoryId.Should().Be("generalid");
54+
appConfiguration.GiscusConfiguration.Repository.Should().Be("repo");
55+
appConfiguration.GiscusConfiguration.RepositoryId.Should().Be("repoid");
56+
appConfiguration.GiscusConfiguration.Category.Should().Be("general");
57+
appConfiguration.GiscusConfiguration.CategoryId.Should().Be("generalid");
5858
}
5959

6060
[Theory]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Linq;
2+
using Bunit;
3+
using FluentAssertions;
4+
using LinkDotNet.Blog.Web;
5+
using LinkDotNet.Blog.Web.Shared;
6+
using LinkDotNet.Blog.Web.Shared.Services;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Xunit;
9+
10+
namespace LinkDotNet.Blog.UnitTests.Web.Shared
11+
{
12+
public class GiscusTests : TestContext
13+
{
14+
[Fact]
15+
public void ShouldLoadJavascript()
16+
{
17+
var giscusData = new GiscusConfiguration
18+
{
19+
Repository = "linkdotnet/somerepo",
20+
RepositoryId = "some_repo_id",
21+
Category = "General",
22+
CategoryId = "GeneralId",
23+
};
24+
Services.AddScoped(_ => new AppConfiguration { GiscusConfiguration = giscusData });
25+
JSInterop.SetupModule("./Shared/Giscus.razor.js");
26+
JSInterop.Mode = JSRuntimeMode.Loose;
27+
28+
RenderComponent<Giscus>();
29+
30+
var init = JSInterop.Invocations.SingleOrDefault(i => i.Identifier == "initGiscus");
31+
init.Should().NotBeNull();
32+
init.Arguments.Should().Contain("giscus");
33+
init.Arguments.Should().Contain(giscusData);
34+
}
35+
36+
[Fact]
37+
public void ShouldNotInitGiscusWhenNoInformationProvided()
38+
{
39+
Services.AddScoped(_ => new AppConfiguration());
40+
41+
RenderComponent<Giscus>();
42+
43+
JSInterop.Invocations.Should().BeEmpty();
44+
}
45+
}
46+
}

LinkDotNet.Blog.UnitTests/Web/Shared/Services/GiscusServiceTests.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

LinkDotNet.Blog.Web/AppConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public record AppConfiguration
2727

2828
public ProfileInformation ProfileInformation { get; init; }
2929

30-
public Giscus Giscus { get; init; }
30+
public GiscusConfiguration GiscusConfiguration { get; init; }
3131

32-
public bool IsGiscusEnabled => Giscus != null;
32+
public bool IsGiscusEnabled => GiscusConfiguration != null;
3333
}
3434
}

LinkDotNet.Blog.Web/AppConfigurationFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static class AppConfigurationFactory
99
public static AppConfiguration Create(IConfiguration config)
1010
{
1111
var profileInformation = config.GetSection("AboutMeProfileInformation").Get<ProfileInformation>();
12-
var giscus = config.GetSection("Giscus").Get<Giscus>();
12+
var giscus = config.GetSection("Giscus").Get<GiscusConfiguration>();
1313
var configuration = new AppConfiguration
1414
{
1515
BlogName = config["BlogName"],
@@ -20,7 +20,7 @@ public static AppConfiguration Create(IConfiguration config)
2020
DatabaseName = config["DatabaseName"],
2121
BlogPostsPerPage = int.Parse(config["BlogPostsPerPage"]),
2222
ProfileInformation = profileInformation,
23-
Giscus = giscus,
23+
GiscusConfiguration = giscus,
2424
};
2525

2626
return configuration;

LinkDotNet.Blog.Web/LinkDotNet.Blog.Web.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
<ItemGroup>
3737
<AdditionalFiles Include="..\stylecop.json" Link="stylecop.json" />
3838
</ItemGroup>
39+
40+
<ItemGroup>
41+
<None Update="Shared\Giscus.razor.js">
42+
<DependentUpon>Giscus.razor</DependentUpon>
43+
</None>
44+
</ItemGroup>
3945
<PropertyGroup>
4046
<CodeAnalysisRuleSet>..\stylecop.analyzers.ruleset</CodeAnalysisRuleSet>
4147
</PropertyGroup>

LinkDotNet.Blog.Web/Pages/BlogPostPage.razor

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
@inject IRepository<BlogPost> blogPostRepository
99
@inject IJSRuntime jsRuntime
1010
@inject IUserRecordService userRecordService
11-
@inject IGiscusService gisgusService
1211
@inherits MarkdownComponentBase
1312

1413
<div class="page">
@@ -41,8 +40,7 @@
4140
</div>
4241
</div>
4342
<Like BlogPost="@BlogPost" OnBlogPostLiked="@UpdateLikes"></Like>
44-
<div class="giscus">
45-
</div>
43+
<Giscus></Giscus>
4644
</div>
4745
</div>
4846
}
@@ -69,7 +67,6 @@
6967
{
7068
await userRecordService.StoreUserRecordAsync();
7169
await jsRuntime.InvokeVoidAsync("hljs.highlightAll");
72-
await gisgusService.EnableCommentSection("giscus");
7370
StateHasChanged();
7471
}
7572
}

LinkDotNet.Blog.Web/Pages/_Host.cshtml

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,5 @@
4747
<script async src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js" integrity="sha512-RXf+QSDCUQs5uwRKaDoXt55jygZZm2V++WUZduaU/Ui/9EGp3f/2KZVahFZBKGH0s774sd3HmrhUy+SgOFQLVQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
4848
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
4949
<script async src="components/slideshow.js" ></script>
50-
V
51-
<script>
52-
window.initGiscus = (divClass, giscus) => {
53-
const script = document.createElement('script');
54-
script.src = 'https://giscus.app/client.js'
55-
script.setAttribute('data-repo', giscus.repository)
56-
script.setAttribute('data-repo-id', giscus.repositoryId)
57-
script.setAttribute('data-category', giscus.category)
58-
script.setAttribute('data-category-id', giscus.categoryId)
59-
script.setAttribute('data-mapping', 'title')
60-
script.setAttribute('data-reactions-enabled', '0')
61-
script.setAttribute('data-emit-metadata', '0')
62-
script.setAttribute('data-theme', 'light')
63-
script.crossOrigin = 'anonymous'
64-
65-
const elementToAppend = document.getElementsByClassName(divClass)[0]
66-
if (elementToAppend) {
67-
elementToAppend.appendChild(script)
68-
}
69-
}
70-
</script>
7150
</body>
7251
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@inject IJSRuntime jsRuntime
2+
@inject AppConfiguration appConfiguration
3+
<div class="giscus">
4+
</div>
5+
6+
@code {
7+
protected override async Task OnAfterRenderAsync(bool firstRender)
8+
{
9+
if (firstRender && appConfiguration.IsGiscusEnabled)
10+
{
11+
await jsRuntime.InvokeAsync<IJSObjectReference>("import", "./Shared/Giscus.razor.js");
12+
await jsRuntime.InvokeVoidAsync("initGiscus", "giscus", appConfiguration.GiscusConfiguration);
13+
}
14+
}
15+
16+
}

0 commit comments

Comments
 (0)