Skip to content

Instantly share code, notes, and snippets.

View alex-okrushko's full-sized avatar

Alex Okrushko alex-okrushko

View GitHub Profile
/** see below - comment is required for all exported symbols */
export const LOGIN = '[Login Page] Login',
interface LoginPayload {
username: string;
password: string;
}
/** Action to log in the User from the Login Page */
export class Login implements Action {
export enum ActionTypes {
Login = '[Login Page] Login',
}
export class Login implements Action {
readonly type = ActionTypes.Login;
constructor(public payload: { username: string; password: string }) {}
}
export const isLoading = createSelector(
getResultState,
state => state.callState === CallState.LOADING,
);
export const isLoaded = createSelector(
getResultState,
state => state.callState === CallState.LOADED,
);
export interface ResultState {
result: Result|null,
callState: CallState;
}
const initState: ResultState = {
result: null,
callState: LoadingState.INIT,
}
// Dispatch is set to false, so this effect will not try to dispatch
// the result of this effect.
@Effect({ dispatch: false })
handleFetchError: Observable<unknown> = this.actions$.pipe(
ofType(actions.FETCH_PRODUCTS_ERROR),
map(() => {
// Setting the timeout, so that angular would re-run change detection.
setTimeout(
() =>
this.snackBar.open('Error fetching products', 'Error', {
@Component({
selector: 'app-movies-page',
template: `
<h1>Movies Page</h1>
<div *ngIf="error$ | async as error">
{{ error }}
</div>
<button (click)="reload()">Refresh List</button>
`
})
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,
@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 =>
@Effect()
handleAddCommentError: Observable<Action> = this.actions.pipe(
ofType(actions.ADD_COMMENT_ERROR),
map(({payload}) => new actions.ShowAddCommentDialog(payload)),
);
@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(