opt-cc/static/src/app/request/award/req-award.component.ts

120 lines
3.4 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 {Award, Decoration, User} from '../../models/model-interfaces';
import {NgForm} from '@angular/forms';
import {AwardingService} from '../../services/army-management/awarding.service';
import {DecorationService} from '../../services/army-management/decoration.service';
import {UserService} from '../../services/army-management/user.service';
import {LoginService} from '../../services/app-user-service/login-service';
2017-06-09 18:30:35 +02:00
@Component({
templateUrl: './req-award.component.html',
styleUrls: ['./req-award.component.css', '../../style/overview.css'],
2017-06-09 18:30:35 +02:00
})
2018-03-08 09:44:35 +01:00
export class RequestAwardComponent implements OnInit {
2017-06-09 18:30:35 +02:00
@ViewChild(NgForm) form: NgForm;
showForm = false;
showSuccessLabel = false;
2017-08-02 20:20:05 +02:00
user: User = {_id: '0'};
2017-06-09 18:30:35 +02:00
decoration: Decoration = {_id: '0'};
2017-06-10 13:16:15 +02:00
2018-03-08 09:44:35 +01:00
reason = '';
2017-06-10 13:16:15 +02:00
2017-06-09 18:30:35 +02:00
decorations: Decoration[];
awards: Award[];
users: User[];
decoPreviewDisplay = 'none';
constructor(private router: Router,
private route: ActivatedRoute,
private userService: UserService,
private awardingService: AwardingService,
2017-09-23 11:53:10 +02:00
private decorationService: DecorationService,
private loginService: LoginService) {
2017-06-09 18:30:35 +02:00
}
ngOnInit() {
2017-09-23 11:53:10 +02:00
const currentUser = this.loginService.getCurrentUser();
2017-06-10 13:16:15 +02:00
this.userService.findUsers('', undefined, currentUser.squad._id).subscribe(users => {
2017-06-09 18:30:35 +02:00
this.users = users;
});
this.decorationService.findDecorations('', currentUser.squad.fraction).subscribe(decorations => {
this.decorations = decorations;
});
2017-06-09 18:30:35 +02:00
}
toggleUser() {
if (this.user._id !== '0') {
2017-08-02 20:20:05 +02:00
this.awardingService.getUserAwardings(this.user._id).subscribe(awards => {
this.awards = awards;
});
this.showForm = true;
} else {
this.showForm = false;
2017-06-10 13:16:15 +02:00
}
2017-06-09 18:30:35 +02:00
}
2017-06-10 13:16:15 +02:00
toggleDecoPreview(descriptionField, image) {
if (this.decoration._id !== '0') {
this.decoPreviewDisplay = 'flex'; // visible & keep same height for all children
2017-06-09 18:30:35 +02:00
const description = this.decorations.find(
decoration => decoration._id === this.decoration._id
).description;
2017-06-09 18:30:35 +02:00
image.src = 'resource/decoration/' + this.decoration._id + '.png';
descriptionField.innerHTML = description;
} else {
this.decoPreviewDisplay = 'none';
}
2017-06-09 18:30:35 +02:00
}
2017-06-10 13:16:15 +02:00
addAwarding(previewImage, descriptionField) {
if (this.decoration._id && this.reason.length > 0) {
const award: Award = {
2018-03-07 11:56:50 +01:00
'userId': this.user._id,
'decorationId': this.decoration._id,
'reason': this.reason,
'date': Date.now()
2017-06-09 18:30:35 +02:00
};
this.awardingService.requestAwarding(award).subscribe(() => {
this.awardingService.getUserAwardings(this.user._id)
2018-02-26 09:04:27 +01:00
.subscribe(awards => {
this.awards = awards;
this.decoration = {_id: '0'};
this.reason = previewImage.src = descriptionField.innerHTML = '';
this.decoPreviewDisplay = 'none';
this.showSuccessLabel = true;
setTimeout(() => {
this.showSuccessLabel = false;
2018-03-08 09:44:35 +01:00
}, 2000);
});
});
2017-06-09 18:30:35 +02:00
}
}
cancel() {
this.router.navigate(['..'], {relativeTo: this.route});
return false;
}
/**
* compare ngValue with ngModel to assign selected element
*/
equals(o1, o2) {
2017-06-09 18:30:35 +02:00
if (o1 && o2) {
return o1._id === o2._id;
}
}
}