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 const isLoading = createSelector( | |
getResultState, | |
state => state.callState === CallState.LOADING, | |
); | |
export const isLoaded = createSelector( | |
getResultState, | |
state => state.callState === CallState.LOADED, | |
); |
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 interface ResultState { | |
result: Result|null, | |
callState: CallState; | |
} | |
const initState: ResultState = { | |
result: null, | |
callState: LoadingState.INIT, | |
} |
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
// 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', { |
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 const enum LoadingState { | |
INIT = 'INIT', | |
LOADING = 'LOADING', | |
LOADED = 'LOADED', | |
} | |
export interface ErrorState { | |
errorMsg: string; | |
} | |
export type CallState = LoadingState | ErrorState; |
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({ | |
selector: 'app-movies-page', | |
template: ` | |
<h1>Movies Page</h1> | |
<div *ngIf="error$ | async as error"> | |
{{ error }} | |
</div> | |
<button (click)="reload()">Refresh List</button> | |
` | |
}) |
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() | |
handleAddCommentError: Observable<Action> = this.actions.pipe( | |
ofType(actions.ADD_COMMENT_ERROR), | |
map(({payload}) => new actions.ShowAddCommentDialog(payload)), | |
); |
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 => |
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
@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
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, |