-
Notifications
You must be signed in to change notification settings - Fork 915
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5edcb3
commit 0fc5dc4
Showing
8 changed files
with
53 additions
and
55 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 9 additions & 11 deletions
20
projects/angular-ngrx-material-starter/src/app/features/examples/crud/books.actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,17 @@ | ||
import { Action } from '@ngrx/store'; | ||
import { createAction, props } from '@ngrx/store'; | ||
import { Book } from './books.model'; | ||
|
||
export enum BookActionTypes { | ||
UPSERT_ONE = '[Books] Upsert One', | ||
DELETE_ONE = '[Books] Delete One' | ||
} | ||
|
||
export class ActionBooksUpsertOne implements Action { | ||
readonly type = BookActionTypes.UPSERT_ONE; | ||
constructor(readonly payload: { book: Book }) {} | ||
} | ||
|
||
export class ActionBooksDeleteOne implements Action { | ||
readonly type = BookActionTypes.DELETE_ONE; | ||
constructor(readonly payload: { id: string }) {} | ||
} | ||
export const actionBooksUpsertOne = createAction( | ||
BookActionTypes.UPSERT_ONE, | ||
props<{ book: Book }>() | ||
); | ||
|
||
export type BookActions = ActionBooksUpsertOne | ActionBooksDeleteOne; | ||
export const actionBooksDeleteOne = createAction( | ||
BookActionTypes.DELETE_ONE, | ||
props<{ id: string }>() | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 14 additions & 11 deletions
25
projects/angular-ngrx-material-starter/src/app/features/examples/crud/books.effects.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,33 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Action, select, Store } from '@ngrx/store'; | ||
import { Actions, Effect, ofType } from '@ngrx/effects'; | ||
import { select, Store } from '@ngrx/store'; | ||
import { Actions, createEffect, ofType } from '@ngrx/effects'; | ||
import { tap, withLatestFrom } from 'rxjs/operators'; | ||
|
||
import { LocalStorageService } from '../../../core/core.module'; | ||
|
||
import { State } from '../examples.state'; | ||
import { BookActionTypes } from './books.actions'; | ||
import { actionBooksDeleteOne, actionBooksUpsertOne } from './books.actions'; | ||
import { selectBooks } from './books.selectors'; | ||
|
||
export const BOOKS_KEY = 'EXAMPLES.BOOKS'; | ||
|
||
@Injectable() | ||
export class BooksEffects { | ||
constructor( | ||
private actions$: Actions<Action>, | ||
private actions$: Actions, | ||
private store: Store<State>, | ||
private localStorageService: LocalStorageService | ||
) {} | ||
|
||
@Effect({ dispatch: false }) | ||
persistBooks = this.actions$.pipe( | ||
ofType(BookActionTypes.UPSERT_ONE, BookActionTypes.DELETE_ONE), | ||
withLatestFrom(this.store.pipe(select(selectBooks))), | ||
tap(([actions, booksState]) => | ||
this.localStorageService.setItem(BOOKS_KEY, booksState) | ||
) | ||
persistBooks = createEffect( | ||
() => | ||
this.actions$.pipe( | ||
ofType(actionBooksUpsertOne.type, actionBooksDeleteOne.type), | ||
withLatestFrom(this.store.pipe(select(selectBooks))), | ||
tap(([actions, booksState]) => | ||
this.localStorageService.setItem(BOOKS_KEY, booksState) | ||
) | ||
), | ||
{ dispatch: false } | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters