Skip to content

Commit 0b801f5

Browse files
committed
Add Redux
1 parent a26d4b8 commit 0b801f5

40 files changed

+1432
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Contains all of my examples from various blog posts. You can find a comprehensiv
44

55
| BlogPost | Publish Date |
66
| -------------------------------------------------------------------------------------------------------------- | ------------ |
7-
| [Blazor with TailwindCSS](BlazorWithTailwind/) | 18.11.2023 |
7+
| [Redux Pattern in Blazor](ReduxBlazor/) | 04.11.2023 |
8+
| [Blazor with TailwindCSS](BlazorWithTailwind/) | 18.10.2023 |
89
| [Structured Concurrency in C#](StructuredConcurrency/) | 11.10.2023 |
910
| [Building a Minimal ASP.NET Core clone](AspNetCoreFromScratch/) | 14.09.2023 |
1011
| [Enabling List<T> to store large amounts of elements](ChunkedList/) | 07.09.2023 |

ReduxBlazor/App.razor

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Router AppAssembly="@typeof(App).Assembly">
2+
<Found Context="routeData">
3+
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/>
4+
<FocusOnNavigate RouteData="@routeData" Selector="h1"/>
5+
</Found>
6+
<NotFound>
7+
<PageTitle>Not found</PageTitle>
8+
<LayoutView Layout="@typeof(MainLayout)">
9+
<p role="alert">Sorry, there's nothing at this address.</p>
10+
</LayoutView>
11+
</NotFound>
12+
</Router>

ReduxBlazor/AppReducer.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using ReduxBlazor.Redux;
2+
3+
namespace ReduxBlazor;
4+
5+
public class AppReducer : IReducer<ApplicationState>
6+
{
7+
private readonly CounterReducer _counterReducer = new();
8+
9+
public ApplicationState Reduce(ApplicationState state, IAction action)
10+
{
11+
return new ApplicationState(_counterReducer.Reduce(state.CounterState, action));
12+
}
13+
}

ReduxBlazor/ApplicationState.cs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace ReduxBlazor;
2+
3+
public record ApplicationState(CounterState CounterState);

ReduxBlazor/CounterReducer.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using ReduxBlazor.Redux;
2+
3+
namespace ReduxBlazor;
4+
5+
public class CounterReducer : IReducer<CounterState>
6+
{
7+
public CounterState Reduce(CounterState state, IAction action)
8+
{
9+
return action switch
10+
{
11+
IncrementAction incrementAction => state with { Count = state.Count + incrementAction.Value },
12+
_ => state
13+
};
14+
}
15+
}

ReduxBlazor/CounterState.cs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace ReduxBlazor;
2+
3+
public record CounterState(int Count = 0);

ReduxBlazor/IncrementAction.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using ReduxBlazor.Redux;
2+
3+
namespace ReduxBlazor;
4+
5+
public record IncrementAction(int Value) : IAction
6+
{
7+
}

ReduxBlazor/Pages/Counter.razor

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@page "/counter"
2+
@inject Store Store
3+
4+
<PageTitle>Counter</PageTitle>
5+
6+
<h1>Counter</h1>
7+
8+
<p role="status">Current count: @State.Count</p>
9+
10+
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
11+
12+
@code {
13+
private CounterState State => Store.GetState().CounterState;
14+
15+
private void IncrementCount()
16+
{
17+
Store.Dispatch(new IncrementAction(1));
18+
}
19+
}

ReduxBlazor/Pages/FetchData.razor

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
@page "/fetchdata"
2+
@inject HttpClient Http
3+
4+
<PageTitle>Weather forecast</PageTitle>
5+
6+
<h1>Weather forecast</h1>
7+
8+
<p>This component demonstrates fetching data from the server.</p>
9+
10+
@if (forecasts == null)
11+
{
12+
<p>
13+
<em>Loading...</em>
14+
</p>
15+
}
16+
else
17+
{
18+
<table class="table">
19+
<thead>
20+
<tr>
21+
<th>Date</th>
22+
<th>Temp. (C)</th>
23+
<th>Temp. (F)</th>
24+
<th>Summary</th>
25+
</tr>
26+
</thead>
27+
<tbody>
28+
@foreach (var forecast in forecasts)
29+
{
30+
<tr>
31+
<td>@forecast.Date.ToShortDateString()</td>
32+
<td>@forecast.TemperatureC</td>
33+
<td>@forecast.TemperatureF</td>
34+
<td>@forecast.Summary</td>
35+
</tr>
36+
}
37+
</tbody>
38+
</table>
39+
}
40+
41+
@code {
42+
private WeatherForecast[]? forecasts;
43+
44+
protected override async Task OnInitializedAsync()
45+
{
46+
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json");
47+
}
48+
49+
public class WeatherForecast
50+
{
51+
public DateOnly Date { get; set; }
52+
53+
public int TemperatureC { get; set; }
54+
55+
public string? Summary { get; set; }
56+
57+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
58+
}
59+
60+
}

ReduxBlazor/Pages/Index.razor

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@page "/"
2+
3+
<PageTitle>Index</PageTitle>
4+
5+
<h1>Hello, world!</h1>
6+
7+
Welcome to your new app.
8+
9+
<SurveyPrompt Title="How is Blazor working for you?"/>

ReduxBlazor/Program.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.AspNetCore.Components.Web;
2+
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
3+
using ReduxBlazor;
4+
using ReduxBlazor.Redux;
5+
6+
var builder = WebAssemblyHostBuilder.CreateDefault(args);
7+
builder.RootComponents.Add<App>("#app");
8+
builder.RootComponents.Add<HeadOutlet>("head::after");
9+
10+
builder.Services.AddSingleton<IReducer<ApplicationState>, AppReducer>();
11+
builder.Services.AddSingleton<Store>();
12+
13+
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
14+
15+
await builder.Build().RunAsync();
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:26422",
7+
"sslPort": 44309
8+
}
9+
},
10+
"profiles": {
11+
"http": {
12+
"commandName": "Project",
13+
"dotnetRunMessages": true,
14+
"launchBrowser": true,
15+
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
16+
"applicationUrl": "http://localhost:5188",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
}
20+
},
21+
"https": {
22+
"commandName": "Project",
23+
"dotnetRunMessages": true,
24+
"launchBrowser": true,
25+
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
26+
"applicationUrl": "https://localhost:7024;http://localhost:5188",
27+
"environmentVariables": {
28+
"ASPNETCORE_ENVIRONMENT": "Development"
29+
}
30+
},
31+
"IIS Express": {
32+
"commandName": "IISExpress",
33+
"launchBrowser": true,
34+
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
35+
"environmentVariables": {
36+
"ASPNETCORE_ENVIRONMENT": "Development"
37+
}
38+
}
39+
}
40+
}

