opt-cc/static/src/app/users/edit-user/edit-user.component.ts

122 lines
3.0 KiB
TypeScript
Raw Normal View History

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({
templateUrl: './edit-user.component.html',
styleUrls: ['./edit-user.component.css', '../../style/entry-form.css'],
2017-05-10 11:04:06 +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
showSuccessLabel = false;
ranksDisplay = 'none';
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 => {
this.router.navigate(['..'], {relativeTo: this.route});
return true;
})
}
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
}
}
}