-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from le-nn/feature/support_for_blazor_web_app
Add Blazor Web App Samples
- Loading branch information
Showing
20 changed files
with
428 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
samples/Memento.Samples.Blazor.WebApp/Memento.Samples.Blazor.WebApp.Client/Index2.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
@page "/index2" | ||
|
||
<PageTitle>Index2</PageTitle> | ||
@System.OperatingSystem.IsBrowser() | ||
<h1>Hello, world 22222222!</h1> | ||
|
||
Welcome to your new app. | ||
|
||
|
||
<button @onclick="IncrementCount">Click me: @count</button> | ||
|
||
@code { | ||
private int count = 0; | ||
|
||
private void IncrementCount() | ||
{ | ||
count++; | ||
} | ||
|
||
private void DecrementCount() | ||
{ | ||
count--; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...es/Memento.Samples.Blazor.WebApp/Memento.Samples.Blazor.WebApp.Client/LoggerMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using Memento.Core; | ||
using Microsoft.JSInterop; | ||
|
||
namespace Memento.Sample.Blazor; | ||
|
||
/// <summary> | ||
/// Middleware for logging state changes. | ||
/// </summary> | ||
public sealed class LoggerMiddleware : Middleware { | ||
/// <summary> | ||
/// Creates a new instance of the LoggerMiddlewareHandler. | ||
/// </summary> | ||
/// <param name="provider">The service provider used to resolve required services.</param> | ||
/// <returns>A new LoggerMiddlewareHandler instance.</returns> | ||
protected override MiddlewareHandler Create(IServiceProvider provider) { | ||
return new LoggerMiddlewareHandler( | ||
provider.GetRequiredService<IJSRuntime>() | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// Handler for logging state changes in the LoggerMiddleware. | ||
/// </summary> | ||
/// <remarks> | ||
/// Creates a new instance of the LoggerMiddlewareHandler. | ||
/// </remarks> | ||
/// <param name="jSRuntime">The JavaScript runtime to be used for logging.</param> | ||
public class LoggerMiddlewareHandler(IJSRuntime jSRuntime) : MiddlewareHandler { | ||
private readonly IJSRuntime _jSRuntime = jSRuntime; | ||
|
||
/// <summary> | ||
/// Handles logging the state changes before passing them to the next middleware. | ||
/// </summary> | ||
/// <param name="state">The current state of the application.</param> | ||
/// <param name="e">The state change event arguments.</param> | ||
/// <param name="next">The next middleware in the pipeline.</param> | ||
/// <returns>The updated state after processing by the middleware pipeline.</returns> | ||
public override RootState? HandleProviderDispatch( | ||
RootState? state, | ||
IStateChangedEventArgs<object, object> e, | ||
NextProviderMiddlewareCallback next | ||
) { | ||
_ = HandleLog(state, e); | ||
return next(state, e); | ||
} | ||
|
||
/// <summary> | ||
/// Logs the state changes using the JavaScript console. | ||
/// </summary> | ||
/// <param name="state">The current state of the application.</param> | ||
/// <param name="e">The state change event arguments.</param> | ||
/// <returns>A task representing the logging operation.</returns> | ||
public async Task HandleLog(object? state, IStateChangedEventArgs<object, object> e) { | ||
if (state is null) { | ||
return; | ||
} | ||
|
||
await _jSRuntime.InvokeVoidAsync("console.log", new { | ||
StateName = state.GetType().Name, | ||
State = state, | ||
EventArgs = e, | ||
}); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...r.WebApp/Memento.Samples.Blazor.WebApp.Client/Memento.Samples.Blazor.WebApp.Client.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile> | ||
<StaticWebAssetProjectMode>Default</StaticWebAssetProjectMode> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Memento.Blazor\Memento.Blazor.csproj" /> | ||
<ProjectReference Include="..\..\..\src\Memento.Core\Memento.Core.csproj" /> | ||
<ProjectReference Include="..\..\..\src\Memento.ReduxDevTool.Browser\Memento.ReduxDevTool.Browser.csproj" /> | ||
<ProjectReference Include="..\..\Memento.Sample.Blazor\Memento.Sample.Blazor.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
19 changes: 19 additions & 0 deletions
19
samples/Memento.Samples.Blazor.WebApp/Memento.Samples.Blazor.WebApp.Client/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Memento.Blazor; | ||
using Memento.ReduxDevTool.Browser; | ||
using Memento.Sample.Blazor; | ||
using Memento.Sample.Blazor.Todos; | ||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting; | ||
|
||
var builder = WebAssemblyHostBuilder.CreateDefault(args); | ||
builder.Services | ||
.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }) | ||
.AddScoped<ITodoService, MockTodoService>() | ||
// Memento | ||
.AddMemento() | ||
.AddMiddleware(() => new LoggerMiddleware()) | ||
.AddBrowserReduxDevToolMiddleware(new() { | ||
StackTraceEnabled = true, | ||
OpenDevTool = true, | ||
}) | ||
.ScanAssemblyAndAddStores(typeof(Memento.Sample.Blazor.App).Assembly); | ||
await builder.Build().RunAsync(); |
16 changes: 16 additions & 0 deletions
16
samples/Memento.Samples.Blazor.WebApp/Memento.Samples.Blazor.WebApp.Client/Routes.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@using Memento.Sample.Blazor.Shared | ||
|
||
<Memento.Blazor.MementoInitializer /> | ||
|
||
<Router AppAssembly="typeof(Program).Assembly" AdditionalAssemblies="[typeof(Sample.Blazor.App).Assembly]"> | ||
<Found Context="routeData"> | ||
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" /> | ||
<FocusOnNavigate RouteData="@routeData" Selector="h1" /> | ||
</Found> | ||
<NotFound> | ||
<PageTitle>Not found</PageTitle> | ||
<LayoutView Layout="@typeof(MainLayout)"> | ||
<p role="alert">Sorry, there's nothing at this address.</p> | ||
</LayoutView> | ||
</NotFound> | ||
</Router> |
9 changes: 9 additions & 0 deletions
9
samples/Memento.Samples.Blazor.WebApp/Memento.Samples.Blazor.WebApp.Client/_Imports.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@using System.Net.Http | ||
@using System.Net.Http.Json | ||
@using Microsoft.AspNetCore.Components.Forms | ||
@using Microsoft.AspNetCore.Components.Routing | ||
@using Microsoft.AspNetCore.Components.Web | ||
@using static Microsoft.AspNetCore.Components.Web.RenderMode | ||
@using Microsoft.AspNetCore.Components.Web.Virtualization | ||
@using Microsoft.JSInterop | ||
@using Memento.Samples.Blazor.WebApp.Client |
20 changes: 20 additions & 0 deletions
20
samples/Memento.Samples.Blazor.WebApp/Memento.Samples.Blazor.WebApp/Components/App.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<base href="/" /> | ||
|
||
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" /> | ||
<link href="css/app.css" rel="stylesheet" /> | ||
<link rel="stylesheet" href="Memento.Samples.Blazor.WebApp.styles.css" /> | ||
<HeadOutlet /> | ||
</head> | ||
|
||
<body> | ||
<Routes @rendermode="InteractiveAuto" /> | ||
<script src="_framework/blazor.web.js"></script> | ||
</body> | ||
|
||
</html> |
36 changes: 36 additions & 0 deletions
36
.../Memento.Samples.Blazor.WebApp/Memento.Samples.Blazor.WebApp/Components/Pages/Error.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
@page "/Error" | ||
@using System.Diagnostics | ||
|
||
<PageTitle>Error</PageTitle> | ||
|
||
<h1 class="text-danger">Error.</h1> | ||
<h2 class="text-danger">An error occurred while processing your request.</h2> | ||
|
||
@if (ShowRequestId) | ||
{ | ||
<p> | ||
<strong>Request ID:</strong> <code>@RequestId</code> | ||
</p> | ||
} | ||
|
||
<h3>Development Mode</h3> | ||
<p> | ||
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred. | ||
</p> | ||
<p> | ||
<strong>The Development environment shouldn't be enabled for deployed applications.</strong> | ||
It can result in displaying sensitive information from exceptions to end users. | ||
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong> | ||
and restarting the app. | ||
</p> | ||
|
||
@code{ | ||
[CascadingParameter] | ||
private HttpContext? HttpContext { get; set; } | ||
|
||
private string? RequestId { get; set; } | ||
private bool ShowRequestId => !string.IsNullOrEmpty(RequestId); | ||
|
||
protected override void OnInitialized() => | ||
RequestId = Activity.Current?.Id ?? HttpContext?.TraceIdentifier; | ||
} |
11 changes: 11 additions & 0 deletions
11
...les/Memento.Samples.Blazor.WebApp/Memento.Samples.Blazor.WebApp/Components/_Imports.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
@using System.Net.Http | ||
@using System.Net.Http.Json | ||
@using Microsoft.AspNetCore.Components.Forms | ||
@using Microsoft.AspNetCore.Components.Routing | ||
@using Microsoft.AspNetCore.Components.Web | ||
@using static Microsoft.AspNetCore.Components.Web.RenderMode | ||
@using Microsoft.AspNetCore.Components.Web.Virtualization | ||
@using Microsoft.JSInterop | ||
@using Memento.Samples.Blazor.WebApp | ||
@using Memento.Samples.Blazor.WebApp.Client | ||
@using Memento.Samples.Blazor.WebApp.Components |
14 changes: 14 additions & 0 deletions
14
....Samples.Blazor.WebApp/Memento.Samples.Blazor.WebApp/Memento.Samples.Blazor.WebApp.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Memento.Samples.Blazor.WebApp.Client\Memento.Samples.Blazor.WebApp.Client.csproj" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |
49 changes: 49 additions & 0 deletions
49
samples/Memento.Samples.Blazor.WebApp/Memento.Samples.Blazor.WebApp/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Memento.Blazor; | ||
using Memento.ReduxDevTool.Browser; | ||
using Memento.Sample.Blazor; | ||
using Memento.Sample.Blazor.Todos; | ||
using Memento.Samples.Blazor.WebApp.Components; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services | ||
.AddScoped(sp => new HttpClient { BaseAddress = new Uri("https://localhost:7288/") }) | ||
.AddScoped<ITodoService, MockTodoService>() | ||
// Memento | ||
.AddMemento() | ||
.AddMiddleware(() => new ServerLoggerMiddleware()) | ||
.AddBrowserReduxDevToolMiddleware(new() { | ||
StackTraceEnabled = true, | ||
OpenDevTool = true, | ||
}, true) | ||
.ScanAssemblyAndAddStores(typeof(Memento.Sample.Blazor._Imports).Assembly); | ||
|
||
// Add services to the container. | ||
builder.Services.AddRazorComponents() | ||
.AddInteractiveServerComponents() | ||
.AddInteractiveWebAssemblyComponents(); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) { | ||
app.UseWebAssemblyDebugging(); | ||
} | ||
else { | ||
app.UseExceptionHandler("/Error", createScopeForErrors: true); | ||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. | ||
app.UseHsts(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseStaticFiles(); | ||
app.UseAntiforgery(); | ||
|
||
app.MapRazorComponents<Memento.Samples.Blazor.WebApp.Components.App>() | ||
.AddInteractiveServerRenderMode() | ||
.AddInteractiveWebAssemblyRenderMode() | ||
.AddAdditionalAssemblies(typeof(Memento.Sample.Blazor.App).Assembly) // shared project | ||
.AddAdditionalAssemblies(typeof(Memento.Samples.Blazor.WebApp.Client._Imports).Assembly); // client project | ||
|
||
app.Run(); |
Oops, something went wrong.