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

114 lines
3.1 KiB
TypeScript
Raw Normal View History

2017-06-09 18:30:35 +02:00
import {Component, ViewChild} from "@angular/core";
import {ActivatedRoute, Router} from "@angular/router";
import {Award, Decoration, User} from "../../models/model-interfaces";
import {NgForm} from "@angular/forms";
import {AwardingService} from "../../services/awarding-service/awarding.service";
import {DecorationService} from "../../services/decoration-service/decoration.service";
import {UserService} from "../../services/user-service/user.service";
@Component({
templateUrl: './req-award.component.html',
styleUrls: ['./req-award.component.css'],
})
export class RequestAwardComponent {
@ViewChild(NgForm) form: NgForm;
showForm = false;
showSuccessLabel = false;
user: User = {};
decorations: Decoration[];
awards: Award[];
users: User[];
decoPreviewDisplay = 'none';
constructor(private router: Router,
private route: ActivatedRoute,
private userService: UserService,
private awardingService: AwardingService,
private decorationService: DecorationService) {
}
ngOnInit() {
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
// show only current users squad members
this.userService.findUsers('', undefined, currentUser.squad).subscribe(users => {
this.users = users;
});
}
toggleUser() {
this.decorationService.findDecorations('', this.user.squad.fraction).subscribe(decorations => {
this.decorations = decorations;
});
this.awardingService.getUserAwardings(this.user._id).subscribe(awards => {
this.awards = awards;
});
this.showForm = true;
}
toggleDecoPreview(descriptionField, decorationId, image) {
this.decoPreviewDisplay = 'flex'; // visible & keep same height for all children
const description = this.decorations.find(
decoration => decoration._id === decorationId
).description;
image.src = 'resource/decoration/' + decorationId + '.png';
descriptionField.innerHTML = description;
}
addAwarding(decorationField, reasonField, previewImage, descriptionField) {
const decorationId = decorationField.value;
const reason = reasonField.value;
if (decorationId && reason.length > 0) {
const award = {
"userId": this.user._id,
"decorationId": decorationId,
"reason": reason,
"date": Date.now()
};
this.awardingService.requestAwarding(award).subscribe(() => {
this.awardingService.getUserAwardings(this.user._id)
.subscribe(awards => {
this.awards = awards;
console.log(awards[0])
this.decoPreviewDisplay = 'none';
decorationField.value = undefined;
reasonField.value = previewImage.src = descriptionField.innerHTML = '';
this.showSuccessLabel = true;
setTimeout(() => {
this.showSuccessLabel = false;
}, 2000)
})
})
}
}
cancel() {
this.router.navigate(['..'], {relativeTo: this.route});
return false;
}
/**
* compare ngValue with ngModel to assign selected element
*/
equals(o1: User, o2: User) {
if (o1 && o2) {
return o1._id === o2._id;
}
}
}