opt-cc/static/src/app/manage/users/award-user/award-user.component.ts

124 lines
3.8 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} from '../../../models/model-interfaces';
2018-03-07 11:56:50 +01:00
import {NgForm} from '@angular/forms';
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({
templateUrl: './award-user.component.html',
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
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 = {};
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,
private awardingService: AwardingService,
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
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
}
toggleDecoPreview(descriptionField, decorationId, image) {
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';
}
}
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()
};
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
});
});
}
}
deleteAwarding() {
2018-06-17 12:48:08 +02:00
const checkedAwardings = this.awards.filter(award => award['checked'] === true);
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');
}
2017-05-14 15:13:13 +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
}
}