import {Component, 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/user-service/user.service"; import {RankService} from "../../services/rank-service/rank.service"; import {PromotionService} from "../../services/promotion-service/promotion.service"; @Component({ templateUrl: './req-promotion.component.html', styleUrls: ['./req-promotion.component.css'], }) export class RequestPromotionComponent { @ViewChild(NgForm) form: NgForm; showForm = false; showSuccessLabel = false; user: User = {_id: '0'}; newLevel: number; ranks: Rank[]; users: User[]; uncheckedPromotions = []; constructor(private router: Router, private route: ActivatedRoute, private userService: UserService, private rankService: RankService, private promotionService: PromotionService) { } ngOnInit() { let currentUser = JSON.parse(localStorage.getItem('currentUser')); // show only current users squad members this.userService.findUsers('', undefined, 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.rank.level; } else { this.showForm = false; } } addPromotion() { const promotion = { "userId": this.user._id, "oldRankLvl": this.user.rank.level, "newRankLvl": this.newLevel }; this.promotionService.requestPromotion(promotion).subscribe(() => { this.showSuccessLabel = true; setTimeout(() => { this.showSuccessLabel = false; }, 2000); this.showForm = false; this.user = {}; let currentUser = JSON.parse(localStorage.getItem('currentUser')); this.promotionService.getSquadPromotions(currentUser.squad._id).subscribe(promotions => { this.uncheckedPromotions = promotions; }) }); } 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; } } }