Skip to content

Commit 7c450a7

Browse files
committed
01-actions
1 parent 3c245b1 commit 7c450a7

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed
Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,28 @@
1-
import { createAction } from "@ngrx/store";
2-
import { BookModel } from "src/app/shared/models/book.model";
1+
import { createAction, props } from "@ngrx/store";
2+
import { BookRequiredProps, BookModel } from "src/app/shared/models/book.model";
3+
4+
export const enter = createAction("[Books Page] Enter");
5+
6+
export const selectBook = createAction(
7+
"[Books Page] Select Book",
8+
props<{ bookId: string }>()
9+
);
10+
11+
export const clearSelectedBook = createAction(
12+
"[Books Page] Clear Selected Book"
13+
);
14+
15+
export const createBook = createAction(
16+
"[Books Page] Create Book",
17+
props<{ book: BookRequiredProps }>()
18+
);
19+
20+
export const updateBook = createAction(
21+
"[Books Page] Update Book",
22+
props<{ bookId: string; changes: BookRequiredProps }>()
23+
);
24+
25+
export const deleteBook = createAction(
26+
"[Books Page] Delete Book",
27+
props<{ bookId: string }>()
28+
);

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { Component, OnInit } from "@angular/core";
2+
import { Store } from "@ngrx/store";
3+
import { State } from "src/app/shared/state";
24
import {
35
BookModel,
46
calculateBooksGrossEarnings,
57
BookRequiredProps
68
} from "src/app/shared/models/book.model";
79
import { BooksService } from "src/app/shared/services/book.service";
10+
import { BooksPageActions } from "../../actions";
811

912
@Component({
1013
selector: "app-books",
@@ -16,9 +19,14 @@ export class BooksPageComponent implements OnInit {
1619
currentBook: BookModel | null = null;
1720
total: number = 0;
1821

19-
constructor(private booksService: BooksService) {}
22+
constructor(
23+
private booksService: BooksService,
24+
private store: Store<State>
25+
) {}
2026

2127
ngOnInit() {
28+
this.store.dispatch(BooksPageActions.enter());
29+
2230
this.getBooks();
2331
this.removeSelectedBook();
2432
}
@@ -35,6 +43,8 @@ export class BooksPageComponent implements OnInit {
3543
}
3644

3745
onSelect(book: BookModel) {
46+
this.store.dispatch(BooksPageActions.selectBook({ bookId: book.id }));
47+
3848
this.currentBook = book;
3949
}
4050

@@ -43,6 +53,8 @@ export class BooksPageComponent implements OnInit {
4353
}
4454

4555
removeSelectedBook() {
56+
this.store.dispatch(BooksPageActions.clearSelectedBook());
57+
4658
this.currentBook = null;
4759
}
4860

@@ -55,20 +67,28 @@ export class BooksPageComponent implements OnInit {
5567
}
5668

5769
saveBook(bookProps: BookRequiredProps) {
70+
this.store.dispatch(BooksPageActions.createBook({ book: bookProps }));
71+
5872
this.booksService.create(bookProps).subscribe(() => {
5973
this.getBooks();
6074
this.removeSelectedBook();
6175
});
6276
}
6377

6478
updateBook(book: BookModel) {
79+
this.store.dispatch(
80+
BooksPageActions.updateBook({ bookId: book.id, changes: book })
81+
);
82+
6583
this.booksService.update(book.id, book).subscribe(() => {
6684
this.getBooks();
6785
this.removeSelectedBook();
6886
});
6987
}
7088

7189
onDelete(book: BookModel) {
90+
this.store.dispatch(BooksPageActions.deleteBook({ bookId: book.id }));
91+
7292
this.booksService.delete(book.id).subscribe(() => {
7393
this.getBooks();
7494
this.removeSelectedBook();

0 commit comments

Comments
 (0)