Add search input date check

This commit is contained in:
Patrick Gebhardt 2020-06-19 15:13:32 +02:00
parent 615727e09d
commit 587e231e05
2 changed files with 15 additions and 2 deletions

View File

@ -3,11 +3,11 @@
<h2>When is your trip?</h2> <h2>When is your trip?</h2>
<mat-form-field appearance="outline"> <mat-form-field appearance="outline">
<mat-label>Start</mat-label> <mat-label>Start</mat-label>
<input [(ngModel)]="from" matInput required type="date"> <input (change)="checkDates()" [(ngModel)]="from" [min]="today" matInput required type="date">
</mat-form-field> </mat-form-field>
<mat-form-field appearance="outline"> <mat-form-field appearance="outline">
<mat-label>End</mat-label> <mat-label>End</mat-label>
<input [(ngModel)]="to" matInput required type="date"> <input (change)="checkDates()" [(ngModel)]="to" [min]="from" matInput required type="date">
</mat-form-field> </mat-form-field>
</section> </section>

View File

@ -24,6 +24,8 @@ export class SearchInputComponent implements OnInit {
singlePresetSelection = {}; singlePresetSelection = {};
multiPresetSelection = {}; multiPresetSelection = {};
readonly today = this.from = formatDate(new Date(), 'yyyy-MM-dd', 'en-GB');
constructor(private router: Router, private ps: PresetService, private ss: SearchService) { constructor(private router: Router, private ps: PresetService, private ss: SearchService) {
const from = new Date(); const from = new Date();
const to = new Date(); const to = new Date();
@ -94,4 +96,15 @@ export class SearchInputComponent implements OnInit {
return true; return true;
} }
} }
checkDates() {
const fromDate = new Date(this.from);
const toDate = new Date(this.to);
if (toDate <= fromDate) {
const newToDate = new Date(this.from);
newToDate.setDate(fromDate.getDate() + 1);
this.to = formatDate(newToDate, 'yyyy-MM-dd', 'en-GB');
}
}
} }