import {Component, OnInit} from '@angular/core'; import {FormControl} from '@angular/forms'; import {ActivatedRoute, Router} from '@angular/router'; import {Observable} from 'rxjs/Observable'; import {Rank} from '../../../models/model-interfaces'; import {RankService} from '../../../services/army-management/rank.service'; import {Fraction} from '../../../utils/fraction.enum'; import {UIHelpers} from '../../../utils/global.helpers'; import {MatButtonToggleGroup} from '@angular/material'; import {TranslateService} from '@ngx-translate/core'; @Component({ selector: 'cc-rank-list', templateUrl: './rank-list.component.html', styleUrls: ['./rank-list.component.scss'] }) export class RankListComponent implements OnInit { selectedRankId: string | number = null; ranks$: Observable; searchTerm = new FormControl(); radioModel = ''; readonly fraction = Fraction; constructor(private rankService: RankService, private router: Router, private route: ActivatedRoute, private translate: TranslateService) { } ngOnInit() { this.ranks$ = this.rankService.ranks$; } initObservable(observables: any) { Observable.merge(observables.params as Observable, observables.searchTerm) .distinctUntilChanged() .switchMap(query => this.rankService.findRanks(query, this.radioModel)) .subscribe(); } openNewRankForm() { this.selectedRankId = null; this.router.navigate([{outlets: {'right': ['new']}}], {relativeTo: this.route}); } selectRank(rankId: string | number) { this.selectedRankId = rankId; this.router.navigate([{outlets: {'right': ['edit', rankId]}}], {relativeTo: this.route}); } filterRanks(group?: MatButtonToggleGroup) { this.radioModel = UIHelpers.toggleReleaseButton(this.radioModel, group); this.ranks$ = this.rankService.findRanks(this.searchTerm.value, this.radioModel); } deleteRank(rank) { const fraction = rank.fraction === 'OPFOR' ? Fraction.OPFOR : Fraction.BLUFOR; this.translate.get('ranks.list.delete.confirm', { name: rank.name, fraction: fraction }).subscribe((confirmQuestion) => { if (confirm(confirmQuestion)) { this.rankService.deleteRank(rank) .subscribe((res) => { }); } }); } }