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

133 lines
4.1 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, 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';
2018-03-08 09:44:35 +01:00
import {Subscription} from 'rxjs/Subscription';
2018-03-07 11:56:50 +01:00
import {NgForm} from '@angular/forms';
import {Fraction} from '../../../utils/fraction.enum';
import {SnackBarService} from '../../../services/user-interface/snack-bar/snack-bar.service';
2017-05-10 11:04:06 +02:00
@Component({
templateUrl: './edit-user.component.html',
styleUrls: ['./edit-user.component.scss'],
2017-05-10 11:04:06 +02:00
})
2018-03-08 09:44:35 +01:00
export class EditUserComponent implements OnInit {
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
ranksDisplay = 'none';
2017-05-10 11:04:06 +02:00
2017-09-15 21:02:43 +02:00
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,
private rankService: RankService,
private snackBarService: SnackBarService) {
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 {
2018-06-17 16:03:25 +02:00
this.rankService
.findRanks('', user.squadId.fraction)
.subscribe(ranks => {
this.ranks = ranks;
this.ranksDisplay = 'block';
});
2018-02-26 09:04:27 +01:00
}
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') {
2018-06-17 16:03:25 +02:00
this.rankService
.findRanks('', this.user.squadId.fraction)
.subscribe(
ranks => {
this.ranks = ranks;
this.ranksDisplay = 'block';
}
);
2017-05-13 14:57:40 +02:00
} 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.squadId) {
user.squadId = '0';
2017-09-15 21:02:43 +02:00
}
2018-02-26 09:04:27 +01:00
this.user = user;
2018-10-05 15:51:46 +02:00
this.snackBarService.showSuccess('generic.save.success');
2018-03-07 11:56:50 +01:00
});
2018-02-26 09:04:27 +01:00
} else {
this.userService.submitUser(updateObject)
.subscribe(user => {
this.router.navigate(['..'], {relativeTo: this.route});
2018-10-05 15:51:46 +02:00
this.snackBarService.showSuccess('generic.save.success');
2018-02-26 09:04:27 +01:00
return true;
},
error => {
// duplicated user error message
2018-10-05 15:51:46 +02:00
const errorMessage = error._body.includes('duplicate') ? 'generic.signup.error.duplicate' : error._body;
2018-07-15 15:38:33 +02:00
this.snackBarService.showError(errorMessage, 10000);
})
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
}
}
}