This file contains hidden or 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
@Effect() | |
fetchProducts: Observable<Action> = this.actions$.pipe( | |
ofType<actions.FetchProducts>(actions.FETCH_PRODUCTS), | |
switchMap(() => | |
this.productService.getProducts().pipe( | |
map(products => new actions.FetchProductsSuccess(products)), | |
catchError(() => of(new actions.FetchProductsError())) | |
) | |
) | |
); |
This file contains hidden or 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
@Component({...}) | |
export class ProductDetailsComponent { | |
product$ = this.store.select(selectors.getCurrentProduct); | |
constructor( | |
private readonly store: Store<{}>, | |
) { | |
this.store.dispatch(new actions.FetchCurrentProduct()); | |
} | |
} |
This file contains hidden or 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
@Effect() | |
fetchProduct: Observable<Action> = this.actions$.pipe( | |
ofType(actions.FETCH_CURRENT_PRODUCT), | |
withLatestFrom( | |
this.store.select(selectors.getCurrentProductId) | |
), | |
switchMap(id => | |
this.productService.getProduct(id).pipe( | |
map(product => new actions.FetchProductSuccess(product)), | |
catchError(() => of(new actions.FetchProductError())) |
This file contains hidden or 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
export function reducer( | |
state: ProductState = initState, | |
action: actions.All | |
): ProductState { | |
switch (action.type) { | |
... | |
case actions.FETCH_PRODUCTS_SUCCESS: { | |
return { | |
// addAll replaces current list of products with new list | |
products: productAdapter.addAll(action.payload, state.products), |
This file contains hidden or 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
export function productSync(reducer: ActionReducer<{ product: ProductState }>) { | |
return (state, action) => { | |
let reducedState = reducer(state, action); | |
if (action.type === INIT) { | |
const data = window.localStorage.getItem('productData'); | |
if (data) { | |
reducedState = { | |
...reducedState, | |
product: JSON.parse(data), | |
}; |
This file contains hidden or 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
@Effect() | |
addCartItem: Observable<Action> = this.actions$.pipe( | |
ofType<cartActions.AddItem>(cartActions.ADD_ITEM), | |
concatMap(({ itemId }) => | |
this.cartService.addToCart(itemId).pipe( | |
// Notice, that nothing is passed to the Success. | |
map(() => new cartActions.AddItemSuccess()), | |
// passing the itemId to the Error, so it can be restored. | |
catchError(() => of(new cartActions.AddItemError(itemId))) | |
) |
This file contains hidden or 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
export function reducer( | |
state: CartState = initState, | |
action: cartActions.All | |
): CartState { | |
switch (action.type) { | |
case cartActions.ADD_ITEM: { | |
// Concatinating the id to the list | |
const newCartItemsIds = [...state.cartItemsIds, action.itemId]; | |
return { | |
cartItemsIds: newCartItemsIds, |
This file contains hidden or 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
@Component({ | |
templateUrl: './add_comment_dialog.ng.html', | |
changeDetection: ChangeDetectionStrategy.OnPush, | |
}) | |
export class AddCommentDialog { | |
comment: FormControl; | |
constructor( | |
readonly dialogRef: MatDialogRef<AddCommentDialog>, | |
@Optional() @Inject(MAT_DIALOG_DATA) readonly errorPayload?: |
This file contains hidden or 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
@Effect() | |
showAddCommentDialog: Observable<Action> = this.actions.pipe( | |
ofType(actions.SHOW_ADD_COMMENT_DIALOG), | |
withLatestFrom( | |
this.store.select(selectors.getCurrentProductId)), | |
concatMap( | |
([{payload}, productId]) => | |
this.dialog.open(AddCommentDialog, {data: payload}) | |
.afterClosed() | |
.pipe( |
This file contains hidden or 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
@Effect() | |
addComment: Observable<Action> = this.actions.pipe( | |
ofType(actions.ADD_COMMENT), | |
concatMap( | |
({payload}) => | |
this.commentsService.add(payload.productId, payload.comment) | |
.pipe( | |
map(response => new actions.AddCommentSuccess()), | |
catchError( | |
error => |