Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | 1x 20x 20x 20x 20x 20x 20x 20x 20x 9x 9x 9x 9x 9x 9x 9x 9x 9x 21x 21x 20x 20x 20x 20x 12x 12x 12x 20x 9x 20x 9x 12x 12x 4x 4x 7x 12x 12x 12x 12x | import { Injectable } from '@angular/core';
import { BehaviorSubject, debounceTime, skip, Subject } from 'rxjs';
import { AppController } from 'src/app/app.controller';
import { GithubSearchRequest } from 'src/app/search/domain/entities/github-search-request';
import { GithubSearchResult } from 'src/app/search/domain/entities/github-search-result';
import { GithubSearchUsecase } from 'src/app/search/domain/usecases/github-search.usecase';
import { GithubSearchDataState } from './states/github-search-data.state';
import { GithubSearchFilterState } from './states/github-search-filter.state';
import { GithubSearchViewState } from './states/github-search-view.state';
@Injectable()
export class GithubSearchController {
public filterState$: BehaviorSubject<GithubSearchFilterState>;
public viewState$: BehaviorSubject<GithubSearchViewState>;
public dataState$: BehaviorSubject<GithubSearchDataState>;
public focusSearchInput$: Subject<void>;
loadRequest$: Subject<void>;
constructor(
private githubSearchUsecase: GithubSearchUsecase,
private appController: AppController
) {
this.filterState$ = new BehaviorSubject<GithubSearchFilterState>(
GithubSearchFilterState.default()
);
this.viewState$ = new BehaviorSubject<GithubSearchViewState>(
GithubSearchViewState.default()
);
this.dataState$ = new BehaviorSubject<GithubSearchDataState>(
GithubSearchDataState.empty()
);
this.focusSearchInput$ = new Subject();
this.loadRequest$ = new Subject<void>();
this.listenStates();
}
public setSearchTerm(searchTerm: string) {
this.updateFilterState({ searchTerm });
}
public setDefaultPage() {
const defaultViewState = GithubSearchViewState.default();
this.setPage(defaultViewState.page);
}
public setPage(page: number) {
this.updateViewState({ page });
}
private updateFilterState(attributes: GithubSearchFilterState | {}) {
const filterState = this.filterState$.value;
this.filterState$.next({
...filterState,
...attributes,
});
this.setDefaultPage();
}
private updateViewState(attributes: GithubSearchViewState | {}) {
const viewState = this.viewState$.value;
this.viewState$.next({
...viewState,
...attributes,
});
}
public load() {
this.loadRequest$.next();
this.appController.scrollToTop$.next();
}
private listenStates() {
this.listenLoadRequest();
this.listenFilterState();
this.listenViewState();
}
private listenLoadRequest() {
this.loadRequest$.pipe(debounceTime(50)).subscribe(() => {
this.setLoading();
const request = this.buildRequest();
this.githubSearchUsecase.call(request).subscribe({
next: this.setData.bind(this),
error: this.setError.bind(this),
});
});
}
private listenFilterState() {
this.filterState$.pipe(skip(1)).subscribe(() => {
this.load();
});
}
private listenViewState() {
this.viewState$.pipe(skip(1)).subscribe(() => {
this.load();
});
}
private setLoading() {
const data = this.dataState$.value.data;
this.dataState$.next(GithubSearchDataState.loading(data));
}
private setError() {
const data = this.dataState$.value.data;
this.dataState$.next(GithubSearchDataState.error(data));
}
private setData(data: GithubSearchResult) {
this.dataState$.next(GithubSearchDataState.success(data));
}
private buildRequest(): GithubSearchRequest {
const filterState = this.filterState$.value;
const viewState = this.viewState$.value;
const perPage = 9;
return new GithubSearchRequest(
filterState.searchTerm,
viewState.page,
perPage
);
}
}
|