Work in edit travel. Login DOES NOT wok!

This commit is contained in:
Eneko Nieto
2021-02-07 23:16:24 +01:00
parent 99be20f47e
commit 386ab547bd
10 changed files with 45 additions and 19 deletions

View File

@@ -30,10 +30,6 @@ export class AppComponent {
this.router.navigate(['/']);
}
});
// Optional
if (authConfig.useSilentRefresh) {
this.oauthService.setupAutomaticSilentRefresh();
}
this.oauthService.setupAutomaticSilentRefresh();
}
}

View File

@@ -37,7 +37,7 @@ export const authConfig: AuthConfig = {
useSilentRefresh: useSilentRefreshForCodeFlow,
showDebugInformation: false,
showDebugInformation: true,
sessionChecksEnabled: true,
sessionCheckIntervall: 10000,

View File

@@ -1,4 +1,4 @@
<div>
<h1>Editar viaje</h1>
<app-travel-form></app-travel-form>
<app-travel-form (travelSubmitted)="editTravel($event)" (travelCanceled)="cancel()" [travel]="travel" [acceptButtonText]="acceptButtonText"></app-travel-form>
</div>

View File

@@ -1,5 +1,5 @@
import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ActivatedRoute, Router } from '@angular/router';
import { TravelFormComponent } from 'src/app/views/travel-form/travel-form.component';
import { PUBLIC_API_URL } from '../../app.config';
import { Travel, TravelId } from '../../entities/travel';
@@ -12,10 +12,12 @@ import { ApiService } from '../../services/api.service';
})
export class EditTravelComponent implements OnInit {
travel: Travel;
acceptButtonText = 'Save';
@ViewChild(TravelFormComponent, { static: true }) form: TravelFormComponent;
constructor(
private router: Router,
private apiService: ApiService,
private route: ActivatedRoute
) { }
@@ -35,4 +37,21 @@ export class EditTravelComponent implements OnInit {
});
});
}
editTravel(travel: Travel): void {
this.apiService.createTravel(travel)
.subscribe(res => {
if (res.success) {
console.log('Travel edited');
this.router.navigateByUrl('/');
}
else {
console.error('Error creating travel: ' + res.error);
}
});
}
cancel(): void {
this.router.navigateByUrl('/');
}
}

View File

@@ -13,4 +13,4 @@
</mat-card>
<button mat-raised-button class="btn btn-default" (click)="goBack()">Back</button>
<button mat-raised-button class="btn btn-default" (click)="edit()">Edit</button>
<button *ngIf="logged" mat-raised-button class="btn btn-default" (click)="edit()">Edit</button>

View File

@@ -1,5 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { OAuthService } from 'angular-oauth2-oidc';
import { Travel, TravelId } from '../../entities/travel';
import { ApiService } from '../../services/api.service';
@@ -15,6 +16,7 @@ export class TravelComponent implements OnInit {
constructor(
private router: Router,
private oauthService: OAuthService,
private apiService: ApiService,
private route: ActivatedRoute
) { }
@@ -40,6 +42,11 @@ export class TravelComponent implements OnInit {
}
edit(): void {
this.router.navigateByUrl('/');
this.router.navigateByUrl(`/travel/${this.travelId}/edit`);
}
get logged(): boolean {
return this.oauthService.getIdentityClaims() != null;
// return this.oauthService.hasValidIdToken() && this.oauthService.hasValidAccessToken();
}
}

View File

@@ -39,6 +39,10 @@ export class ApiService {
return this.callApi<void>(TRAVEL_API_URL + '/create', null, travel);
}
editTravel = (travel: Travel) => {
return this.callApi<void>(TRAVEL_API_URL + '/edit', null, travel);
}
private callApi<T>(
url: string,
params?: { [param: string]: any },

View File

@@ -15,6 +15,7 @@ export class HeaderComponent implements OnInit {
) { }
ngOnInit(): void {
console.log('INIT logged=' + String(this.logged));
this.oauthService.configure(authConfig);
this.oauthService.loadDiscoveryDocumentAndTryLogin().then(success => {
console.log('Autologin success=' + success + ' logged=' + String(this.logged));

View File

@@ -1,8 +1,8 @@
<form #travelForm="ngForm">
<form *ngIf="travel" #travelForm="ngForm">
<div>
<label for="departureDate">Fecha de salida</label>
<input matInput id="departureDate" name="departureDate" [ngxMatDatetimePicker]="picker"
placeholder="Choose a date" [min]="minDate" [(ngModel)]="newTravel.departureDate">
placeholder="Choose a date" [min]="minDate" [(ngModel)]="travel.departureDate">
<mat-datepicker-toggle matSuffix [for]="$any(picker)"></mat-datepicker-toggle>
<ngx-mat-datetime-picker #picker [showSeconds]="$any('false')" [stepMinute]="$any('15')">
</ngx-mat-datetime-picker>
@@ -13,7 +13,7 @@
<div>
<label for="origin">Origen</label>
<input type="text" id="origin" required [(ngModel)]="newTravel.origin" name="origin">
<input type="text" id="origin" required [(ngModel)]="travel.origin" name="origin">
<div hidden="origin.valid || origin.pristine" class="alert alert-danger">
El origen es obligatorio
</div>
@@ -21,7 +21,7 @@
<div>
<label for="destination">Destino</label>
<input type="text" id="destination" required [(ngModel)]="newTravel.destination" name="destination">
<input type="text" id="destination" required [(ngModel)]="travel.destination" name="destination">
<div hidden="destination.valid || destination.pristine" class="alert alert-danger">
El destino es obligatorio
</div>
@@ -29,7 +29,7 @@
<div>
<label for="places">Plazas</label>
<input type="number" id="places" required [(ngModel)]="newTravel.places" name="places">
<input type="number" id="places" required [(ngModel)]="travel.places" name="places">
<div hidden="places.valid || places.pristine" class="alert alert-danger">
El destino es obligatorio
</div>
@@ -37,7 +37,7 @@
<div>
<label for="description">Descripción</label>
<input type="text" id="description" [(ngModel)]="newTravel.description" name="description">
<input type="text" id="description" [(ngModel)]="travel.description" name="description">
</div>

View File

@@ -12,19 +12,18 @@ export class TravelFormComponent implements OnInit {
@Input() travel: Travel;
@Input() acceptButtonText: string;
newTravel: Travel;
submitted = false;
minDate = new Date();
constructor() { }
ngOnInit(): void {
this.newTravel = { ...this.travel };
this.travel = this.travel;
}
onSubmit(): void {
this.submitted = true;
this.travelSubmitted.emit(this.newTravel);
this.travelSubmitted.emit(this.travel);
}
onCancel(): void {