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';
|
2018-10-04 14:08:29 +02:00
|
|
|
import {Award, Decoration} from '../../../models/model-interfaces';
|
2018-03-07 11:56:50 +01:00
|
|
|
import {NgForm} from '@angular/forms';
|
2018-10-04 14:08:29 +02:00
|
|
|
import {AwardingService} from '../../../services/army-management/awarding.service';
|
|
|
|
import {DecorationService} from '../../../services/army-management/decoration.service';
|
|
|
|
import {Fraction} from '../../../utils/fraction.enum';
|
|
|
|
import {SnackBarService} from '../../../services/user-interface/snack-bar/snack-bar.service';
|
2018-10-04 10:33:15 +02:00
|
|
|
import {TranslateService} from '@ngx-translate/core';
|
2017-05-13 14:57:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
2017-05-17 15:55:22 +02:00
|
|
|
templateUrl: './award-user.component.html',
|
2019-02-25 14:32:29 +01:00
|
|
|
styleUrls: ['./award-user.component.scss'],
|
2017-05-13 14:57:40 +02:00
|
|
|
})
|
2018-03-08 09:44:35 +01:00
|
|
|
export class AwardUserComponent implements OnInit {
|
2017-05-13 14:57:40 +02:00
|
|
|
|
|
|
|
@ViewChild(NgForm) form: NgForm;
|
|
|
|
|
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
|
|
|
|
2018-10-04 10:33:15 +02:00
|
|
|
awardStatus = {};
|
|
|
|
|
2017-05-13 23:49:17 +02:00
|
|
|
decoPreviewDisplay = 'none';
|
|
|
|
|
2017-11-08 19:40:51 +01:00
|
|
|
readonly fraction = Fraction;
|
|
|
|
|
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,
|
2018-06-30 17:29:58 +02:00
|
|
|
private decorationService: DecorationService,
|
2018-10-04 10:33:15 +02:00
|
|
|
private snackBarService: SnackBarService,
|
|
|
|
private translate: TranslateService) {
|
2017-05-13 14:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2018-10-04 10:33:15 +02:00
|
|
|
['users.award.table.status.in.progress',
|
|
|
|
'users.award.table.status.approved',
|
|
|
|
'users.award.table.status.rejected'].forEach((i18n) => {
|
|
|
|
this.translate.get(i18n).subscribe((translated) => {
|
|
|
|
this.awardStatus[i18n] = translated;
|
|
|
|
})
|
|
|
|
});
|
2017-05-13 14:57:40 +02:00
|
|
|
|
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
|
2018-02-26 09:04:27 +01:00
|
|
|
.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
|
2018-02-26 09:04:27 +01:00
|
|
|
.map(params => params['id'])
|
2018-03-07 11:56:50 +01:00
|
|
|
.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 = {
|
2018-06-17 16:03:25 +02:00
|
|
|
userId: this.userId,
|
|
|
|
decorationId: decorationId,
|
|
|
|
reason: reason,
|
|
|
|
date: Date.now()
|
2017-05-13 23:49:17 +02:00
|
|
|
};
|
|
|
|
this.awardingService.addAwarding(award).subscribe(() => {
|
2017-05-14 15:13:13 +02:00
|
|
|
this.awardingService.getUserAwardings(this.userId)
|
2018-02-26 09:04:27 +01:00
|
|
|
.subscribe(awards => {
|
|
|
|
this.awards = awards;
|
|
|
|
this.decoPreviewDisplay = 'none';
|
|
|
|
decorationField.value = undefined;
|
|
|
|
reasonField.value = previewImage.src = descriptionField.innerHTML = '';
|
2018-10-05 15:51:46 +02:00
|
|
|
this.snackBarService.showSuccess('generic.save.success');
|
2018-03-07 11:56:50 +01:00
|
|
|
});
|
|
|
|
});
|
2017-05-13 23:49:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-17 12:46:58 +02:00
|
|
|
deleteAwarding() {
|
2018-06-17 12:48:08 +02:00
|
|
|
const checkedAwardings = this.awards.filter(award => award['checked'] === true);
|
2018-06-17 12:46:58 +02:00
|
|
|
|
|
|
|
if (checkedAwardings.length > 0) {
|
|
|
|
checkedAwardings.forEach(awarding => {
|
|
|
|
this.awardingService.deleteAwarding(awarding._id).subscribe((res) => {
|
|
|
|
this.awardingService.getUserAwardings(this.userId)
|
|
|
|
.subscribe((awards) => {
|
|
|
|
this.awards = awards;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-10-05 15:51:46 +02:00
|
|
|
this.snackBarService.showSuccess('generic.save.success');
|
2018-06-17 12:46:58 +02:00
|
|
|
}
|
2017-05-14 15:13:13 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|