Files
SkydiveLogs/Front/skydivelogs-app/src/app/new-tunnel-flight/new-tunnel-flight.component.ts
2023-05-05 17:59:11 +02:00

121 lines
3.6 KiB
TypeScript

import { Component, OnInit } from "@angular/core";
import { formatDate } from '@angular/common';
import { DateAdapter, MAT_DATE_FORMATS, NativeDateAdapter } from "@angular/material/core";
import { TranslateService } from '@ngx-translate/core';
import { TunnelResp } from "../../models/tunnel";
import { DateService } from 'src/services/date.service';
import { TunnelService } from 'src/services/tunnel.service';
import { ServiceComm } from 'src/services/service-comm.service';
import { StatsService } from 'src/services/stats.service';
export const PICK_FORMATS = {
parse: { dateInput: 'yy MM dd' },
display: {
dateInput: 'yyyy-MM-dd',
monthYearLabel: 'yyyy MMM',
dateA11yLabel: 'yyyy MM dd',
monthYearA11yLabel: 'yyyy MMMM',
}
};
class PickDateAdapter extends NativeDateAdapter {
format(date: Date, displayFormat: Object): string {
return formatDate(date, displayFormat.toString(), "en");
}
}
@Component({
selector: 'app-new-tunnel-flight',
templateUrl: './new-tunnel-flight.component.html',
styleUrls: ['./new-tunnel-flight.component.css'],
providers: [
{ provide: DateAdapter, useClass: PickDateAdapter },
{ provide: MAT_DATE_FORMATS, useValue: PICK_FORMATS }
]
})
export class NewTunnelFlightComponent implements OnInit {
public beginDate: Date;
public endDate: Date;
public minutesOfFlight: number;
public selectedTunnel: TunnelResp;
public listOfTunnel: Array<TunnelResp>;
public resetForm: boolean;
public comments: string;
private countDatasLoaded: number;
private pendingAddRequest: boolean;
constructor(private serviceComm: ServiceComm,
private serviceTunnel: TunnelService,
private dateService: DateService,
private translateService: TranslateService,
private statsService: StatsService) { }
ngOnInit() {
this.serviceComm.forceTranslateTitle.subscribe((data) => {
if (data === true) {
this.updateTitle();
}
});
this.updateTitle();
this.pendingAddRequest = false;
this.initForm();
this.getListOfTunnels();
}
onFormSubmit() {
this.pendingAddRequest = true;
this.serviceTunnel.AddFlight(this.selectedTunnel.id,
this.beginDate,
this.minutesOfFlight,
this.comments)
.subscribe(() => {
this.statsService.resetStats();
this.comments = undefined;
if (this.resetForm === true) {
this.initForm();
}
this.pendingAddRequest = false;
});
}
public isValidatedForm(): boolean {
return (this.selectedTunnel !== undefined &&
this.selectedTunnel.id !== undefined &&
this.minutesOfFlight !== undefined &&
typeof this.minutesOfFlight === "number");
}
private getListOfTunnels() {
this.serviceTunnel.getListOfTunnels().subscribe((data) => {
data.sort((a, b) => a.name.localeCompare(b.name));
this.listOfTunnel = data;
this.countDatasLoaded++;
});
}
public notLoadingToDisplay(): boolean {
return !(this.pendingAddRequest || this.countDatasLoaded !== 1);
}
private updateTitle() {
this.translateService.get("NewTunnelFlight_Title").subscribe(
data => { this.serviceComm.UpdatedComponentTitle(data); }
);
}
private initForm() {
this.endDate = new Date();
this.endDate.setHours(0, 0, 0, 0);
this.beginDate = this.dateService.AddDays(this.endDate, -1);
this.minutesOfFlight = 1;
this.selectedTunnel = undefined;
this.comments = undefined;
}
}