Skip to content

Commit c785826

Browse files
brandonrobertsMikeRyanDev
authored andcommitted
01-reducers complete
1 parent 25b968d commit c785826

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed
Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Component, OnInit } from "@angular/core";
22

33
import { Book } from "src/app/shared/models/book.model";
4-
import { BooksService } from "src/app/shared/services/book.service";
54

65
import { Observable } from "rxjs";
76
import { Store, select } from "@ngrx/store";
@@ -15,14 +14,10 @@ import { map } from "rxjs/operators";
1514
})
1615
export class BooksPageComponent implements OnInit {
1716
books$: Observable<Book[]>;
18-
books: Book[];
1917
currentBook: Book;
2018
total: number;
2119

22-
constructor(
23-
private booksService: BooksService,
24-
private store: Store<fromRoot.State>
25-
) {
20+
constructor(private store: Store<fromRoot.State>) {
2621
this.books$ = this.store.pipe(
2722
select(state => state.books),
2823
map(booksState => booksState.books)
@@ -35,10 +30,7 @@ export class BooksPageComponent implements OnInit {
3530
}
3631

3732
getBooks() {
38-
this.booksService.all().subscribe(books => {
39-
this.books = books;
40-
this.updateTotals(books);
41-
});
33+
// Pending
4234
}
4335

4436
updateTotals(books: Book[]) {
@@ -48,6 +40,7 @@ export class BooksPageComponent implements OnInit {
4840
}
4941

5042
onSelect(book: Book) {
43+
this.store.dispatch({ type: "select", bookId: book.id });
5144
this.currentBook = book;
5245
}
5346

@@ -56,6 +49,7 @@ export class BooksPageComponent implements OnInit {
5649
}
5750

5851
removeSelectedBook() {
52+
this.store.dispatch({ type: "clear select" });
5953
this.currentBook = null;
6054
}
6155

@@ -68,23 +62,14 @@ export class BooksPageComponent implements OnInit {
6862
}
6963

7064
saveBook(book: Book) {
71-
this.booksService.create(book).subscribe(() => {
72-
this.getBooks();
73-
this.removeSelectedBook();
74-
});
65+
this.store.dispatch({ type: "create", book });
7566
}
7667

7768
updateBook(book: Book) {
78-
this.booksService.update(book.id, book).subscribe(() => {
79-
this.getBooks();
80-
this.removeSelectedBook();
81-
});
69+
this.store.dispatch({ type: "update", book });
8270
}
8371

8472
onDelete(book: Book) {
85-
this.booksService.delete(book.id).subscribe(() => {
86-
this.getBooks();
87-
this.removeSelectedBook();
88-
});
73+
this.store.dispatch({ type: "delete", book });
8974
}
9075
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ export const initialState = {
4242

4343
export function reducer(state = initialState, action: any): State {
4444
switch (action.type) {
45+
case "select":
46+
return {
47+
activeBookId: action.bookId,
48+
books: state.books
49+
};
50+
case "clear select":
51+
return {
52+
activeBookId: null,
53+
books: state.books
54+
};
55+
case "create":
56+
return {
57+
activeBookId: state.activeBookId,
58+
books: createBook(state.books, action.book)
59+
};
60+
case "update":
61+
return {
62+
activeBookId: state.activeBookId,
63+
books: updateBook(state.books, action.book)
64+
};
65+
case "delete":
66+
return {
67+
activeBookId: null,
68+
books: deleteBook(state.books, action.book)
69+
};
4570
default:
4671
return state;
4772
}

0 commit comments

Comments
 (0)