import {Component, OnInit, ViewChild} from '@angular/core'; import {ActivatedRoute, Router} from '@angular/router'; import {Rank, User} from '../../models/model-interfaces'; import {NgForm} from '@angular/forms'; import {UserService} from '../../services/army-management/user.service'; import {RankService} from '../../services/army-management/rank.service'; import {PromotionService} from '../../services/army-management/promotion.service'; import {LoginService} from '../../services/app-user-service/login-service'; import {SnackBarService} from '../../services/user-interface/snack-bar/snack-bar.service'; @Component({ templateUrl: './req-promotion.component.html', styleUrls: ['./req-promotion.component.scss'], }) export class RequestPromotionComponent implements OnInit { @ViewChild(NgForm) form: NgForm; showForm = false; user: User = {_id: '0'}; newLevel: number; ranks: Rank[]; users: User[]; uncheckedPromotions = []; selectedUserRank; constructor(private router: Router, private route: ActivatedRoute, private userService: UserService, private rankService: RankService, private promotionService: PromotionService, private loginService: LoginService, private snackBarService: SnackBarService) { } ngOnInit() { const currentUser = this.loginService.getCurrentUser(); // show only current users squad members this.userService.findUsers({squadId: currentUser.squad._id}).subscribe(users => { this.users = users; }); this.rankService.findRanks('', currentUser.squad.fraction).subscribe(ranks => { this.ranks = ranks; }); this.promotionService.getSquadPromotions(currentUser.squad._id).subscribe(promotions => { this.uncheckedPromotions = promotions; }); } toggleUser() { if (this.user._id !== '0') { this.showForm = true; this.newLevel = this.user.rankLvl; this.selectedUserRank = this.ranks.filter(rank => rank.level === this.user.rankLvl).map(rank => rank.name); } else { this.showForm = false; } } addPromotion() { const promotion = { 'userId': this.user._id, 'oldRankLvl': this.user.rankLvl, 'newRankLvl': this.newLevel }; this.promotionService.requestPromotion(promotion).subscribe(() => { const currentUser = this.loginService.getCurrentUser(); this.promotionService.getSquadPromotions(currentUser.squad._id).subscribe(promotions => { this.uncheckedPromotions = promotions; this.showForm = false; this.user = {_id: '0'}; this.snackBarService.showSuccess('generic.save.success'); }); }); } cancel() { this.router.navigate(['..'], {relativeTo: this.route}); return false; } /** * compare ngValue with ngModel to assign selected element */ equals(o1: User, o2: User) { if (o1 && o2) { return o1._id === o2._id; } } }