Skip to content

Instantly share code, notes, and snippets.

View alex-okrushko's full-sized avatar

Alex Okrushko alex-okrushko

View GitHub Profile
@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()))
@Component({...})
export class ProductDetailsComponent {
product$ = this.store.select(selectors.getCurrentProduct);
constructor(
private readonly store: Store<{}>,
) {
this.store.dispatch(new actions.FetchCurrentProduct());
}
}
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),
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),
};
@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)))
)
@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?:
@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(
@Effect()
handleAddCommentError: Observable<Action> = this.actions.pipe(
ofType(actions.ADD_COMMENT_ERROR),
map(({payload}) => new actions.ShowAddCommentDialog(payload)),
);
@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 =>
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,