2017-05-10 11:04:06 +02:00
|
|
|
import {Component, ViewChild} from "@angular/core";
|
|
|
|
import {ActivatedRoute, Router} from "@angular/router";
|
|
|
|
import {NgForm} from "@angular/forms";
|
|
|
|
import {Decoration} from "../../models/model-interfaces";
|
2017-10-22 18:06:37 +02:00
|
|
|
import {DecorationService} from "../../services/army-management/decoration.service";
|
2017-05-16 20:06:00 +02:00
|
|
|
import {Subscription} from "rxjs/Subscription";
|
2017-11-08 19:23:15 +01:00
|
|
|
import {Fraction} from "../../utils/fraction.enum";
|
2017-05-10 11:04:06 +02:00
|
|
|
|
|
|
|
@Component({
|
2017-05-17 15:55:22 +02:00
|
|
|
templateUrl: './edit-decoration.component.html',
|
2017-10-07 19:32:16 +02:00
|
|
|
styleUrls: ['./edit-decoration.component.css', '../../style/entry-form.css', '../../style/overview.css']
|
2017-05-10 11:04:06 +02:00
|
|
|
})
|
2017-05-17 15:55:22 +02:00
|
|
|
export class EditDecorationComponent {
|
2017-05-10 11:04:06 +02:00
|
|
|
|
2017-05-16 20:06:00 +02:00
|
|
|
subscription: Subscription;
|
|
|
|
|
2017-05-10 11:04:06 +02:00
|
|
|
decoration: Decoration = {name: '', fraction: '', sortingNumber: 0};
|
|
|
|
|
|
|
|
fileList: FileList;
|
|
|
|
|
|
|
|
showImageError = false;
|
|
|
|
|
2017-05-16 20:06:00 +02:00
|
|
|
imagePreviewSrc;
|
|
|
|
|
|
|
|
showSuccessLabel = false;
|
|
|
|
|
2017-05-10 11:04:06 +02:00
|
|
|
@ViewChild(NgForm) form: NgForm;
|
|
|
|
|
2017-11-08 19:23:15 +01:00
|
|
|
readonly fraction = Fraction;
|
|
|
|
|
2017-05-10 11:04:06 +02:00
|
|
|
constructor(private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
|
|
|
private decorationService: DecorationService) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2017-05-16 20:06:00 +02:00
|
|
|
this.subscription = this.route.params
|
2018-02-26 09:04:27 +01:00
|
|
|
.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();
|
|
|
|
});
|
2017-05-16 20:06:00 +02:00
|
|
|
}
|
2017-05-10 11:04:06 +02:00
|
|
|
|
2017-05-16 20:06:00 +02:00
|
|
|
ngOnDestroy() {
|
|
|
|
this.subscription.unsubscribe();
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-16 20:06:00 +02:00
|
|
|
saveDecoration(fileInput) {
|
|
|
|
let file: File;
|
|
|
|
if (!this.decoration._id) {
|
|
|
|
if (this.fileList) {
|
|
|
|
file = this.fileList[0];
|
|
|
|
this.decorationService.submitDecoration(this.decoration, file)
|
2018-02-26 09:04:27 +01:00
|
|
|
.subscribe(rank => {
|
|
|
|
this.router.navigate(['..'], {relativeTo: this.route});
|
|
|
|
})
|
2017-05-16 20:06:00 +02:00
|
|
|
} else {
|
|
|
|
return window.alert(`Bild ist ein Pflichtfeld`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this.fileList) {
|
|
|
|
file = this.fileList[0];
|
|
|
|
}
|
|
|
|
delete this.decoration['__v'];
|
2017-05-10 11:04:06 +02:00
|
|
|
this.decorationService.submitDecoration(this.decoration, file)
|
2018-02-26 09:04:27 +01:00
|
|
|
.subscribe(rank => {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.imagePreviewSrc = 'resource/decoration/' + this.decoration._id + '.png?' + Date.now();
|
|
|
|
}, 300);
|
|
|
|
fileInput.value = '';
|
|
|
|
this.showSuccessLabel = true;
|
|
|
|
setTimeout(() => {
|
|
|
|
this.showSuccessLabel = false;
|
|
|
|
}, 2000)
|
|
|
|
})
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel() {
|
2017-05-16 20:06:00 +02:00
|
|
|
this.router.navigate([this.decoration._id ? '../..' : '..'], {relativeTo: this.route});
|
2017-05-10 11:04:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|