From 25b968d093da5b440cce889d40e4a644c05e27f4 Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Tue, 23 Apr 2019 23:35:07 -0500 Subject: [PATCH] 00-setup complete --- .../books-page/books-page.component.html | 2 +- .../books-page/books-page.component.ts | 16 +++++++++++++++- src/app/shared/state/books.reducer.ts | 17 +++++++++++++++++ src/app/shared/state/index.ts | 5 ++++- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/app/books/components/books-page/books-page.component.html b/src/app/books/components/books-page/books-page.component.html index 69c54a0..1ca8160 100755 --- a/src/app/books/components/books-page/books-page.component.html +++ b/src/app/books/components/books-page/books-page.component.html @@ -3,7 +3,7 @@ diff --git a/src/app/books/components/books-page/books-page.component.ts b/src/app/books/components/books-page/books-page.component.ts index a2d4cb3..128f302 100755 --- a/src/app/books/components/books-page/books-page.component.ts +++ b/src/app/books/components/books-page/books-page.component.ts @@ -3,17 +3,31 @@ import { Component, OnInit } from "@angular/core"; import { Book } from "src/app/shared/models/book.model"; import { BooksService } from "src/app/shared/services/book.service"; +import { Observable } from "rxjs"; +import { Store, select } from "@ngrx/store"; +import * as fromRoot from "src/app/shared/state"; +import { map } from "rxjs/operators"; + @Component({ selector: "app-books", templateUrl: "./books-page.component.html", styleUrls: ["./books-page.component.css"] }) export class BooksPageComponent implements OnInit { + books$: Observable; books: Book[]; currentBook: Book; total: number; - constructor(private booksService: BooksService) {} + constructor( + private booksService: BooksService, + private store: Store + ) { + this.books$ = this.store.pipe( + select(state => state.books), + map(booksState => booksState.books) + ); + } ngOnInit() { this.getBooks(); diff --git a/src/app/shared/state/books.reducer.ts b/src/app/shared/state/books.reducer.ts index f36eee0..4054327 100644 --- a/src/app/shared/state/books.reducer.ts +++ b/src/app/shared/state/books.reducer.ts @@ -29,3 +29,20 @@ const updateBook = (books: Book[], book: Book) => }); const deleteBook = (books: Book[], book: Book) => books.filter(w => book.id !== w.id); + +export interface State { + activeBookId: string | null; + books: Book[]; +} + +export const initialState = { + activeBookId: null, + books: initialBooks +}; + +export function reducer(state = initialState, action: any): State { + switch (action.type) { + default: + return state; + } +} diff --git a/src/app/shared/state/index.ts b/src/app/shared/state/index.ts index 055c5f1..4d0a49e 100644 --- a/src/app/shared/state/index.ts +++ b/src/app/shared/state/index.ts @@ -1,12 +1,15 @@ import { ActionReducerMap, createSelector, MetaReducer } from "@ngrx/store"; import * as fromMovies from "./movie.reducer"; +import * as fromBooks from "./books.reducer"; export interface State { movies: fromMovies.State; + books: fromBooks.State; } export const reducers: ActionReducerMap = { - movies: fromMovies.reducer + movies: fromMovies.reducer, + books: fromBooks.reducer }; export const metaReducers: MetaReducer[] = [];