|
| 1 | +import {Component} from "@angular/core"; |
| 2 | +import {DocService} from "../../services"; |
| 3 | +import {ActivatedRoute, Router} from "@angular/router"; |
| 4 | +import {Observable} from "rxjs"; |
| 5 | +import {tap} from "rxjs/operators"; |
| 6 | +import {DocumentationVersion, Post} from "../../models"; |
| 7 | + |
| 8 | +@Component({ |
| 9 | + selector: "app-layout", |
| 10 | + templateUrl: "./layout.component.html", |
| 11 | + styleUrls: ["./layout.component.sass"] |
| 12 | +}) |
| 13 | +export class LayoutComponent { |
| 14 | + |
| 15 | + private version$: Observable<DocumentationVersion>; |
| 16 | + private post$: Observable<Post>; |
| 17 | + |
| 18 | + constructor (private route: ActivatedRoute, private router: Router, private docService: DocService) { |
| 19 | + |
| 20 | + |
| 21 | + this.route.paramMap.pipe( |
| 22 | + tap(params => { |
| 23 | + |
| 24 | + const versionId = params.get("versionId"); |
| 25 | + const postId = params.get("postId"); |
| 26 | + |
| 27 | + if (!versionId || !postId) { |
| 28 | + this.docService.GetLatestVersion().pipe( |
| 29 | + tap(version => |
| 30 | + this.router.navigate([`/docs/${version.version}/${version.groups[0].posts[0].id}`]) |
| 31 | + ) |
| 32 | + ).subscribe(); |
| 33 | + } else { |
| 34 | + |
| 35 | + if (!this.version$) { |
| 36 | + this.version$ = this.docService |
| 37 | + .GetVersion(versionId).pipe( |
| 38 | + tap(version => { |
| 39 | + if (!version) { |
| 40 | + this.router.navigate([`/docs`]); |
| 41 | + } |
| 42 | + }) |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + this.post$ = this.docService.GetPost(versionId, postId).pipe( |
| 47 | + tap(post => { |
| 48 | + if (!post) { |
| 49 | + this.router.navigate([`/docs`]); |
| 50 | + } |
| 51 | + }) |
| 52 | + ); |
| 53 | + |
| 54 | + } |
| 55 | + }) |
| 56 | + ).subscribe(); |
| 57 | + } |
| 58 | +} |
0 commit comments