Skip to content

Commit 645f68e

Browse files
01-reducers complete
1 parent 4d5e9a4 commit 645f68e

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed
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,12 +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

2220
constructor(
23-
private booksService: BooksService,
2421
private store: Store<fromRoot.State>
2522
) {
2623
this.books$ = this.store.pipe(
@@ -35,11 +32,7 @@ export class BooksPageComponent implements OnInit {
3532
}
3633

3734
getBooks() {
38-
this.booksService.all()
39-
.subscribe(books => {
40-
this.books = books;
41-
this.updateTotals(books);
42-
});
35+
// Pending
4336
}
4437

4538
updateTotals(books: Book[]) {
@@ -49,6 +42,7 @@ export class BooksPageComponent implements OnInit {
4942
}
5043

5144
onSelect(book: Book) {
45+
this.store.dispatch({ type: 'select', bookId: book.id });
5246
this.currentBook = book;
5347
}
5448

@@ -57,6 +51,7 @@ export class BooksPageComponent implements OnInit {
5751
}
5852

5953
removeSelectedBook() {
54+
this.store.dispatch({ type: 'clear select' });
6055
this.currentBook = null;
6156
}
6257

@@ -69,26 +64,14 @@ export class BooksPageComponent implements OnInit {
6964
}
7065

7166
saveBook(book: Book) {
72-
this.booksService.create(book)
73-
.subscribe(() => {
74-
this.getBooks();
75-
this.removeSelectedBook();
76-
});
67+
this.store.dispatch({ type: 'create', book });
7768
}
7869

7970
updateBook(book: Book) {
80-
this.booksService.update(book.id, book)
81-
.subscribe(() => {
82-
this.getBooks();
83-
this.removeSelectedBook();
84-
});
71+
this.store.dispatch({ type: 'update', book });
8572
}
8673

8774
onDelete(book: Book) {
88-
this.booksService.delete(book.id)
89-
.subscribe(() => {
90-
this.getBooks();
91-
this.removeSelectedBook();
92-
});
75+
this.store.dispatch({ type: 'delete', book });
9376
}
9477
}

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

+25-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,31 @@ export const initialState = {
4040

4141
export function reducer(state = initialState, action: any): State {
4242
switch(action.type) {
43-
43+
case 'select':
44+
return {
45+
activeBookId: action.bookId,
46+
books: state.books
47+
};
48+
case 'clear select':
49+
return {
50+
activeBookId: null,
51+
books: state.books
52+
};
53+
case 'create':
54+
return {
55+
activeBookId: state.activeBookId,
56+
books: createBook(state.books, action.book)
57+
};
58+
case 'update':
59+
return {
60+
activeBookId: state.activeBookId,
61+
books: updateBook(state.books, action.book)
62+
};
63+
case 'delete':
64+
return {
65+
activeBookId: null,
66+
books: deleteBook(state.books, action.book)
67+
};
4468
default:
4569
return state;
4670
}

0 commit comments

Comments
 (0)