Skip to content

Commit

Permalink
02-actions complete
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts committed Apr 26, 2019
1 parent 645f68e commit b9a5c11
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
44 changes: 44 additions & 0 deletions src/app/books/actions/books-page.actions.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,46 @@
import { Book } from "src/app/shared/models/book.model";
import { Action } from "@ngrx/store";

export enum BooksActionTypes {
SelectBook = '[Books Page] Select Book',
ClearSelectedBook = '[Books Page] Clear Selected Book',
CreateBook = '[Books Page] Create Book',
UpdateBook = '[Books Page] Update Book',
DeleteBook = '[Books Page] Delete Book'
}

export class SelectBook implements Action {
readonly type = BooksActionTypes.SelectBook;

constructor(public bookId: string) {}
}

export class ClearSelectedBook implements Action {
readonly type = BooksActionTypes.ClearSelectedBook;
}

export class CreateBook implements Action {
readonly type = BooksActionTypes.CreateBook;

constructor(public book: Book) {}
}

export class UpdateBook implements Action {
readonly type = BooksActionTypes.UpdateBook;

constructor(public book: Book) {}
}

export class DeleteBook implements Action {
readonly type = BooksActionTypes.DeleteBook;

constructor(public book: Book) {}
}

export type BooksActions =
| SelectBook
| ClearSelectedBook
| CreateBook
| UpdateBook
| DeleteBook
;
16 changes: 9 additions & 7 deletions src/app/books/components/books-page/books-page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Book } from 'src/app/shared/models/book.model';
import { Observable } from 'rxjs';
import { Store, select } from '@ngrx/store';
import * as fromRoot from 'src/app/shared/state';
import { map } from 'rxjs/operators';
import { map, tap } from 'rxjs/operators';
import { BooksPageActions } from '../../actions';

@Component({
selector: 'app-books',
Expand All @@ -22,7 +23,8 @@ export class BooksPageComponent implements OnInit {
) {
this.books$ = this.store.pipe(
select(state => state.books),
map(booksState => booksState.books)
map(booksState => booksState.books),
tap(books => this.updateTotals(books))
);
}

Expand All @@ -42,7 +44,7 @@ export class BooksPageComponent implements OnInit {
}

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

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

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

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

saveBook(book: Book) {
this.store.dispatch({ type: 'create', book });
this.store.dispatch(new BooksPageActions.CreateBook(book));
}

updateBook(book: Book) {
this.store.dispatch({ type: 'update', book });
this.store.dispatch(new BooksPageActions.UpdateBook(book));
}

onDelete(book: Book) {
this.store.dispatch({ type: 'delete', book });
this.store.dispatch(new BooksPageActions.DeleteBook(book));
}
}
14 changes: 8 additions & 6 deletions src/app/shared/state/books.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +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';


const initialBooks: Book[] = [
{
Expand Down Expand Up @@ -38,29 +40,29 @@ export const initialState = {
books: initialBooks
};

export function reducer(state = initialState, action: any): State {
export function reducer(state = initialState, action: BooksPageActions.BooksActions): State {
switch(action.type) {
case 'select':
case BooksPageActions.BooksActionTypes.SelectBook:
return {
activeBookId: action.bookId,
books: state.books
};
case 'clear select':
case BooksPageActions.BooksActionTypes.ClearSelectedBook:
return {
activeBookId: null,
books: state.books
};
case 'create':
case BooksPageActions.BooksActionTypes.CreateBook:
return {
activeBookId: state.activeBookId,
books: createBook(state.books, action.book)
};
case 'update':
case BooksPageActions.BooksActionTypes.UpdateBook:
return {
activeBookId: state.activeBookId,
books: updateBook(state.books, action.book)
};
case 'delete':
case BooksPageActions.BooksActionTypes.DeleteBook:
return {
activeBookId: null,
books: deleteBook(state.books, action.book)
Expand Down

0 comments on commit b9a5c11

Please sign in to comment.