Skip to content

Commit 0b801f5

Browse files
committed
Add Redux
1 parent a26d4b8 commit 0b801f5

40 files changed

+1432
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 13 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 19 additions & 0 deletions
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

Lines changed: 60 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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?"/>

0 commit comments

Comments
 (0)