2018-03-07 11:56:50 +01:00
|
|
|
import {Component, ViewChild} from '@angular/core';
|
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
import {Rank, Squad, User} from '../../models/model-interfaces';
|
|
|
|
import {UserService} from '../../services/army-management/user.service';
|
|
|
|
import {SquadService} from '../../services/army-management/squad.service';
|
|
|
|
import {RankService} from '../../services/army-management/rank.service';
|
|
|
|
import {Subscription} from 'rxjs';
|
|
|
|
import {NgForm} from '@angular/forms';
|
|
|
|
import {Fraction} from '../../utils/fraction.enum';
|
2017-05-10 11:04:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
2017-05-17 15:55:22 +02:00
|
|
|
templateUrl: './edit-user.component.html',
|
2017-10-07 19:32:16 +02:00
|
|
|
styleUrls: ['./edit-user.component.css', '../../style/entry-form.css', '../../style/overview.css'],
|
2017-05-10 11:04:06 +02:00
|
|
|
})
|
2017-05-17 15:55:22 +02:00
|
|
|
export class EditUserComponent {
|
2017-05-10 11:04:06 +02:00
|
|
|
|
2017-05-13 14:57:40 +02:00
|
|
|
@ViewChild(NgForm) form: NgForm;
|
|
|
|
|
|
|
|
subscription: Subscription;
|
|
|
|
|
2017-10-14 15:26:05 +02:00
|
|
|
user: User = {username: '', squadId: '0', rankLvl: 0};
|
2017-05-10 11:04:06 +02:00
|
|
|
|
2017-05-13 14:57:40 +02:00
|
|
|
squads: Squad[] = [];
|
2017-05-10 11:04:06 +02:00
|
|
|
|
2017-05-13 14:57:40 +02:00
|
|
|
ranks: Rank[] = [];
|
2017-05-10 11:04:06 +02:00
|
|
|
|
2017-05-17 15:55:22 +02:00
|
|
|
showSuccessLabel = false;
|
|
|
|
|
|
|
|
ranksDisplay = 'none';
|
2017-05-10 11:04:06 +02:00
|
|
|
|
2017-09-15 21:02:43 +02:00
|
|
|
showErrorLabel = false;
|
|
|
|
|
|
|
|
error: string;
|
|
|
|
|
2017-11-08 19:40:51 +01:00
|
|
|
readonly fraction = Fraction;
|
|
|
|
|
2017-05-10 11:04:06 +02:00
|
|
|
constructor(private router: Router,
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private userService: UserService,
|
|
|
|
private squadService: SquadService,
|
2017-05-13 14:57:40 +02:00
|
|
|
private rankService: RankService) {
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2017-05-13 14:57:40 +02:00
|
|
|
|
|
|
|
this.subscription = this.route.params
|
2018-02-26 09:04:27 +01:00
|
|
|
.map(params => params['id'])
|
2018-03-07 11:56:50 +01:00
|
|
|
.filter(id => id !== undefined)
|
2018-02-26 09:04:27 +01:00
|
|
|
.flatMap(id => this.userService.getUser(id))
|
|
|
|
.subscribe(user => {
|
|
|
|
if (!user.squadId) {
|
2018-03-07 11:56:50 +01:00
|
|
|
user.squadId = '0';
|
2018-02-26 09:04:27 +01:00
|
|
|
this.ranksDisplay = 'none';
|
|
|
|
} else {
|
|
|
|
this.rankService.findRanks('', user.squadId.fraction).subscribe(ranks => {
|
|
|
|
this.ranks = ranks;
|
|
|
|
this.ranksDisplay = 'block';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.user = user;
|
|
|
|
});
|
2017-05-10 11:04:06 +02:00
|
|
|
|
|
|
|
this.squadService.findSquads().subscribe(squads => {
|
|
|
|
this.squads = squads;
|
|
|
|
});
|
2017-05-13 14:57:40 +02:00
|
|
|
}
|
2017-05-10 11:04:06 +02:00
|
|
|
|
2017-05-13 14:57:40 +02:00
|
|
|
toggleRanks() {
|
2018-03-07 11:56:50 +01:00
|
|
|
if (this.user.squadId !== '0') {
|
2017-10-14 15:26:05 +02:00
|
|
|
this.rankService.findRanks('', this.user.squadId.fraction).subscribe(
|
2017-05-13 14:57:40 +02:00
|
|
|
ranks => {
|
|
|
|
this.ranks = ranks;
|
|
|
|
this.ranksDisplay = 'block';
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.ranksDisplay = 'none';
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 11:04:06 +02:00
|
|
|
|
2017-05-13 14:57:40 +02:00
|
|
|
saveUser(rankLevel) {
|
|
|
|
const updateObject = {
|
|
|
|
_id: this.user._id,
|
|
|
|
username: this.user.username,
|
|
|
|
rankLvl: rankLevel,
|
|
|
|
squadId: null
|
|
|
|
};
|
2017-10-14 15:26:05 +02:00
|
|
|
if (this.user.squadId._id !== '0') {
|
2018-03-07 11:56:50 +01:00
|
|
|
updateObject.squadId = this.user.squadId._id;
|
2017-05-13 14:57:40 +02:00
|
|
|
}
|
2017-05-14 16:35:44 +02:00
|
|
|
|
|
|
|
if (this.user._id) {
|
|
|
|
this.userService.updateUser(updateObject)
|
2018-02-26 09:04:27 +01:00
|
|
|
.subscribe(user => {
|
|
|
|
if (!user.squad) {
|
|
|
|
user.squad = '0';
|
2017-09-15 21:02:43 +02:00
|
|
|
}
|
2018-02-26 09:04:27 +01:00
|
|
|
this.user = user;
|
|
|
|
this.showSuccessLabel = true;
|
|
|
|
setTimeout(() => {
|
|
|
|
this.showSuccessLabel = false;
|
2018-03-07 11:56:50 +01:00
|
|
|
}, 2000);
|
|
|
|
});
|
2018-02-26 09:04:27 +01:00
|
|
|
} else {
|
|
|
|
this.userService.submitUser(updateObject)
|
|
|
|
.subscribe(user => {
|
|
|
|
this.router.navigate(['..'], {relativeTo: this.route});
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
// duplicated user error message
|
|
|
|
if (error._body.includes('duplicate')) {
|
2018-03-07 11:56:50 +01:00
|
|
|
this.error = 'Benutzername existiert bereits';
|
2018-02-26 09:04:27 +01:00
|
|
|
this.showErrorLabel = true;
|
|
|
|
setTimeout(() => {
|
|
|
|
this.showErrorLabel = false;
|
|
|
|
}, 5000);
|
|
|
|
}
|
2018-03-07 11:56:50 +01:00
|
|
|
});
|
2017-05-14 16:35:44 +02:00
|
|
|
}
|
2017-05-13 14:57:40 +02:00
|
|
|
}
|
2017-05-10 11:04:06 +02:00
|
|
|
|
2017-05-13 14:57:40 +02:00
|
|
|
cancel() {
|
2017-05-14 16:35:44 +02:00
|
|
|
this.router.navigate([this.user._id ? '../..' : '..'], {relativeTo: this.route});
|
2017-05-13 14:57:40 +02:00
|
|
|
return false;
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|
|
|
|
|
2017-05-13 14:57:40 +02:00
|
|
|
/**
|
|
|
|
* compare ngValue with ngModel to assign selected element
|
|
|
|
*/
|
|
|
|
equals(o1: Squad, o2: Squad) {
|
|
|
|
if (o1 && o2) {
|
|
|
|
return o1._id === o2._id;
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|