ReduxBlazor/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Redux Pattern in Blazor
2+
3+
In this blog post we will use the Redux pattern with a small Blazor application. To demonstrate the inner workings, we will built everything from scratch.
4+
5+
Found [here](https://steven-giesel.com/blogPost/d62f8d80-ba9b-47c3-9040-8e17affda513)

ReduxBlazor/Redux/IAction.cs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace ReduxBlazor.Redux;
2+
3+
public interface IAction
4+
{
5+
}

ReduxBlazor/Redux/IReducer.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ReduxBlazor.Redux;
2+
3+
public interface IReducer<TState>
4+
{
5+
TState Reduce(TState state, IAction action);
6+
}

ReduxBlazor/ReduxBlazor.csproj

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.11"/>
11+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.11" PrivateAssets="all"/>
12+
</ItemGroup>
13+
14+
</Project>

ReduxBlazor/ReduxBlazor.sln

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReduxBlazor", "ReduxBlazor.csproj", "{F7E9843C-0581-432A-9BA0-D8132B2F85DC}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{F7E9843C-0581-432A-9BA0-D8132B2F85DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{F7E9843C-0581-432A-9BA0-D8132B2F85DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{F7E9843C-0581-432A-9BA0-D8132B2F85DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{F7E9843C-0581-432A-9BA0-D8132B2F85DC}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

ReduxBlazor/Shared/MainLayout.razor

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@inherits LayoutComponentBase
2+
3+
<div class="page">
4+
<div class="sidebar">
5+
<NavMenu/>
6+
</div>
7+
8+
<main>
9+
<div class="top-row px-4">
10+
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
11+
</div>
12+
13+
<article class="content px-4">
14+
@Body
15+
</article>
16+
</main>
17+
</div>
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
.page {
2+
position: relative;
3+
display: flex;
4+
flex-direction: column;
5+
}
6+
7+
main {
8+
flex: 1;
9+
}
10+
11+
.sidebar {
12+
background-image: linear-gradient(180deg, rgb(5, 39, 103) 0%, #3a0647 70%);
13+
}
14+
15+
.top-row {
16+
background-color: #f7f7f7;
17+
border-bottom: 1px solid #d6d5d5;
18+
justify-content: flex-end;
19+
height: 3.5rem;
20+
display: flex;
21+
align-items: center;
22+
}
23+
24+
.top-row ::deep a, .top-row ::deep .btn-link {
25+
white-space: nowrap;
26+
margin-left: 1.5rem;
27+
text-decoration: none;
28+
}
29+
30+
.top-row ::deep a:hover, .top-row ::deep .btn-link:hover {
31+
text-decoration: underline;
32+
}
33+
34+
.top-row ::deep a:first-child {
35+
overflow: hidden;
36+
text-overflow: ellipsis;
37+
}
38+
39+
@media (max-width: 640.98px) {
40+
.top-row:not(.auth) {
41+
display: none;
42+
}
43+
44+
.top-row.auth {
45+
justify-content: space-between;
46+
}
47+
48+
.top-row ::deep a, .top-row ::deep .btn-link {
49+
margin-left: 0;
50+
}
51+
}
52+
53+
@media (min-width: 641px) {
54+
.page {
55+
flex-direction: row;
56+
}
57+
58+
.sidebar {
59+
width: 250px;
60+
height: 100vh;
61+
position: sticky;
62+
top: 0;
63+
}
64+
65+
.top-row {
66+
position: sticky;
67+
top: 0;
68+
z-index: 1;
69+
}
70+
71+
.top-row.auth ::deep a:first-child {
72+
flex: 1;
73+
text-align: right;
74+
width: 0;
75+
}
76+
77+
.top-row, article {
78+
padding-left: 2rem !important;
79+
padding-right: 1.5rem !important;
80+
}
81+
}

0 commit comments

Comments
 (0)