98 lines
2.9 KiB
TypeScript
98 lines
2.9 KiB
TypeScript
import {Component, ViewChild} from '@angular/core';
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
import {NgForm} from '@angular/forms';
|
|
import {Decoration} from '../../models/model-interfaces';
|
|
import {DecorationService} from '../../services/army-management/decoration.service';
|
|
import {Subscription} from 'rxjs/Subscription';
|
|
import {Fraction} from '../../utils/fraction.enum';
|
|
|
|
@Component({
|
|
templateUrl: './edit-decoration.component.html',
|
|
styleUrls: ['./edit-decoration.component.css', '../../style/entry-form.css', '../../style/overview.css']
|
|
})
|
|
export class EditDecorationComponent {
|
|
|
|
subscription: Subscription;
|
|
|
|
decoration: Decoration = {name: '', fraction: '', sortingNumber: 0};
|
|
|
|
fileList: FileList;
|
|
|
|
showImageError = false;
|
|
|
|
imagePreviewSrc;
|
|
|
|
showSuccessLabel = false;
|
|
|
|
@ViewChild(NgForm) form: NgForm;
|
|
|
|
readonly fraction = Fraction;
|
|
|
|
constructor(private route: ActivatedRoute,
|
|
private router: Router,
|
|
private decorationService: DecorationService) {
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.subscription = this.route.params
|
|
.map(params => params['id'])
|
|
.filter(id => id != undefined)
|
|
.flatMap(id => this.decorationService.getDecoration(id))
|
|
.subscribe(decoration => {
|
|
this.decoration = decoration;
|
|
this.imagePreviewSrc = 'resource/decoration/' + this.decoration._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;
|
|
}
|
|
}
|
|
|
|
saveDecoration(fileInput) {
|
|
let file: File;
|
|
if (!this.decoration._id) {
|
|
if (this.fileList) {
|
|
file = this.fileList[0];
|
|
this.decorationService.submitDecoration(this.decoration, file)
|
|
.subscribe(rank => {
|
|
this.router.navigate(['..'], {relativeTo: this.route});
|
|
});
|
|
} else {
|
|
return window.alert(`Bild ist ein Pflichtfeld`);
|
|
}
|
|
} else {
|
|
if (this.fileList) {
|
|
file = this.fileList[0];
|
|
}
|
|
delete this.decoration['__v'];
|
|
this.decorationService.submitDecoration(this.decoration, file)
|
|
.subscribe(rank => {
|
|
setTimeout(() => {
|
|
this.imagePreviewSrc = 'resource/decoration/' + this.decoration._id + '.png?' + Date.now();
|
|
}, 300);
|
|
fileInput.value = '';
|
|
this.showSuccessLabel = true;
|
|
setTimeout(() => {
|
|
this.showSuccessLabel = false;
|
|
}, 2000);
|
|
});
|
|
}
|
|
}
|
|
|
|
cancel() {
|
|
this.router.navigate([this.decoration._id ? '../..' : '..'], {relativeTo: this.route});
|
|
return false;
|
|
}
|
|
|
|
}
|