Skip to content

Instantly share code, notes, and snippets.

View vvscode's full-sized avatar
⌨️
Here is Rhodes, jump here!

Vasiliy Vanchuk vvscode

⌨️
Here is Rhodes, jump here!
View GitHub Profile
/*
'(', '{', '[' 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
@vvscode
vvscode / loading-container-usage.ts
Created February 8, 2019 10:39
handle-api-call-state-nicely
<loading-container [loadable]="news$ | async">
<p *ngFor="let item of (news$ | async).entities">{{item}}</p>
</loading-container>
@vvscode
vvscode / loadable-container.ts
Created February 8, 2019 10:39
handle-api-call-state-nicely
@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>
`
})
@vvscode
vvscode / base-reducer.ts
Created February 8, 2019 10:38
handle-api-call-state-nicely
// базовый редуктор должен обновлять только не loadable состояния
function baseNewsReducer(state: News = createDefaultNews(), action: NewsActions): News {
switch (action.type) {
case NewsActionsTypes.LoadSuccess:
return {
...state,
entities: action.payload.entities
};
default:
return state;
@vvscode
vvscode / withLoadable.ts
Created February 8, 2019 10:37
handle-api-call-state-nicely
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);
@vvscode
vvscode / component.ts
Created February 8, 2019 10:37
handle-api-call-state-nicely
@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>
`
})
@vvscode
vvscode / effects.ts
Created February 8, 2019 10:36
handle-api-call-state-nicely
@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)))
);
}),
);
@vvscode
vvscode / reducer.ts
Created February 8, 2019 10:36
handle-api-call-state-nicely
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:
@vvscode
vvscode / actions.ts
Created February 8, 2019 10:35
handle-api-call-state-nicely
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;
@vvscode
vvscode / createDefaultNews.ts
Created February 8, 2019 10:34
handle-api-call-state-nicely
export interface News extends Loadable {
news: string[];
}
export function createDefaultNews(): News {
return {
...createDefaultLoadable(),
entities: []
};
}