From df32a04d5148c7bdd868e36de885d6b064c86eba Mon Sep 17 00:00:00 2001 From: Chanan Braunstein Date: Sat, 2 Jun 2018 09:43:57 -0600 Subject: [PATCH] Updated Fluxor integration sample --- src/FluxorIntegration/Pages/Index.cshtml | 102 ++++++++++-------- .../Store/Movies/InsertSeedDataReducer.cs | 56 ++++++++++ .../Store/Movies/InsertSeedMoviesAction.cs | 8 ++ 3 files changed, 124 insertions(+), 42 deletions(-) create mode 100644 src/FluxorIntegration/Store/Movies/InsertSeedDataReducer.cs create mode 100644 src/FluxorIntegration/Store/Movies/InsertSeedMoviesAction.cs diff --git a/src/FluxorIntegration/Pages/Index.cshtml b/src/FluxorIntegration/Pages/Index.cshtml index 0ffd5d6..d18384d 100644 --- a/src/FluxorIntegration/Pages/Index.cshtml +++ b/src/FluxorIntegration/Pages/Index.cshtml @@ -9,11 +9,25 @@

Movie Reviews

- +

Notes

+ + + +

Movies

+ + @if (State.Current.Movies.Count > 0) { - +
@@ -28,62 +42,60 @@ - - + + }
Id
@movie.Id @movie.Name
} -

Add New Movie

+

Add New Movie

- -
- +
+
+ -

Reviews

- @if (ReviewState.Current.MovieId != 0) { +

Reviews

+ @if (ReviewState.Current.Reviews != null) - { - - - - - - - - - - - @foreach (var review in ReviewState.Current.Reviews) - { - - - - - - - } - -
IdStarsCommentDelete
@review.Id@review.Stars@review.Comment
- } - -

Add New Review

+ { + + + + + + + + + + + @foreach (var review in ReviewState.Current.Reviews) + { + + + + + + + } + +
IdStarsCommentDelete
@review.Id@review.Stars@review.Comment
+ } + +

Add New Review

-
- +
+
-
- -
-
- +
+
+ } @@ -130,4 +142,10 @@ await Dispatcher.DispatchAsync(new DeleteMovieAction(movieId)); StateHasChanged(); } + + async void OnInsertSeedData() + { + await Dispatcher.DispatchAsync(new InsertSeedMoviesAction()); + StateHasChanged(); + } } \ No newline at end of file diff --git a/src/FluxorIntegration/Store/Movies/InsertSeedDataReducer.cs b/src/FluxorIntegration/Store/Movies/InsertSeedDataReducer.cs new file mode 100644 index 0000000..f0f5c72 --- /dev/null +++ b/src/FluxorIntegration/Store/Movies/InsertSeedDataReducer.cs @@ -0,0 +1,56 @@ +using System.Collections.Generic; +using System.Linq; +using Blazor.Fluxor; +using FluxorIntegration.Models; + +namespace FluxorIntegration.Store.Movies +{ + public class InsertSeedDataReducer : IReducer + { + private Context Context { get; set; } + + public InsertSeedDataReducer(Context context) + { + Context = context; + } + public MovieState Reduce(MovieState state, InsertSeedMoviesAction action) + { + var query = from m in Context.Movies + where m.Name == "Solo: A Star Wars Story" + select m; + if (!query.Any()) + { + var movie = new Movie + { + Name = "Solo: A Star Wars Story", + Reviews = new List + { + new Review {Stars = 4, Comment = "Pretty good!"}, + new Review {Stars = 3, Comment = "Not as good as Rogue One"} + } + }; + Context.Movies.Add(movie); + } + + query = from m in Context.Movies + where m.Name == "Isle of Dog" + select m; + if (!query.Any()) + { + var movie = new Movie + { + Name = "Isle of Dog", + Reviews = new List + { + new Review {Stars = 5, Comment = "Awesome stop motion movie!"} + } + }; + Context.Movies.Add(movie); + } + + Context.SaveChanges(); + + return new MovieState(Context.Movies); + } + } +} diff --git a/src/FluxorIntegration/Store/Movies/InsertSeedMoviesAction.cs b/src/FluxorIntegration/Store/Movies/InsertSeedMoviesAction.cs new file mode 100644 index 0000000..b339ae2 --- /dev/null +++ b/src/FluxorIntegration/Store/Movies/InsertSeedMoviesAction.cs @@ -0,0 +1,8 @@ +using Blazor.Fluxor; + +namespace FluxorIntegration.Store.Movies +{ + public class InsertSeedMoviesAction : IAction + { + } +}