Skip to content

Commit

Permalink
04-selectors complete
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts committed Apr 26, 2019
1 parent b2d2184 commit c26fbe5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/app/books/components/books-page/books-page.component.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="container">
<div class="col-50">
<app-books-total
[total]="total">
[total]="total$ | async">
</app-books-total>

<app-books-list
Expand All @@ -12,7 +12,7 @@
</div>

<app-book-detail class="col-50"
[book]="currentBook"
[book]="currentBook$ | async"
(save)="onSave($event)"
(cancel)="onCancel()">
</app-book-detail>
Expand Down
20 changes: 5 additions & 15 deletions src/app/books/components/books-page/books-page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@ import { BooksPageActions } from '../../actions';
})
export class BooksPageComponent implements OnInit {
books$: Observable<Book[]>;
currentBook: Book;
total: number;
currentBook$: Observable<Book>;
total$: Observable<number>;

constructor(
private store: Store<fromRoot.State>
) {
this.books$ = this.store.pipe(
select(state => state.books),
map((booksState: any) => booksState.ids.map(id => booksState.entities[id])),
tap(books => this.updateTotals(books))
);
this.books$ = this.store.pipe(select(fromRoot.selectAllBooks));
this.currentBook$ = this.store.pipe(select(fromRoot.selectActiveBook));
this.total$ = this.store.pipe(select(fromRoot.selectBookEarningsTotals));
}

ngOnInit() {
Expand All @@ -37,15 +35,8 @@ export class BooksPageComponent implements OnInit {
this.store.dispatch(new BooksPageActions.Enter());
}

updateTotals(books: Book[]) {
this.total = books.reduce((total, book) => {
return total + parseInt(`${book.earnings}`, 10) || 0;
}, 0);
}

onSelect(book: Book) {
this.store.dispatch(new BooksPageActions.SelectBook(book.id));
this.currentBook = book;
}

onCancel() {
Expand All @@ -54,7 +45,6 @@ export class BooksPageComponent implements OnInit {

removeSelectedBook() {
this.store.dispatch(new BooksPageActions.ClearSelectedBook());
this.currentBook = null;
}

onSave(book: Book) {
Expand Down
2 changes: 2 additions & 0 deletions src/app/shared/state/books.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ export function reducer(state = initialState, action: BooksPageActions.BooksActi
}
}

export const { selectAll, selectEntities } = adapter.getSelectors();
export const activeBookId = (state: State) => state.activeBookId;
30 changes: 30 additions & 0 deletions src/app/shared/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,33 @@ export const selectMoviesEarningsTotal = createSelector(
selectMovies,
movies => movies.reduce((total, movie) => total + parseInt(`${movie.earnings}`, 10) || 0, 0)
);

export const selectBooksState = (state: State) => state.books;

export const selectActiveBookId = createSelector(
selectBooksState,
fromBooks.activeBookId
);

export const selectAllBooks = createSelector(
selectBooksState,
fromBooks.selectAll
);

export const selectAllBooksEntities = createSelector(
selectBooksState,
fromBooks.selectEntities
);

export const selectActiveBook = createSelector(
selectAllBooksEntities,
selectActiveBookId,
(entities, bookId) => bookId ? entities[bookId] : null
);

export const selectBookEarningsTotals = createSelector(
selectAllBooks,
books => books.reduce((total, book) => {
return total + parseInt(`${book.earnings}`, 10) || 0;
}, 0)
)

0 comments on commit c26fbe5

Please sign in to comment.