import { Component, OnInit } from '@angular/core' import { Router, NavigationEnd, NavigationStart, ActivatedRoute } from '@angular/router' import { Location } from '@angular/common' import { ApisService } from '../services/apis.service' @Component({ selector: 'app-detail', templateUrl: './detail.component.html', styleUrls: ['./detail.component.scss'] }) export class DetailComponent implements OnInit { public details: any = {} public section: string = '' public id: number = 0 private history: string[] = [] constructor( private apisService: ApisService, private router: Router, private location: Location, private activeRoute: ActivatedRoute ) { } ngOnInit(): void { this.section = this.router.url.split('/')[2] this.id = parseInt(this.router.url.split('/')[3]) this.showDetails(this.section, this.id) } showDetails(section, id): void { this.apisService.getDetails(section, id).toPromise().then((response) => { if(this.history[this.history.length - 1] != `/detail/${section}/${id}`) { this.history.push(`/detail/${section}/${id}`) } this.details = response.item },(error) => { console.error('getPortfolio ERROR', error) }).catch((e) => { console.error('getPortfolio CATCH', e) }) } back(): void { this.history.pop() if(this.history.length > 0) { const last = this.history[this.history.length - 1] this.section = last.split('/')[2] this.id = parseInt(last.split('/')[3]) this.showDetails(this.section, this.id) this.location.back() } else { this.location.back() } } }