2017-05-13 14:57:40 +02:00
|
|
|
import {Component, ViewChild} from "@angular/core";
|
|
|
|
import {ActivatedRoute, Router} from "@angular/router";
|
2017-05-14 15:13:13 +02:00
|
|
|
import {Award, Decoration} from "../../models/model-interfaces";
|
2017-05-13 14:57:40 +02:00
|
|
|
import {NgForm} from "@angular/forms";
|
2017-05-13 23:49:17 +02:00
|
|
|
import {AwardingService} from "../../services/awarding-service/awarding.service";
|
|
|
|
import {DecorationService} from "../../services/decoration-service/decoration.service";
|
2017-05-13 14:57:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
2017-05-17 15:55:22 +02:00
|
|
|
templateUrl: './award-user.component.html',
|
|
|
|
styleUrls: ['./award-user.component.css'],
|
2017-05-13 14:57:40 +02:00
|
|
|
})
|
2017-05-18 14:32:51 +02:00
|
|
|
export class AwardUserComponent {
|
2017-05-13 14:57:40 +02:00
|
|
|
|
|
|
|
@ViewChild(NgForm) form: NgForm;
|
|
|
|
|
|
|
|
showSuccessLabel = false;
|
|
|
|
|
2017-05-14 15:13:13 +02:00
|
|
|
userId: string;
|
2017-05-13 14:57:40 +02:00
|
|
|
|
2017-05-13 23:49:17 +02:00
|
|
|
decorations: Decoration[];
|
2017-05-13 14:57:40 +02:00
|
|
|
|
2017-05-14 15:13:13 +02:00
|
|
|
awards: Award[];
|
2017-05-13 14:57:40 +02:00
|
|
|
|
2017-05-13 23:49:17 +02:00
|
|
|
decoPreviewDisplay = 'none';
|
|
|
|
|
2017-05-13 14:57:40 +02:00
|
|
|
constructor(private router: Router,
|
|
|
|
private route: ActivatedRoute,
|
2017-05-13 23:49:17 +02:00
|
|
|
private awardingService: AwardingService,
|
|
|
|
private decorationService: DecorationService) {
|
2017-05-13 14:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
2017-05-13 23:49:17 +02:00
|
|
|
this.decorationService.findDecorations().subscribe(decorations => {
|
|
|
|
this.decorations = decorations;
|
|
|
|
});
|
2017-05-13 14:57:40 +02:00
|
|
|
|
2017-05-14 15:13:13 +02:00
|
|
|
this.route.params
|
|
|
|
.map(params => params['id'])
|
|
|
|
.flatMap(id => this.awardingService.getUserAwardings(id))
|
|
|
|
.subscribe(awards => {
|
|
|
|
this.awards = awards;
|
|
|
|
});
|
2017-05-13 14:57:40 +02:00
|
|
|
|
2017-05-14 15:13:13 +02:00
|
|
|
this.route.params
|
|
|
|
.map(params => params['id'])
|
|
|
|
.subscribe(id => this.userId = id)
|
2017-05-13 14:57:40 +02:00
|
|
|
}
|
|
|
|
|
2017-05-13 23:49:17 +02:00
|
|
|
|
|
|
|
toggleDecoPreview(descriptionField, decorationId, image) {
|
2017-08-05 00:05:47 +02:00
|
|
|
if (decorationId !== '0') {
|
|
|
|
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;
|
|
|
|
} else {
|
|
|
|
this.decoPreviewDisplay = 'none';
|
|
|
|
}
|
2017-05-13 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
addAwarding(decorationField, reasonField, previewImage, descriptionField) {
|
|
|
|
const decorationId = decorationField.value;
|
|
|
|
const reason = reasonField.value;
|
|
|
|
if (decorationId && reason.length > 0) {
|
|
|
|
const award = {
|
2017-05-14 15:13:13 +02:00
|
|
|
"userId": this.userId,
|
2017-05-13 23:49:17 +02:00
|
|
|
"decorationId": decorationId,
|
|
|
|
"reason": reason,
|
|
|
|
"date": Date.now()
|
|
|
|
};
|
|
|
|
this.awardingService.addAwarding(award).subscribe(() => {
|
2017-05-14 15:13:13 +02:00
|
|
|
this.awardingService.getUserAwardings(this.userId)
|
2017-05-13 23:49:17 +02:00
|
|
|
.subscribe(awards => {
|
2017-05-14 15:13:13 +02:00
|
|
|
this.awards = awards;
|
2017-05-13 23:49:17 +02:00
|
|
|
this.decoPreviewDisplay = 'none';
|
|
|
|
decorationField.value = undefined;
|
2017-05-14 15:13:13 +02:00
|
|
|
reasonField.value = previewImage.src = descriptionField.innerHTML = '';
|
2017-05-13 23:49:17 +02:00
|
|
|
this.showSuccessLabel = true;
|
|
|
|
setTimeout(() => {
|
|
|
|
this.showSuccessLabel = false;
|
|
|
|
}, 2000)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-14 15:13:13 +02:00
|
|
|
deleteAwarding(awardingId) {
|
|
|
|
this.awardingService.deleteAwarding(awardingId).subscribe((res) => {
|
|
|
|
this.awardingService.getUserAwardings(this.userId)
|
|
|
|
.subscribe((awards) => {
|
|
|
|
this.awards = awards;
|
|
|
|
this.showSuccessLabel = true;
|
|
|
|
setTimeout(() => {
|
|
|
|
this.showSuccessLabel = false;
|
|
|
|
}, 2000)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2017-05-13 23:49:17 +02:00
|
|
|
|
2017-05-14 15:13:13 +02:00
|
|
|
cancel() {
|
|
|
|
this.router.navigate(['../..'], {relativeTo: this.route});
|
|
|
|
return false;
|
2017-05-13 14:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|