Skip to content

Commit 7ebd811

Browse files
committed
02-api-actions
1 parent 7c450a7 commit 7ebd811

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
1-
import { createAction } from "@ngrx/store";
1+
import { createAction, props } from "@ngrx/store";
22
import { BookModel } from "src/app/shared/models/book.model";
3+
4+
export const booksLoaded = createAction(
5+
"[Books API] Books Loaded Success",
6+
props<{ books: BookModel[] }>()
7+
);
8+
9+
export const bookCreated = createAction(
10+
"[Books API] Book Created",
11+
props<{ book: BookModel }>()
12+
);
13+
14+
export const bookUpdated = createAction(
15+
"[Books API] Book Updated",
16+
props<{ book: BookModel }>()
17+
);
18+
19+
export const bookDeleted = createAction(
20+
"[Books API] Book Deleted",
21+
props<{ bookId: string }>()
22+
);

src/app/books/components/books-page/books-page.component.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
BookRequiredProps
88
} from "src/app/shared/models/book.model";
99
import { BooksService } from "src/app/shared/services/book.service";
10-
import { BooksPageActions } from "../../actions";
10+
import { BooksPageActions, BooksApiActions } from "../../actions";
1111

1212
@Component({
1313
selector: "app-books",
@@ -35,6 +35,8 @@ export class BooksPageComponent implements OnInit {
3535
this.booksService.all().subscribe(books => {
3636
this.books = books;
3737
this.updateTotals(books);
38+
39+
this.store.dispatch(BooksApiActions.booksLoaded({ books }));
3840
});
3941
}
4042

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

72-
this.booksService.create(bookProps).subscribe(() => {
74+
this.booksService.create(bookProps).subscribe(book => {
7375
this.getBooks();
7476
this.removeSelectedBook();
77+
78+
this.store.dispatch(BooksApiActions.bookCreated({ book }));
7579
});
7680
}
7781

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

83-
this.booksService.update(book.id, book).subscribe(() => {
87+
this.booksService.update(book.id, book).subscribe(book => {
8488
this.getBooks();
8589
this.removeSelectedBook();
90+
91+
this.store.dispatch(BooksApiActions.bookUpdated({ book }));
8692
});
8793
}
8894

@@ -92,6 +98,8 @@ export class BooksPageComponent implements OnInit {
9298
this.booksService.delete(book.id).subscribe(() => {
9399
this.getBooks();
94100
this.removeSelectedBook();
101+
102+
this.store.dispatch(BooksApiActions.bookDeleted({ bookId: book.id }));
95103
});
96104
}
97105
}

0 commit comments

Comments
 (0)