106 lines
3.3 KiB
TypeScript
106 lines
3.3 KiB
TypeScript
|
import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
|
||
|
import {ActivatedRoute, Router} from '@angular/router';
|
||
|
import {NgForm} from '@angular/forms';
|
||
|
import {Rank} from '../../../models/model-interfaces';
|
||
|
import {RankService} from '../../../services/army-management/rank.service';
|
||
|
import {Subscription} from 'rxjs/Subscription';
|
||
|
import {Fraction} from '../../../utils/fraction.enum';
|
||
|
import {Message} from '../../../i18n/de.messages';
|
||
|
import {SnackBarService} from '../../../services/user-interface/snack-bar/snack-bar.service';
|
||
|
import {TranslateService} from '@ngx-translate/core';
|
||
|
|
||
|
|
||
|
@Component({
|
||
|
templateUrl: './edit-rank.component.html',
|
||
|
styleUrls: ['./edit-rank.component.css', '../../../style/entry-form.css', '../../../style/overview.css']
|
||
|
})
|
||
|
export class EditRankComponent implements OnInit, OnDestroy {
|
||
|
|
||
|
subscription: Subscription;
|
||
|
|
||
|
rank: Rank = {name: '', fraction: '', level: 0};
|
||
|
|
||
|
fileList: FileList;
|
||
|
|
||
|
saved = false;
|
||
|
|
||
|
showImageError = false;
|
||
|
|
||
|
imagePreviewSrc;
|
||
|
|
||
|
@ViewChild(NgForm) form: NgForm;
|
||
|
|
||
|
readonly fraction = Fraction;
|
||
|
|
||
|
constructor(private route: ActivatedRoute,
|
||
|
private router: Router,
|
||
|
private rankService: RankService,
|
||
|
private snackBarService: SnackBarService,
|
||
|
private translate: TranslateService) {
|
||
|
}
|
||
|
|
||
|
ngOnInit() {
|
||
|
this.subscription = this.route.params
|
||
|
.map(params => params['id'])
|
||
|
.filter(id => id !== undefined)
|
||
|
.flatMap(id => this.rankService.getRank(id))
|
||
|
.subscribe(rank => {
|
||
|
this.rank = rank;
|
||
|
this.imagePreviewSrc = 'resource/rank/' + this.rank._id + '.png?' + Date.now();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
ngOnDestroy() {
|
||
|
this.subscription.unsubscribe();
|
||
|
}
|
||
|
|
||
|
fileChange(event) {
|
||
|
if (!event.target.files[0].name.endsWith('.png')) {
|
||
|
this.showImageError = true;
|
||
|
this.fileList = undefined;
|
||
|
} else {
|
||
|
this.showImageError = false;
|
||
|
this.fileList = event.target.files;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
saveRank(fileInput) {
|
||
|
let file: File;
|
||
|
if (!this.rank._id) {
|
||
|
if (this.fileList) {
|
||
|
file = this.fileList[0];
|
||
|
this.rankService.submitRank(this.rank, file)
|
||
|
.subscribe(rank => {
|
||
|
this.saved = true;
|
||
|
this.router.navigate(['..'], {relativeTo: this.route});
|
||
|
});
|
||
|
} else {
|
||
|
this.translate.get('ranks.submit.field.image').subscribe((fieldNameIMage) => {
|
||
|
this.translate.get('public.error.message.required',
|
||
|
{fieldName: fieldNameIMage}).subscribe((message) => {
|
||
|
this.snackBarService.showError(message, 4000);
|
||
|
})
|
||
|
});
|
||
|
}
|
||
|
} else {
|
||
|
if (this.fileList) {
|
||
|
file = this.fileList[0];
|
||
|
}
|
||
|
delete this.rank['__v'];
|
||
|
this.rankService.submitRank(this.rank, file)
|
||
|
.subscribe(rank => {
|
||
|
setTimeout(() => {
|
||
|
this.imagePreviewSrc = 'resource/rank/' + this.rank._id + '.png?' + Date.now();
|
||
|
}, 300);
|
||
|
fileInput.value = '';
|
||
|
this.snackBarService.showSuccess(Message.SUCCESS_SAVE);
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
cancel() {
|
||
|
this.router.navigate([this.rank._id ? '../..' : '..'], {relativeTo: this.route});
|
||
|
return false;
|
||
|
}
|
||
|
}
|