Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tutorial Sample and some cleanup #264

Merged
merged 15 commits into from
Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ root = true
[*]
# Indentation and spacing
indent_size = 2
indent_style = space
tab_width = 4
indent_style =space
tab_width =2

# New line preferences
end_of_line = lf
Expand Down
36 changes: 34 additions & 2 deletions BlazorState.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28606.126
# Visual Studio Version 17
VisualStudioVersion = 17.0.31808.319
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorState", "source\BlazorState\BlazorState.csproj", "{DF355720-EA1E-4505-AE68-1D36BD0D4711}"
EndProject
Expand Down Expand Up @@ -35,6 +35,16 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
TestCafe.ps1 = TestCafe.ps1
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{FA456F71-9A99-4869-970E-9D5C9A1E6C9F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Client", "Samples\Tutorial\Sample\Client\Sample.Client.csproj", "{EB9B1439-C22F-45CD-AC22-9B003FA1B8BA}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tutorial", "Tutorial", "{243A5B2C-5E9D-432B-A258-7BCE33645683}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Server", "Samples\Tutorial\Sample\Server\Sample.Server.csproj", "{18004EED-3ACC-4A0E-AC01-753675625C47}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Shared", "Samples\Tutorial\Sample\Shared\Sample.Shared.csproj", "{892484E4-8C27-461B-8C0D-81122903A27E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -90,6 +100,24 @@ Global
{1F8DBDA6-780A-414E-8517-E96A1B9536E1}.ReduxDevToolsEnabled|Any CPU.Build.0 = ReduxDevToolsEnabled|Any CPU
{1F8DBDA6-780A-414E-8517-E96A1B9536E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F8DBDA6-780A-414E-8517-E96A1B9536E1}.Release|Any CPU.Build.0 = Release|Any CPU
{EB9B1439-C22F-45CD-AC22-9B003FA1B8BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EB9B1439-C22F-45CD-AC22-9B003FA1B8BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EB9B1439-C22F-45CD-AC22-9B003FA1B8BA}.ReduxDevToolsEnabled|Any CPU.ActiveCfg = Release|Any CPU
{EB9B1439-C22F-45CD-AC22-9B003FA1B8BA}.ReduxDevToolsEnabled|Any CPU.Build.0 = Release|Any CPU
{EB9B1439-C22F-45CD-AC22-9B003FA1B8BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EB9B1439-C22F-45CD-AC22-9B003FA1B8BA}.Release|Any CPU.Build.0 = Release|Any CPU
{18004EED-3ACC-4A0E-AC01-753675625C47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{18004EED-3ACC-4A0E-AC01-753675625C47}.Debug|Any CPU.Build.0 = Debug|Any CPU
{18004EED-3ACC-4A0E-AC01-753675625C47}.ReduxDevToolsEnabled|Any CPU.ActiveCfg = Release|Any CPU
{18004EED-3ACC-4A0E-AC01-753675625C47}.ReduxDevToolsEnabled|Any CPU.Build.0 = Release|Any CPU
{18004EED-3ACC-4A0E-AC01-753675625C47}.Release|Any CPU.ActiveCfg = Release|Any CPU
{18004EED-3ACC-4A0E-AC01-753675625C47}.Release|Any CPU.Build.0 = Release|Any CPU
{892484E4-8C27-461B-8C0D-81122903A27E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{892484E4-8C27-461B-8C0D-81122903A27E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{892484E4-8C27-461B-8C0D-81122903A27E}.ReduxDevToolsEnabled|Any CPU.ActiveCfg = Release|Any CPU
{892484E4-8C27-461B-8C0D-81122903A27E}.ReduxDevToolsEnabled|Any CPU.Build.0 = Release|Any CPU
{892484E4-8C27-461B-8C0D-81122903A27E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{892484E4-8C27-461B-8C0D-81122903A27E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -102,6 +130,10 @@ Global
{37FFCA3B-6533-4262-8913-1E62EC0034CA} = {2902FDFD-016E-4BF7-8B41-53D674FA53DD}
{26D026AD-762A-4EE9-AC1B-FCE1CF5767E9} = {50D398DD-1384-4FF3-9415-E46F5DC2E79F}
{1F8DBDA6-780A-414E-8517-E96A1B9536E1} = {2902FDFD-016E-4BF7-8B41-53D674FA53DD}
{EB9B1439-C22F-45CD-AC22-9B003FA1B8BA} = {243A5B2C-5E9D-432B-A258-7BCE33645683}
{243A5B2C-5E9D-432B-A258-7BCE33645683} = {FA456F71-9A99-4869-970E-9D5C9A1E6C9F}
{18004EED-3ACC-4A0E-AC01-753675625C47} = {243A5B2C-5E9D-432B-A258-7BCE33645683}
{892484E4-8C27-461B-8C0D-81122903A27E} = {243A5B2C-5E9D-432B-A258-7BCE33645683}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {16FA64C2-4A0E-42D4-94E6-C8CFDE84597E}
Expand Down
19 changes: 8 additions & 11 deletions Documentation/Tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,22 @@ namespace Sample.Client.Features.Counter
```csharp
namespace Sample.Client
{
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection;
using BlazorState;
using System.Reflection;
using MediatR;

public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddSingleton
(
new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }
);
builder.RootComponents.Add<App>("#app");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

ConfigureServices(builder.Services);

Expand All @@ -143,6 +140,7 @@ namespace Sample.Client
}
}
}

```

## Displaying state in the user interface
Expand Down Expand Up @@ -228,9 +226,9 @@ To Send the action to the pipeline when the user clicks the `Click me` button,
In `Pages/Counter.razor` update the `IncrementCount` function as follows:

```csharp
void IncrementCount()
async Task IncrementCount()
{
Mediator.Send(new CounterState.IncrementCountAction { Amount = 5 });
await Mediator.Send(new CounterState.IncrementCountAction { Amount = 5 });
}
```

Expand Down Expand Up @@ -325,7 +323,6 @@ namespace Sample.Client
await ReduxDevToolsInterop.InitAsync();
await JsonRequestHandler.InitAsync();
}

}
}
```
Expand Down
10 changes: 10 additions & 0 deletions Samples/Tutorial/Sample/Client/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
23 changes: 23 additions & 0 deletions Samples/Tutorial/Sample/Client/App.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Sample.Client
{
using System.Threading.Tasks;
using BlazorState.Pipeline.ReduxDevTools;
using BlazorState.Features.JavaScriptInterop;
using BlazorState.Features.Routing;
using Microsoft.AspNetCore.Components;

public partial class App : ComponentBase
{
[Inject] private JsonRequestHandler JsonRequestHandler { get; set; }
[Inject] private ReduxDevToolsInterop ReduxDevToolsInterop { get; set; }

// Injected so it is created by the container. Even though the IDE says it is not used, it is.
[Inject] private RouteManager RouteManager { get; set; }

protected override async Task OnAfterRenderAsync(bool firstRender)
{
await ReduxDevToolsInterop.InitAsync();
await JsonRequestHandler.InitAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Sample.Client.Features.Counter
{
using BlazorState;

public partial class CounterState
{
public class IncrementCountAction : IAction
{
public int Amount { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Sample.Client.Features.Counter
{
using System.Threading;
using System.Threading.Tasks;
using BlazorState;
using MediatR;

public partial class CounterState
{
public class IncrementCountHandler : ActionHandler<IncrementCountAction>
{
public IncrementCountHandler(IStore aStore) : base(aStore) { }

CounterState CounterState => Store.GetState<CounterState>();

public override Task<Unit> Handle(IncrementCountAction aIncrementCountAction, CancellationToken aCancellationToken)
{
CounterState.Count = CounterState.Count + aIncrementCountAction.Amount;
return Unit.Task;
}
}
}
}
10 changes: 10 additions & 0 deletions Samples/Tutorial/Sample/Client/Features/Counter/CounterState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Sample.Client.Features.Counter
{
using BlazorState;

public partial class CounterState : State<CounterState>
{
public int Count { get; private set; }
public override void Initialize() => Count = 3;
}
}
22 changes: 22 additions & 0 deletions Samples/Tutorial/Sample/Client/Pages/Counter.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@page "/counter"
@using BlazorState
@using Sample.Client.Features.Counter

@inherits BlazorStateComponent

<h1>Counter</h1>

<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
CounterState CounterState => GetState<CounterState>();

private int currentCount => CounterState.Count;

async Task IncrementCount()
{
await Mediator.Send(new CounterState.IncrementCountAction { Amount = 5 });
}
}
46 changes: 46 additions & 0 deletions Samples/Tutorial/Sample/Client/Pages/FetchData.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@page "/fetchdata"
@using Sample.Shared
@inject HttpClient Http

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from the server.</p>

@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}

@code {
private WeatherForecast[] forecasts;

protected override async Task OnInitializedAsync()
{
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");
}

}
7 changes: 7 additions & 0 deletions Samples/Tutorial/Sample/Client/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@page "/"

<h1>Hello, world!</h1>

Welcome to your new app.

<SurveyPrompt Title="How is Blazor working for you?" />
41 changes: 41 additions & 0 deletions Samples/Tutorial/Sample/Client/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace Sample.Client
{
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection;
using BlazorState;
using System.Reflection;

public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

ConfigureServices(builder.Services);

await builder.Build().RunAsync();
}

public static void ConfigureServices(IServiceCollection aServiceCollection)
{
aServiceCollection.AddBlazorState
(
(aOptions) =>
{
aOptions.UseReduxDevToolsBehavior = true;
aOptions.Assemblies =
new Assembly[]
{
typeof(Program).GetTypeInfo().Assembly,
};
}
);
}
}
}
18 changes: 18 additions & 0 deletions Samples/Tutorial/Sample/Client/Sample.Client.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Blazor-State" Version="4.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.11" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\Sample.Shared.csproj" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions Samples/Tutorial/Sample/Client/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@inherits LayoutComponentBase

<div class="page">
<div class="sidebar">
<NavMenu />
</div>

<div class="main">
<div class="top-row px-4">
<a href="http://blazor.net" target="_blank" class="ml-md-auto">About</a>
</div>

<div class="content px-4">
@Body
</div>
</div>
</div>
Loading