opt-cc/static/src/app/request/promotion/req-promotion.component.ts

101 lines
2.9 KiB
TypeScript
Raw Normal View History

2018-03-08 09:44:35 +01:00
import {Component, OnInit, ViewChild} from '@angular/core';
2018-03-07 11:56:50 +01:00
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';
2017-06-10 13:16:15 +02:00
@Component({
templateUrl: './req-promotion.component.html',
styleUrls: ['./req-promotion.component.scss'],
2017-06-10 13:16:15 +02:00
})
2018-03-08 09:44:35 +01:00
export class RequestPromotionComponent implements OnInit {
2017-06-10 13:16:15 +02:00
@ViewChild(NgForm) form: NgForm;
showForm = false;
2017-08-02 20:20:05 +02:00
user: User = {_id: '0'};
2017-06-10 13:16:15 +02:00
newLevel: number;
ranks: Rank[];
users: User[];
uncheckedPromotions = [];
2017-10-14 17:47:55 +02:00
selectedUserRank;
2017-06-10 13:16:15 +02:00
constructor(private router: Router,
private route: ActivatedRoute,
private userService: UserService,
private rankService: RankService,
2017-09-23 11:53:10 +02:00
private promotionService: PromotionService,
private loginService: LoginService,
private snackBarService: SnackBarService) {
2017-06-10 13:16:15 +02:00
}
ngOnInit() {
2018-03-07 11:56:50 +01:00
const currentUser = this.loginService.getCurrentUser();
2017-06-10 13:16:15 +02:00
// show only current users squad members
this.userService.findUsers({squadId: currentUser.squad._id}).subscribe(users => {
2017-06-10 13:16:15 +02:00
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;
2018-03-07 11:56:50 +01:00
});
2017-06-10 13:16:15 +02:00
}
toggleUser() {
2018-03-07 11:56:50 +01:00
if (this.user._id !== '0') {
2017-08-02 20:20:05 +02:00
this.showForm = true;
2017-10-14 17:47:55 +02:00
this.newLevel = this.user.rankLvl;
this.selectedUserRank = this.ranks.filter(rank => rank.level === this.user.rankLvl).map(rank => rank.name);
2017-08-02 20:20:05 +02:00
} else {
this.showForm = false;
}
2017-06-10 13:16:15 +02:00
}
addPromotion() {
const promotion = {
2018-03-07 11:56:50 +01:00
'userId': this.user._id,
'oldRankLvl': this.user.rankLvl,
'newRankLvl': this.newLevel
2017-06-10 13:16:15 +02:00
};
2017-07-22 23:36:36 +02:00
this.promotionService.requestPromotion(promotion).subscribe(() => {
2018-03-07 11:56:50 +01:00
const currentUser = this.loginService.getCurrentUser();
2017-07-22 23:36:36 +02:00
this.promotionService.getSquadPromotions(currentUser.squad._id).subscribe(promotions => {
this.uncheckedPromotions = promotions;
2017-10-14 17:47:55 +02:00
this.showForm = false;
this.user = {_id: '0'};
2018-10-05 15:51:46 +02:00
this.snackBarService.showSuccess('generic.save.success');
2018-03-07 11:56:50 +01:00
});
2017-07-22 23:36:36 +02:00
});
2017-06-10 13:16:15 +02:00
}
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;
}
}
}