2017-05-13 14:57:40 +02:00
|
|
|
import {Component, ViewChild} from "@angular/core";
|
2017-05-10 11:04:06 +02:00
|
|
|
import {ActivatedRoute, Router} from "@angular/router";
|
2017-05-13 14:57:40 +02:00
|
|
|
import {Rank, Squad, User} from "../../models/model-interfaces";
|
2017-05-10 11:04:06 +02:00
|
|
|
import {UserService} from "../../services/user-service/user.service";
|
|
|
|
import {SquadService} from "../../services/squad-service/squad.service";
|
2017-05-13 14:57:40 +02:00
|
|
|
import {RankService} from "../../services/rank-service/rank.service";
|
|
|
|
import {Subscription} from "rxjs";
|
|
|
|
import {NgForm} from "@angular/forms";
|
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-05-14 16:35:44 +02:00
|
|
|
user: User = {username: '', squad: '0', rank: {level: 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-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
|
|
|
|
.map(params => params['id'])
|
|
|
|
.filter(id => id != undefined)
|
|
|
|
.flatMap(id => this.userService.getUser(id))
|
|
|
|
.subscribe(user => {
|
|
|
|
if (!user.squad) {
|
|
|
|
user.squad = "0";
|
|
|
|
this.ranksDisplay = 'none';
|
|
|
|
} else {
|
|
|
|
this.rankService.findRanks('', user.squad.fraction).subscribe(ranks => {
|
|
|
|
this.ranks = ranks;
|
|
|
|
this.ranksDisplay = 'block';
|
|
|
|
});
|
|
|
|
}
|
2017-05-10 11:04:06 +02:00
|
|
|
this.user = user;
|
|
|
|
});
|
|
|
|
|
|
|
|
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() {
|
|
|
|
if (this.user.squad != '0') {
|
|
|
|
this.rankService.findRanks('', this.user.squad.fraction).subscribe(
|
|
|
|
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
|
|
|
|
};
|
|
|
|
if (this.user.squad._id !== '0') {
|
|
|
|
updateObject.squadId = this.user.squad._id
|
|
|
|
}
|
2017-05-14 16:35:44 +02:00
|
|
|
|
|
|
|
if (this.user._id) {
|
|
|
|
this.userService.updateUser(updateObject)
|
|
|
|
.subscribe(user => {
|
|
|
|
if (!user.squad) {
|
|
|
|
user.squad = '0';
|
|
|
|
}
|
|
|
|
this.user = user;
|
|
|
|
this.showSuccessLabel = true;
|
|
|
|
setTimeout(() => {
|
|
|
|
this.showSuccessLabel = false;
|
|
|
|
}, 2000)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
this.userService.submitUser(updateObject)
|
|
|
|
.subscribe(user => {
|
2017-09-15 21:02:43 +02:00
|
|
|
this.router.navigate(['..'], {relativeTo: this.route});
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
// duplicated user error message
|
|
|
|
if (error._body.indexOf('duplicate') >= 0) {
|
|
|
|
this.error = "Benutzername existiert bereits";
|
|
|
|
this.showErrorLabel = true;
|
|
|
|
setTimeout(() => {
|
|
|
|
this.showErrorLabel = false;
|
|
|
|
}, 5000);
|
|
|
|
}
|
|
|
|
})
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|