travopti/frontend/src/app/containers/bookmark-list/bookmark-list.component.ts
Patrick Gebhardt 9c3f70d36b Add bookmarks
2020-06-19 17:50:38 +02:00

47 lines
1.3 KiB
TypeScript

import {Component, OnDestroy, OnInit} from '@angular/core';
import {Region} from '../../interfaces/region.interface';
import {BookmarkService} from '../../services/bookmark.service';
import {DataService} from '../../services/data.service';
import {switchMap, takeUntil, tap} from 'rxjs/operators';
import {Router} from '@angular/router';
import {Subject} from 'rxjs';
@Component({
selector: 'app-bookmark-list',
templateUrl: './bookmark-list.component.html',
styleUrls: ['./bookmark-list.component.scss']
})
export class BookmarkListComponent implements OnInit, OnDestroy {
bookmarks: Region[];
isLoading = true;
private destroyed$ = new Subject<void>();
constructor(private bs: BookmarkService, private ds: DataService, private router: Router) {
}
ngOnInit() {
this.bs.getRegionIds().pipe(
takeUntil(this.destroyed$),
tap(() => this.isLoading = true),
switchMap(ids => {
const regions: Promise<Region>[] = ids.map(id => this.ds.getRegion(id));
return Promise.all(regions);
})
).subscribe(regions => {
this.bookmarks = regions;
this.isLoading = false;
});
}
onBookmarkClick(region: Region) {
this.router.navigate(['/region', region.region_id]).catch(console.log);
}
ngOnDestroy(): void {
this.destroyed$.next();
this.destroyed$.complete();
}
}