opt-cc/static/src/app/squads/edit-squad/edit-squad.component.ts

109 lines
3.0 KiB
TypeScript
Raw Normal View History

2018-03-07 11:56:50 +01:00
import {Component, ViewChild} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {NgForm} from '@angular/forms';
import {Squad} from '../../models/model-interfaces';
import {SquadService} from '../../services/army-management/squad.service';
import {Subscription} from 'rxjs/Subscription';
import {Fraction} from '../../utils/fraction.enum';
2017-05-10 11:04:06 +02:00
@Component({
templateUrl: './edit-squad.component.html',
styleUrls: ['./edit-squad.component.css', '../../style/entry-form.css', '../../style/overview.css']
2017-05-10 11:04:06 +02:00
})
export class EditSquadComponent {
2017-05-10 11:04:06 +02:00
subscription: Subscription;
2017-05-10 11:04:06 +02:00
squad: Squad = {name: '', fraction: '', sortingNumber: 0};
fileList: FileList;
saved = false;
showImageError = false;
showSuccessLabel = false;
imagePreviewSrc;
2017-05-10 11:04:06 +02:00
@ViewChild(NgForm) form: NgForm;
2017-11-08 19:35:34 +01:00
readonly fraction = Fraction;
2017-05-10 11:04:06 +02:00
constructor(private route: ActivatedRoute,
private router: Router,
2017-09-03 12:49:59 +02:00
private squadService: SquadService) {
2017-05-10 11:04:06 +02:00
}
ngOnInit() {
this.subscription = this.route.params
2018-02-26 09:04:27 +01:00
.map(params => params['id'])
2018-03-07 11:56:50 +01:00
.filter(id => id !== undefined)
2018-02-26 09:04:27 +01:00
.flatMap(id => this.squadService.getSquad(id))
.subscribe(squad => {
this.squad = squad;
this.imagePreviewSrc = 'resource/squad/' + this.squad._id + '.png?' + Date.now();
});
}
2017-05-10 11:04:06 +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;
}
}
saveSquad(fileInput) {
let file: File;
if (!this.squad._id) {
if (this.fileList) {
file = this.fileList[0];
this.squadService.submitSquad(this.squad, file)
2018-02-26 09:04:27 +01:00
.subscribe(rank => {
this.saved = true;
this.router.navigate(['..'], {relativeTo: this.route});
2018-03-07 11:56:50 +01:00
});
} else {
return window.alert(`Bild ist ein Pflichtfeld`);
}
} else {
if (this.fileList) {
file = this.fileList[0];
}
delete this.squad['__v'];
2017-05-10 11:04:06 +02:00
this.squadService.submitSquad(this.squad, file)
2018-02-26 09:04:27 +01:00
.subscribe(rank => {
setTimeout(() => {
this.imagePreviewSrc = 'resource/squad/' + this.squad._id + '.png?' + Date.now();
}, 300);
fileInput.value = '';
this.showSuccessLabel = true;
setTimeout(() => {
this.showSuccessLabel = false;
}, 2000)
})
2017-05-10 11:04:06 +02:00
}
}
cancel() {
this.router.navigate([this.squad._id ? '../..' : '..'], {relativeTo: this.route});
2017-05-10 11:04:06 +02:00
return false;
}
canDeactivate(): boolean {
if (this.saved || !this.form.dirty) {
return true;
}
return window.confirm(`Ihr Formular besitzt ungespeicherte Änderungen, möchten Sie die Seite wirklich verlassen?`);
}
}