Skip to content

Commit 4d5e9a4

Browse files
00-setup complete
1 parent 75ee852 commit 4d5e9a4

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

src/app/books/components/books-page/books-page.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</app-books-total>
66

77
<app-books-list
8-
[books]="books"
8+
[books]="books$ | async"
99
(select)="onSelect($event)"
1010
(delete)="onDelete($event)">
1111
</app-books-list>

src/app/books/components/books-page/books-page.component.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,31 @@ import { Component, OnInit } from '@angular/core';
33
import { Book } from 'src/app/shared/models/book.model';
44
import { BooksService } from 'src/app/shared/services/book.service';
55

6+
import { Observable } from 'rxjs';
7+
import { Store, select } from '@ngrx/store';
8+
import * as fromRoot from 'src/app/shared/state';
9+
import { map } from 'rxjs/operators';
10+
611
@Component({
712
selector: 'app-books',
813
templateUrl: './books-page.component.html',
914
styleUrls: ['./books-page.component.css']
1015
})
1116
export class BooksPageComponent implements OnInit {
17+
books$: Observable<Book[]>;
1218
books: Book[];
1319
currentBook: Book;
1420
total: number;
1521

16-
constructor(private booksService: BooksService) {}
22+
constructor(
23+
private booksService: BooksService,
24+
private store: Store<fromRoot.State>
25+
) {
26+
this.books$ = this.store.pipe(
27+
select(state => state.books),
28+
map(booksState => booksState.books)
29+
);
30+
}
1731

1832
ngOnInit() {
1933
this.getBooks();

src/app/shared/state/books.reducer.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,21 @@ const updateBook = (books: Book[], book: Book) => books.map(w => {
2727
return w.id === book.id ? Object.assign({}, book) : w;
2828
});
2929
const deleteBook = (books: Book[], book: Book) => books.filter(w => book.id !== w.id);
30+
31+
export interface State {
32+
activeBookId: string | null;
33+
books: Book[];
34+
}
35+
36+
export const initialState = {
37+
activeBookId: null,
38+
books: initialBooks
39+
};
40+
41+
export function reducer(state = initialState, action: any): State {
42+
switch(action.type) {
43+
44+
default:
45+
return state;
46+
}
47+
}

src/app/shared/state/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { ActionReducerMap, createSelector, MetaReducer } from "@ngrx/store";
22
import * as fromMovies from "./movie.reducer";
3+
import * as fromBooks from "./books.reducer";
34

45
export interface State {
56
movies: fromMovies.State;
7+
books: fromBooks.State;
68
}
79

810
export const reducers: ActionReducerMap<State> = {
9-
movies: fromMovies.reducer
11+
movies: fromMovies.reducer,
12+
books: fromBooks.reducer
1013
};
1114

1215
export const metaReducers: MetaReducer<State>[] = [];

0 commit comments

Comments
 (0)