2018-11-24 14:05:52 +01:00
|
|
|
import {Component, OnInit, ViewChild} from '@angular/core';
|
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
import {AppUser, Rank, Squad} from '../../models/model-interfaces';
|
|
|
|
import {SquadService} from '../../services/army-management/squad.service';
|
|
|
|
import {Subscription} from 'rxjs/Subscription';
|
|
|
|
import {NgForm} from '@angular/forms';
|
|
|
|
import {Fraction} from '../../utils/fraction.enum';
|
|
|
|
import {SnackBarService} from '../../services/user-interface/snack-bar/snack-bar.service';
|
|
|
|
import {AppUserService} from '../../services/app-user-service/app-user.service';
|
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
templateUrl: './edit-app-user.component.html',
|
2019-02-22 08:14:46 +01:00
|
|
|
styleUrls: ['./edit-app-user.component.scss'],
|
2018-11-24 14:05:52 +01:00
|
|
|
})
|
|
|
|
export class EditAppUserComponent implements OnInit {
|
|
|
|
|
|
|
|
@ViewChild(NgForm) form: NgForm;
|
|
|
|
|
|
|
|
subscription: Subscription;
|
|
|
|
|
|
|
|
appUser: AppUser = {};
|
|
|
|
|
|
|
|
appUserSquadId;
|
|
|
|
|
|
|
|
squads: Squad[] = [];
|
|
|
|
|
|
|
|
ranks: Rank[] = [];
|
|
|
|
|
|
|
|
ranksDisplay = 'none';
|
|
|
|
|
|
|
|
error: string;
|
|
|
|
|
|
|
|
readonly fraction = Fraction;
|
|
|
|
|
|
|
|
constructor(private router: Router,
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private appUserService: AppUserService,
|
|
|
|
private squadService: SquadService,
|
|
|
|
private snackBarService: SnackBarService) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.subscription = this.route.params
|
|
|
|
.map(params => params['id'])
|
|
|
|
.filter(id => id !== undefined)
|
|
|
|
.flatMap(id => this.appUserService.getAppUser(id))
|
|
|
|
.subscribe(appUser => {
|
|
|
|
this.appUser = appUser;
|
|
|
|
this.appUserSquadId = appUser.squad ? appUser.squad._id : 'null';
|
|
|
|
});
|
|
|
|
|
|
|
|
this.squadService.findSquads().subscribe(squads => {
|
|
|
|
this.squads = squads;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
saveUser() {
|
|
|
|
const updateObject: AppUser = {
|
|
|
|
_id: this.appUser._id,
|
|
|
|
username: this.appUser.username,
|
|
|
|
squad: this.appUserSquadId === 'null' ? null : this.appUserSquadId,
|
|
|
|
activated: this.appUser.activated,
|
|
|
|
permission: this.appUser.permission,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.appUserService.updateUser(updateObject)
|
|
|
|
.subscribe(appUser => {
|
|
|
|
this.appUser = appUser;
|
|
|
|
this.appUserSquadId = appUser.squad ? appUser.squad._id : 'null';
|
|
|
|
this.snackBarService.showSuccess('generic.save.success');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel() {
|
|
|
|
this.router.navigate([this.appUser._id ? '../..' : '..'], {relativeTo: this.route});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* compare ngValue with ngModel to assign selected element
|
|
|
|
*/
|
|
|
|
equals(o1: Squad, o2: Squad) {
|
|
|
|
if (o1 && o2) {
|
|
|
|
return o1._id === o2._id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|