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

02-api-actions #3

Open
wants to merge 1 commit into
base: 01-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
20 changes: 20 additions & 0 deletions src/app/books/actions/books-api.actions.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
import { createAction, props } from "@ngrx/store";
import { BookModel } from "src/app/shared/models";

export const booksLoaded = createAction(
"[Books API] Books Loaded Success",
props<{ books: BookModel[] }>()
);

export const bookCreated = createAction(
"[Books API] Book Created",
props<{ book: BookModel }>()
);

export const bookUpdated = createAction(
"[Books API] Book Updated",
props<{ book: BookModel }>()
);

export const bookDeleted = createAction(
"[Books API] Book Deleted",
props<{ bookId: string }>()
);
14 changes: 11 additions & 3 deletions src/app/books/components/books-page/books-page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
BookRequiredProps
} from "src/app/shared/models";
import { BooksService } from "src/app/shared/services";
import { BooksPageActions } from "../../actions";
import { BooksPageActions, BooksApiActions } from "../../actions";

@Component({
selector: "app-books",
Expand Down Expand Up @@ -35,6 +35,8 @@ export class BooksPageComponent implements OnInit {
this.booksService.all().subscribe(books => {
this.books = books;
this.updateTotals(books);

this.store.dispatch(BooksApiActions.booksLoaded({ books }));
});
}

Expand Down Expand Up @@ -69,9 +71,11 @@ export class BooksPageComponent implements OnInit {
saveBook(bookProps: BookRequiredProps) {
this.store.dispatch(BooksPageActions.createBook({ book: bookProps }));

this.booksService.create(bookProps).subscribe(() => {
this.booksService.create(bookProps).subscribe(book => {
this.getBooks();
this.removeSelectedBook();

this.store.dispatch(BooksApiActions.bookCreated({ book }));
});
}

Expand All @@ -80,9 +84,11 @@ export class BooksPageComponent implements OnInit {
BooksPageActions.updateBook({ bookId: book.id, changes: book })
);

this.booksService.update(book.id, book).subscribe(() => {
this.booksService.update(book.id, book).subscribe(book => {
this.getBooks();
this.removeSelectedBook();

this.store.dispatch(BooksApiActions.bookUpdated({ book }));
});
}

Expand All @@ -92,6 +98,8 @@ export class BooksPageComponent implements OnInit {
this.booksService.delete(book.id).subscribe(() => {
this.getBooks();
this.removeSelectedBook();

this.store.dispatch(BooksApiActions.bookDeleted({ bookId: book.id }));
});
}
}