Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
mtwmt committed Oct 3, 2023
1 parent a46f8fe commit ff7c52d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 141 deletions.
137 changes: 34 additions & 103 deletions src/app/ironman/ironman-store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
switchMap,
tap,
} from 'rxjs';
import { IronmanListInfo, NthKey } from './ironman.model';
import { IronmanListInfo } from './ironman.model';
import { IronmanService } from './ironman.service';

@Injectable({
Expand All @@ -19,124 +19,55 @@ import { IronmanService } from './ironman.service';
export class IronmanStoreService {
public isLoading$: BehaviorSubject<boolean> = new BehaviorSubject(true);

public ironman15th$: Observable<IronmanListInfo[]>;
public ironman14th$: Observable<IronmanListInfo[]>;
public ironman13th$: Observable<IronmanListInfo[]>;
public ironman12th$: Observable<IronmanListInfo[]>;
public ironman11th$: Observable<IronmanListInfo[]>;
public ironman10th$: Observable<IronmanListInfo[]>;
public ironman9th$: Observable<IronmanListInfo[]>;
public ironman8th$: Observable<IronmanListInfo[]>;
public ironman7th$: Observable<IronmanListInfo[]>;
public ironmanHistory$: Observable<IronmanListInfo[]>;
private ironmanObservables: { [key: string]: Observable<IronmanListInfo[]> } =
{};

public getIronmanList$: BehaviorSubject<IronmanListInfo[]> =
new BehaviorSubject([] as IronmanListInfo[]);

private ironmanTh = [
'15th',
'14th',
'13th',
'12th',
'11th',
'10th',
'9th',
'8th',
'7th',
'',
];

constructor(private ironmanService: IronmanService) {
this.ironman15th$ = this.ironmanService
.fetch15thIronman()
.pipe(shareReplay(1));
this.ironman14th$ = this.ironmanService
.fetch14thIronman()
.pipe(shareReplay(1));
this.ironman13th$ = this.ironmanService
.fetch13thIronman()
.pipe(shareReplay(1));
this.ironman12th$ = this.ironmanService
.fetch12thIronman()
.pipe(shareReplay(1));
this.ironman11th$ = this.ironmanService
.fetch11thIronman()
.pipe(shareReplay(1));
this.ironman10th$ = this.ironmanService
.fetch10thIronman()
.pipe(shareReplay(1));
this.ironman9th$ = this.ironmanService
.fetch9thIronman()
.pipe(shareReplay(1));
this.ironman8th$ = this.ironmanService
.fetch8thIronman()
.pipe(shareReplay(1));
this.ironman7th$ = this.ironmanService
.fetch7thIronman()
.pipe(shareReplay(1));
this.ironmanHistory$ = this.ironmanService
.fetchHistoryIronman()
.pipe(shareReplay(1));
this.ironmanTh.forEach((th) => {
this.ironmanObservables[th] = this.ironmanService
.fetchIronman(th)
.pipe(shareReplay(1));
});
}

filterNthObservable(th: string = '') {
return combineLatest([
this.ironman15th$,
this.ironman14th$,
this.ironman13th$,
this.ironman12th$,
this.ironman11th$,
this.ironman10th$,
this.ironman9th$,
this.ironman8th$,
this.ironman7th$,
this.ironmanHistory$,
]).pipe(
tap(() => this.isLoading$.next(true)),
map(([th15, th14, th13, th12, th11, th10, th9, th8, th7, history]) => {
let list: IronmanListInfo[] = [];
switch (th) {
case NthKey.Th15:
list = th15;
break;
case NthKey.Th14:
list = th14;
break;
case NthKey.Th13:
list = th13;
break;
case NthKey.Th12:
list = th12;
break;
case NthKey.Th11:
list = th11;
break;
case NthKey.Th10:
list = th10;
break;
case NthKey.Th9:
list = th9;
break;
case NthKey.Th8:
list = th8;
break;
case NthKey.Th7:
list = th7;
break;
case NthKey.History:
list = history;
break;
default:
list = [
...th15,
...th14,
...th13,
...th12,
...th11,
...th10,
...th9,
...th8,
...th7,
...history,
];
break;
return combineLatest(Object.values(this.ironmanObservables)).pipe(
tap(() => {
this.isLoading$.next(true);
}),
map((list) => {
if (th in this.ironmanObservables) {
const keys = Object.keys(this.ironmanObservables);
const idx = keys.indexOf(th);

return list[idx];
} else {
return list.reduce((acc, list) => acc.concat(list), []);
}
return list;
})
);
}

filterCategory(th: string = '', category: string = '') {
return this.filterNthObservable(th).pipe(
tap(() => this.isLoading$.next(true)),
map((list) => {
map((list: any) => {
return list.filter((d: IronmanListInfo) => {
return d.category
.toLocaleLowerCase()
Expand Down
43 changes: 5 additions & 38 deletions src/app/ironman/ironman.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,14 @@ import { IronmanListInfo } from './ironman.model';
})
export class IronmanService {
readonly baseUrl = `${environment.baseUrl}/assets`;



constructor(private httpClient: HttpClient) {}

fetchHistoryIronman() {
const url = `${this.baseUrl}/ironman.json`;
fetchIronman(year: string) {
const url = `${this.baseUrl}/ironman${year}.json`;
return this.httpClient.get<IronmanListInfo[]>(url);
}

fetch7thIronman() {
const url = `${this.baseUrl}/ironman7th.json`;
return this.httpClient.get<IronmanListInfo[]>(url);
}
fetch8thIronman() {
const url = `${this.baseUrl}/ironman8th.json`;
return this.httpClient.get<IronmanListInfo[]>(url);
}
fetch9thIronman() {
const url = `${this.baseUrl}/ironman9th.json`;
return this.httpClient.get<IronmanListInfo[]>(url);
}
fetch10thIronman() {
const url = `${this.baseUrl}/ironman10th.json`;
return this.httpClient.get<IronmanListInfo[]>(url);
}
fetch11thIronman() {
const url = `${this.baseUrl}/ironman11th.json`;
return this.httpClient.get<IronmanListInfo[]>(url);
}
fetch12thIronman() {
const url = `${this.baseUrl}/ironman12th.json`;
return this.httpClient.get<IronmanListInfo[]>(url);
}
fetch13thIronman() {
const url = `${this.baseUrl}/ironman13th.json`;
return this.httpClient.get<IronmanListInfo[]>(url);
}
fetch14thIronman() {
const url = `${this.baseUrl}/ironman14th.json`;
return this.httpClient.get<IronmanListInfo[]>(url);
}
fetch15thIronman() {
const url = `${this.baseUrl}/ironman15th.json`;
return this.httpClient.get<IronmanListInfo[]>(url);
}
}

0 comments on commit ff7c52d

Please sign in to comment.