Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

04-selectors #5

Open
wants to merge 1 commit into
base: 03-entity
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,6 +1,6 @@
<div class="container">
<div class="col-50">
<app-books-total [total]="total"> </app-books-total>
<app-books-total [total]="total$ | async"> </app-books-total>

<app-books-list
[books]="books$ | async"
Expand All @@ -12,7 +12,7 @@

<app-book-detail
class="col-50"
[book]="currentBook"
[book]="activeBook$ | async"
(save)="onSave($event)"
(cancel)="onCancel()"
>
Expand Down
22 changes: 5 additions & 17 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,13 @@ import { BooksPageActions } from "../../actions";
})
export class BooksPageComponent implements OnInit {
books$: Observable<Book[]>;
currentBook: Book;
total: number;
activeBook$: 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.activeBook$ = this.store.pipe(select(fromRoot.selectActiveBook));
this.total$ = this.store.pipe(select(fromRoot.selectBookEarningsTotals));
}

ngOnInit() {
Expand All @@ -37,15 +33,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 +43,6 @@ export class BooksPageComponent implements OnInit {

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

onSave(book: Book) {
Expand Down
16 changes: 16 additions & 0 deletions src/app/shared/state/books.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createEntityAdapter, EntityAdapter, EntityState } from "@ngrx/entity";
import { Book } from "src/app/shared/models/book.model";
import { BooksPageActions } from "src/app/books/actions";
import { createSelector } from "@ngrx/store";

export const initialBooks: Book[] = [
{
Expand Down Expand Up @@ -72,3 +73,18 @@ export function reducer(
return state;
}
}

export const { selectAll, selectEntities } = adapter.getSelectors();
export const selectActiveBookId = (state: State) => state.activeBookId;
export const selectActiveBook = createSelector(
selectEntities,
selectActiveBookId,
(entities, bookId) => (bookId ? entities[bookId] : null)
);
export const selectEarningsTotals = createSelector(
selectAll,
books =>
books.reduce((total, book) => {
return total + parseInt(`${book.earnings}`, 10) || 0;
}, 0)
);
17 changes: 17 additions & 0 deletions src/app/shared/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,20 @@ export const selectMoviesEarningsTotal = createSelector(
selectMovieState,
fromMovies.selectEarningsTotal
);

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

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

export const selectActiveBook = createSelector(
selectBooksState,
fromBooks.selectActiveBook
);

export const selectBookEarningsTotals = createSelector(
selectBooksState,
fromBooks.selectEarningsTotals
);