Skip to content

Commit

Permalink
01-reducers complete
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts committed Apr 26, 2019
1 parent 4d5e9a4 commit 645f68e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
29 changes: 6 additions & 23 deletions src/app/books/components/books-page/books-page.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Component, OnInit } from '@angular/core';

import { Book } from 'src/app/shared/models/book.model';
import { BooksService } from 'src/app/shared/services/book.service';

import { Observable } from 'rxjs';
import { Store, select } from '@ngrx/store';
Expand All @@ -15,12 +14,10 @@ import { map } from 'rxjs/operators';
})
export class BooksPageComponent implements OnInit {
books$: Observable<Book[]>;
books: Book[];
currentBook: Book;
total: number;

constructor(
private booksService: BooksService,
private store: Store<fromRoot.State>
) {
this.books$ = this.store.pipe(
Expand All @@ -35,11 +32,7 @@ export class BooksPageComponent implements OnInit {
}

getBooks() {
this.booksService.all()
.subscribe(books => {
this.books = books;
this.updateTotals(books);
});
// Pending
}

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

onSelect(book: Book) {
this.store.dispatch({ type: 'select', bookId: book.id });
this.currentBook = book;
}

Expand All @@ -57,6 +51,7 @@ export class BooksPageComponent implements OnInit {
}

removeSelectedBook() {
this.store.dispatch({ type: 'clear select' });
this.currentBook = null;
}

Expand All @@ -69,26 +64,14 @@ export class BooksPageComponent implements OnInit {
}

saveBook(book: Book) {
this.booksService.create(book)
.subscribe(() => {
this.getBooks();
this.removeSelectedBook();
});
this.store.dispatch({ type: 'create', book });
}

updateBook(book: Book) {
this.booksService.update(book.id, book)
.subscribe(() => {
this.getBooks();
this.removeSelectedBook();
});
this.store.dispatch({ type: 'update', book });
}

onDelete(book: Book) {
this.booksService.delete(book.id)
.subscribe(() => {
this.getBooks();
this.removeSelectedBook();
});
this.store.dispatch({ type: 'delete', book });
}
}
26 changes: 25 additions & 1 deletion src/app/shared/state/books.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,31 @@ export const initialState = {

export function reducer(state = initialState, action: any): State {
switch(action.type) {

case 'select':
return {
activeBookId: action.bookId,
books: state.books
};
case 'clear select':
return {
activeBookId: null,
books: state.books
};
case 'create':
return {
activeBookId: state.activeBookId,
books: createBook(state.books, action.book)
};
case 'update':
return {
activeBookId: state.activeBookId,
books: updateBook(state.books, action.book)
};
case 'delete':
return {
activeBookId: null,
books: deleteBook(state.books, action.book)
};
default:
return state;
}
Expand Down

0 comments on commit 645f68e

Please sign in to comment.