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';
|
2018-04-29 10:12:27 +02:00
|
|
|
import {WarService} from '../../../services/logs/war.service';
|
|
|
|
import {War} from '../../../models/model-interfaces';
|
|
|
|
import {CampaignService} from '../../../services/logs/campaign.service';
|
2018-06-30 17:29:58 +02:00
|
|
|
import {SnackBarService} from '../../../services/user-interface/snack-bar/snack-bar.service';
|
2018-07-17 11:34:32 +02:00
|
|
|
import {SpinnerService} from '../../../services/user-interface/spinner/spinner.service';
|
2018-10-03 11:49:17 +02:00
|
|
|
import {Subscription} from 'rxjs';
|
|
|
|
import {Fraction} from '../../../utils/fraction.enum';
|
2018-10-03 15:22:14 +02:00
|
|
|
import {TranslateService} from '@ngx-translate/core';
|
2017-07-14 23:33:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'war-submit',
|
|
|
|
templateUrl: './war-submit.component.html',
|
2018-07-17 11:34:32 +02:00
|
|
|
styleUrls: ['./war-submit.component.css', '../../../style/entry-form.css', '../../../style/overview.css']
|
2017-07-14 23:33:17 +02:00
|
|
|
})
|
|
|
|
export class WarSubmitComponent {
|
|
|
|
|
2017-10-21 15:00:49 +02:00
|
|
|
war: War = {players: []};
|
2017-07-14 23:33:17 +02:00
|
|
|
|
2018-10-03 11:49:17 +02:00
|
|
|
subscription: Subscription;
|
|
|
|
|
2017-07-14 23:33:17 +02:00
|
|
|
fileList: FileList;
|
|
|
|
|
2018-04-28 16:31:14 +02:00
|
|
|
readonly validExtensions = ['.rpt', '.log', '.txt'];
|
|
|
|
|
2018-10-03 11:49:17 +02:00
|
|
|
readonly fraction = Fraction;
|
|
|
|
|
2018-04-28 16:31:14 +02:00
|
|
|
showFileError = false;
|
2017-07-14 23:33:17 +02:00
|
|
|
|
|
|
|
loading = false;
|
|
|
|
|
|
|
|
@ViewChild(NgForm) form: NgForm;
|
|
|
|
|
|
|
|
constructor(private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
2017-09-14 12:01:16 +02:00
|
|
|
private warService: WarService,
|
2018-06-30 17:29:58 +02:00
|
|
|
private snackBarService: SnackBarService,
|
2018-07-17 11:34:32 +02:00
|
|
|
private spinnerService: SpinnerService,
|
2018-10-03 15:22:14 +02:00
|
|
|
private translate: TranslateService,
|
2017-10-08 12:17:21 +02:00
|
|
|
public campaignService: CampaignService) {
|
2018-10-03 11:49:17 +02:00
|
|
|
this.subscription = this.route.params
|
|
|
|
.map(params => params['id'])
|
|
|
|
.filter(id => id !== undefined)
|
|
|
|
.flatMap(id => this.warService.getWar(id))
|
|
|
|
.subscribe(war => {
|
|
|
|
this.war = war;
|
|
|
|
});
|
2017-07-14 23:33:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fileChange(event) {
|
2018-04-28 16:31:14 +02:00
|
|
|
if (this.validExtensions.filter(ext => event.target.files[0] &&
|
2018-04-29 09:51:28 +02:00
|
|
|
event.target.files[0].name.endsWith(ext)).length === 1) {
|
2018-04-28 16:31:14 +02:00
|
|
|
this.showFileError = false;
|
2017-07-14 23:33:17 +02:00
|
|
|
this.fileList = event.target.files;
|
2018-04-28 16:31:14 +02:00
|
|
|
} else {
|
|
|
|
this.showFileError = true;
|
|
|
|
this.fileList = undefined;
|
2017-07-14 23:33:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 10:42:28 +02:00
|
|
|
submitWar() {
|
|
|
|
if (this.war._id) {
|
|
|
|
this.warService.updateWar(this.war).subscribe((updatedWar) => {
|
|
|
|
this.router.navigate(['../../war/' + updatedWar._id], {relativeTo: this.route});
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
const errorMsg = JSON.parse(error._body).error.message;
|
|
|
|
this.snackBarService.showError('Error: '.concat(errorMsg), 15000);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
if (!this.fileList) {
|
|
|
|
this.translate.get('stats.war.submit.logfile').subscribe((fieldNameLogfile) => {
|
|
|
|
this.translate.get('public.error.message.required',
|
|
|
|
{fieldName: fieldNameLogfile}).subscribe((message) => {
|
|
|
|
this.snackBarService.showError(message, 4000);
|
|
|
|
})
|
2018-10-03 15:22:14 +02:00
|
|
|
})
|
2018-10-09 10:42:28 +02:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
});
|
2017-07-14 23:33:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel() {
|
|
|
|
this.router.navigate(['..'], {relativeTo: this.route});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|