import {Component, ViewChild} from '@angular/core'; import {ActivatedRoute, Router} from '@angular/router'; import {NgForm} from '@angular/forms'; import {WarService} from '../../../services/logs/war.service'; import {War} from '../../../models/model-interfaces'; import {CampaignService} from '../../../services/logs/campaign.service'; import {SnackBarService} from '../../../services/user-interface/snack-bar/snack-bar.service'; import {SpinnerService} from '../../../services/user-interface/spinner/spinner.service'; @Component({ selector: 'war-submit', templateUrl: './war-submit.component.html', styleUrls: ['./war-submit.component.css', '../../../style/entry-form.css', '../../../style/overview.css'] }) export class WarSubmitComponent { war: War = {players: []}; fileList: FileList; readonly validExtensions = ['.rpt', '.log', '.txt']; showFileError = false; loading = false; @ViewChild(NgForm) form: NgForm; constructor(private route: ActivatedRoute, private router: Router, private warService: WarService, private snackBarService: SnackBarService, private spinnerService: SpinnerService, public campaignService: CampaignService) { } fileChange(event) { if (this.validExtensions.filter(ext => event.target.files[0] && event.target.files[0].name.endsWith(ext)).length === 1) { this.showFileError = false; this.fileList = event.target.files; } else { this.showFileError = true; this.fileList = undefined; } } saveWar() { if (!this.fileList) { return window.alert(`Logfile ist ein Pflichtfeld`); } const file: File = this.fileList[0]; this.loading = true; this.spinnerService.activate(); this.warService.submitWar(this.war, file) .subscribe(war => { this.router.navigate(['../war/' + war._id], {relativeTo: this.route}); }, error => { this.spinnerService.deactivate(); this.loading = false; const errorMsg = JSON.parse(error._body).error.message; this.snackBarService.showError('Error: '.concat(errorMsg), 15000); }); } cancel() { this.router.navigate(['..'], {relativeTo: this.route}); return false; } }