opt-cc/static/src/app/manage/decorations/edit-decoration/edit-decoration.component.ts

105 lines
3.5 KiB
TypeScript
Raw Normal View History

2018-03-08 09:44:35 +01:00
import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
2018-03-07 11:56:50 +01:00
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';
2018-03-07 11:56:50 +01:00
import {Subscription} from 'rxjs/Subscription';
import {Fraction} from '../../../utils/fraction.enum';
import {SnackBarService} from '../../../services/user-interface/snack-bar/snack-bar.service';
2018-10-04 11:48:22 +02:00
import {TranslateService} from '@ngx-translate/core';
2017-05-10 11:04:06 +02:00
@Component({
templateUrl: './edit-decoration.component.html',
styleUrls: ['./edit-decoration.component.scss']
2017-05-10 11:04:06 +02:00
})
2018-03-08 09:44:35 +01:00
export class EditDecorationComponent implements OnInit, OnDestroy {
2017-05-10 11:04:06 +02:00
subscription: Subscription;
2017-05-10 11:04:06 +02:00
decoration: Decoration = {name: '', fraction: '', sortingNumber: 0};
fileList: FileList;
showImageError = false;
imagePreviewSrc;
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,
2018-10-04 11:48:22 +02:00
private snackBarService: SnackBarService,
private translate: TranslateService) {
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-08 09:44:35 +01:00
.filter(id => id !== undefined)
2018-02-26 09:04:27 +01:00
.flatMap(id => this.decorationService.getDecoration(id))
.subscribe(decoration => {
this.decoration = decoration;
this.imagePreviewSrc = 'resource/decoration/' + this.decoration._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;
}
}
saveDecoration(fileInput) {
let file: File;
if (!this.decoration._id) {
if (this.fileList) {
file = this.fileList[0];
this.decorationService.submitDecoration(this.decoration, file)
.subscribe(decoration => {
2018-02-26 09:04:27 +01:00
this.router.navigate(['..'], {relativeTo: this.route});
2018-03-07 11:56:50 +01:00
});
} else {
2018-10-04 11:48:22 +02:00
this.translate.get('decorations.submit.field.image').subscribe((fieldNameLogo) => {
this.translate.get('public.error.message.required',
{fieldName: fieldNameLogo}).subscribe((message) => {
this.snackBarService.showError(message, 4000);
})
});
}
} 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)
.subscribe(decoration => {
2018-02-26 09:04:27 +01:00
setTimeout(() => {
this.imagePreviewSrc = 'resource/decoration/' + this.decoration._id + '.png?' + Date.now();
}, 300);
fileInput.value = '';
2018-10-05 15:51:46 +02:00
this.snackBarService.showSuccess('generic.save.success');
}, error => {
const errorMsg = error._body ? JSON.parse(error._body).error.message : error.error.error.message;
this.snackBarService.showError('Error: '.concat(errorMsg), 15000);
2018-03-07 11:56:50 +01:00
});
2017-05-10 11:04:06 +02:00
}
}
cancel() {
this.router.navigate([this.decoration._id ? '../..' : '..'], {relativeTo: this.route});
2017-05-10 11:04:06 +02:00
return false;
}
}