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

03-entity #4

Open
wants to merge 1 commit into
base: 02-actions
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
6 changes: 6 additions & 0 deletions src/app/books/actions/books-page.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import { Book } from "src/app/shared/models/book.model";
import { Action } from "@ngrx/store";

export enum BooksActionTypes {
Enter = "[Books Page] Enter",
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 Enter implements Action {
readonly type = BooksActionTypes.Enter;
}

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

Expand Down Expand Up @@ -38,6 +43,7 @@ export class DeleteBook implements Action {
}

export type BooksActions =
| Enter
| SelectBook
| ClearSelectedBook
| CreateBook
Expand Down
6 changes: 4 additions & 2 deletions src/app/books/components/books-page/books-page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export class BooksPageComponent implements OnInit {
constructor(private store: Store<fromRoot.State>) {
this.books$ = this.store.pipe(
select(state => state.books),
map(booksState => booksState.books),
map((booksState: any) =>
booksState.ids.map(id => booksState.entities[id])
),
tap(books => this.updateTotals(books))
);
}
Expand All @@ -32,7 +34,7 @@ export class BooksPageComponent implements OnInit {
}

getBooks() {
// Pending
this.store.dispatch(new BooksPageActions.Enter());
}

updateTotals(books: Book[]) {
Expand Down
59 changes: 28 additions & 31 deletions src/app/shared/state/books.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,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[] = [
export const initialBooks: Book[] = [
{
id: "1",
name: "Fellowship of the Ring",
Expand All @@ -23,54 +23,51 @@ const initialBooks: Book[] = [
}
];

const createBook = (books: Book[], book: Book) => [...books, book];
const updateBook = (books: Book[], book: Book) =>
books.map(w => {
return w.id === book.id ? Object.assign({}, book) : w;
});
const deleteBook = (books: Book[], book: Book) =>
books.filter(w => book.id !== w.id);

export interface State {
export interface State extends EntityState<Book> {
activeBookId: string | null;
books: Book[];
}

export const initialState = {
activeBookId: null,
books: initialBooks
};
export const adapter = createEntityAdapter<Book>();

export const initialState = adapter.getInitialState({
activeBookId: null
});

export function reducer(
state = initialState,
action: BooksPageActions.BooksActions
): State {
switch (action.type) {
case BooksPageActions.BooksActionTypes.Enter:
return adapter.addAll(initialBooks, state);

case BooksPageActions.BooksActionTypes.SelectBook:
return {
activeBookId: action.bookId,
books: state.books
...state,
activeBookId: action.bookId
};

case BooksPageActions.BooksActionTypes.ClearSelectedBook:
return {
activeBookId: null,
books: state.books
...state,
activeBookId: null
};

case BooksPageActions.BooksActionTypes.CreateBook:
return {
activeBookId: state.activeBookId,
books: createBook(state.books, action.book)
};
return adapter.addOne(action.book, state);

case BooksPageActions.BooksActionTypes.UpdateBook:
return {
activeBookId: state.activeBookId,
books: updateBook(state.books, action.book)
};
return adapter.updateOne(
{ id: action.book.id, changes: action.book },
{ ...state, activeBookId: action.book.id }
);

case BooksPageActions.BooksActionTypes.DeleteBook:
return {
activeBookId: null,
books: deleteBook(state.books, action.book)
};
return adapter.removeOne(action.book.id, {
...state,
activeBookId: null
});

default:
return state;
}
Expand Down