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
/* | |
'(', '{', '[' are called "openers". | |
')', '}', ']' are called "closers". | |
Write an efficient function that tells us whether input string's openers | |
and closers are properly nested. | |
Examples: | |
"{ [ ] ( ) }" -> true | |
"{ [ ( ] ) }" -> 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
<loading-container [loadable]="news$ | async"> | |
<p *ngFor="let item of (news$ | async).entities">{{item}}</p> | |
</loading-container> |
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
@Component({ | |
selector: 'loading-container', | |
template: ` | |
<div *ngIf="loadable.loading">This is loading spinner...</div> | |
<div *ngIf="loadable.error">{{loadable?.error?.message || 'Something went wrong'}}</div> | |
<ng-container *ngIf="loadable.success"> | |
<ng-content></ng-content> | |
</ng-container> | |
` | |
}) |
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
// базовый редуктор должен обновлять только не loadable состояния | |
function baseNewsReducer(state: News = createDefaultNews(), action: NewsActions): News { | |
switch (action.type) { | |
case NewsActionsTypes.LoadSuccess: | |
return { | |
...state, | |
entities: action.payload.entities | |
}; | |
default: | |
return state; |
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
export function withLoadable(baseReducer, {loadingActionType, successActionType, errorActionType}) { | |
return (state, action) => { | |
if (action.type === loadingActionType) { | |
state = onLoadableLoad(state); | |
} | |
if (action.type === successActionType) { | |
state = onLoadableSuccess(state); | |
} | |
if (action.type === errorActionType) { | |
state = onLoadableError(state, action.error); |
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
@Component({ | |
selector: 'app-news', | |
template: ` | |
<button (click)="load()">Load News</button> | |
<!--loading spinner--> | |
<p *ngIf="(news$ | async).loading">loading...</p> | |
<p *ngFor="let item of (news$ | async).entities">{{item}}</p> | |
` | |
}) |
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
@Effect() | |
loadNews = this.actions$.pipe( | |
ofType(NewsActionsTypes.Load), | |
switchMap(action => { | |
return this.http.get('some url').pipe( | |
map((response: any) => new LoadNewsSuccess({entities: response.todaysNews})), | |
catchError(error => of(new LoadNewsError(error))) | |
); | |
}), | |
); |
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
export function newsReducer(state: News = createDefaultNews(), action: NewsActions): News { | |
switch (action.type) { | |
case NewsActionsTypes.Load: | |
return onLoadableLoad(state); | |
case NewsActionsTypes.LoadSuccess: | |
return { | |
...onLoadableSuccess(state), | |
entities: action.payload.entities | |
}; | |
case NewsActionsTypes.LoadError: |
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
export enum NewsActionsTypes { | |
Load = '[NEWS PAGE] LOAD NEWS', | |
LoadSuccess = '[NEWS PAGE] LOAD NEWS SUCCESS', | |
LoadError = '[NEWS PAGE] LOAD NEWS ERROR', | |
} | |
export class LoadNews implements Action { | |
readonly type = NewsActionsTypes.Load; | |
} | |
export class LoadNewsSuccess implements Action { | |
readonly type = NewsActionsTypes.LoadSuccess; |
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
export interface News extends Loadable { | |
news: string[]; | |
} | |
export function createDefaultNews(): News { | |
return { | |
...createDefaultLoadable(), | |
entities: [] | |
}; | |
} |